JAVA - Jenkins集成Github 成功

1Jenkins集成Github和Git实现自动化部署

1.1Git和Github的区别

Github是一个基于Git的项目托管平台,GitHub 的替代产品有哪些?

Github和Gitlab的区别?

1.2服务器上安装Java

[root@localhost ~]# yum install java

[root@localhost ~]# java -version

1.3服务器上安装Git

[root@localhost ~]# yum install git

[root@localhost ~]# git version

1.4服务器上安装Tomcat

下载地址:https://tomcat.apache.org

[root@localhost ~]# yum install wget

[root@localhost ~]# wget http://mirrors.koehn.com/apache/tomcat/tomcat-9/v9.0.11/bin/apache-tomcat-9.0.11.zip

[root@localhost ~]# yum install unzip

[root@localhost ~]# unzip apache-tomcat-9.0.11.zip

[root@localhost apache-tomcat-9.0.11]# chmod a+x -R *

[root@localhost apache-tomcat-9.0.11]# bin/startup.sh

[root@localhost apache-tomcat-9.0.11]# ps -ef|grep tomcat

开放8080接口

[root@localhost apache-tomcat-9.0.11]# firewall-cmd --zone=public --add-port=8080/tcp --permanent

[root@localhost apache-tomcat-9.0.11]# firewall-cmd --reload

# 查看防火墙

[root@localhost apache-tomcat-9.0.11]# firewall-cmd --list-all

验证:http://192.168.137.136:8080/

1.5服务器上安装Maven

[root@localhost ~]# wget http://mirror.metrocast.net/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.zip

[root@localhost ~]# unzip apache-maven-3.5.4-bin.zip

配置

[root@localhost ~]# cd apache-maven-3.5.4

[root@localhost apache-maven-3.5.4]# pwd

[root@localhost apache-maven-3.5.4]# yum install vim

[root@localhost apache-maven-3.5.4]# vim /etc/profile

#在profile文件末尾加上这两句:

export MAVEN_HOME=/root/apache-maven-3.5.4

export PATH=$MAVEN_HOME/bin:$PATH

#保存profile文件

[root@localhost apache-maven-3.5.4]# . /etc/profile

验证

[root@localhost apache-maven-3.5.4]# mvn -version

1.6本地Windows安装Jenkins

1.6.1下载jenkins.war到C:\Jenkins,下载地址:https://jenkins.io,打开cmd

用命令来安装Jenkins失败,可以直接跳到下一段;

C:\Jenkins>java -jar jenkins.war

出现了一个错误

SEVERE: Running with Java class version 54.0, but 52.0 is required.Run with the --enable-future-java flag to enable such behavior.

Java SE 8 = 52, Jenkins requires Java 8, but you are running 10.0.1+10 from C:\Program Files\Java\jre-10.0.1

官网指南

C:\Jenkins>java --add-modules java.xml.bind -jar jenkins.war --enable-future-java --httpPort=8080 --prefix=/jenkins

最后通过java -jar的方式来启动Jenkins还是没有成功:(

虽然成功获取了如下信息:

Jenkins initial setup is required. An admin user has been created and a password generated.

Please use the following password to proceed to installation:

70baae147267441183b646a27c26c8ef

This may also be found at: C:\Users\hylyn\.jenkins\secrets\initialAdminPassword。


1.6.2直接安装Windows安装包

下载地址:https://jenkins.io,选择Windows版

initialAdminPassword:bd498f0a059441818443819ccc54176e

安装成功:http://localhost:8080/

1.6.3安装插件

2个插件:Rebuilder和Safe Restart

1.6.4新建节点

新建一个节点DIT,开放22端口

[root@localhost apache-maven-3.5.4]# firewall-cmd --zone=public --add-port=22/tcp --permanent

[root@localhost apache-maven-3.5.4]# firewall-cmd --reload

1.6.5测试一个任务

测试成功,证明Jenkins能正常在Linux服务器上运行

http://localhost:8080/job/TestTask/1/console

1.7本地安装Git

1.7.1选定项目目录

clone到本地失败,需要配置与Github的通信

$ git clone git@github.com:hylyn/order.git

1.7.2配置本地Git与Github通信的公钥

$ git config --global user.name "xxx"

$ git config --global user.email "xxx@gmail.com"

$ ssh-keygen -t rsa -C "xxx@gmail.com"

1.8自动化部署

1.8.1出错信息1

配置Git在本地的运行地址

且需配置Git在Linux虚拟机上的运行地址


1.8.2出错信息2

需配置服务端的Git

在服务器上建立Git与Github通信的公钥

[root@localhost ~]# git config --global user.name "hylyn"

[root@localhost ~]# git config --global user.email "hylyn@gmail.com"

[root@localhost ~]# ssh-keygen -t rsa -C "hylyntan@gmail.com"

[root@localhost ~]# vim /root/.ssh/id_rsa.pub

在Github上添加公钥后

[root@localhost ~]# ssh git@github.com

执行任务成功,总结一下,Windows上集成自动部署有三大坑:

1) 无法用Java 10运行jar包,然后无法在Windows上安装Jenkins;

2) 本地Git的安装和与Github的通信;

3) 虚拟机上Git的安装和与Github的通信;

最后终于成功,截图留念:


1.8.3发布到Linux服务器

需要在服务器上安装MySQL

更新pom文件


在服务端安装JDK,解决‘No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?’的问题

yum install java-1.8.0-openjdk-devel.x86_64

或尝试

yum install java-1.8.0-openjdk

yum install java-1.8.0-openjdk-devel

1.9开源自己的项目到Github

1.9.1在另一台电脑上建立Git与Github通信的公钥

$ git config --global user.name "hylyn"

$ git config --global user.email "hylyn@gmail.com"

$ ssh-keygen -t rsa -C "hylyntan@gmail.com"

配制公钥到自己的Github账号:

Settings - SSH and GPG keys

1.9.2Github主页https://github.com/创建New repository,项目在Github上就叫仓库

1.9.3版本线的概念

C2(子版本)指向C1(父版本) : C2—>C1

1.9.4命令

$ git init

$ git add .

$ git commit -m "first commit"

$ git remote add origin git@github.com:hylyn/samemart-monolith.git

$ git pull origin master

$ git push -u origin master

fatal: refusing to merge unrelated histories的解决方法

git pull origin master --allow-unrelated-histories

1.9.5划重点

A. 本地仓库由git维护的三棵“树"组成:第1个是工作目录,它持有实际文件(即可以直接打开修改的文件);第2个是缓存区(index),它像个缓存区域,临时保存你的改动;最后一个是HEAD,指向你最近一次提交后的结果(这个结果还是在本地电脑里面)

B. git push -u origin master相当于 git push origin master 加上 git branch --set-upstream-to=origin/master master

C. git pull origin feature/UX-1400相当于git fetch origin 加上 git merge origin/feature/UX-1400

D. 当团队协助时,需从自己修改的分支上拉代码到Master分支上(pull request);

完整流程:github的项目—>fork到自己的github网站分支—>clone到自己的本地版本—>修改后push到自己的github网站分支—>将自己gitub网站分支pull request到github的原项目

1.9.6清除不想add的文件

$ git reset --hard origin/master

$ git clean -fd

$ git gc

截图留念:


1.9.6在相应文件夹下用Git Bash创建.gitignore文件

$ touch .gitignore

1.9.7从Github网站创建.gitignore文件


1.9.8从另一台电脑clone

$ git clone https://github.com/hylyn/samemart.git

1.9.9两个非常容易混淆的博主

https://www.liaoxuefeng.com/

http://www.ruanyifeng.com

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容