Fork me on GitHub
Suzf  Blog

How-to use Jenkins and GitLab to build Continuous Integration

环境: Jenkins + GitLab + Maven + Tomcat

主要实现自动构建部署 Java Web应用。

工作流程:

提交代码到 GitLab --> Jenkins触发构建任务 --> Maven编译打包 --> Jenkins将war包部署到Tomcat。 安装 GitLab 安装 Jenkins 安装  Maven & JDK 安装 Plugins Publish over SSH

修改 Jenkins 密码

# passwd  jenkins

generate a ssh key for gitlab

cd $HOME
ssh-keygen -t rsa -C "[email protected]" -f .ssh/gitlab_rsa

reslove multi ssh key issue

cat  >> $HOME/.ssh/config << EOF
Host git.suzf.net
    IdentityFile ~/.ssh/gitlab_rsa
    User git
    Port 22
EOF

设置Jenkins用户git全局变量

git config --global user.name "Jenkins"
git config --global user.email "[email protected]"

登陆到 GitLab 为 Jenkins 设置 SSH Keys 测试 GitLab

[email protected]:~$ ssh -T [email protected]
Welcome to GitLab, zfsu!

Jekins SSH 配置 Jenkins --> 配置 --> Publish over SSH

[email protected]:~# useradd deploy
[email protected]:~# passwd deploy 

su - deploy
[email protected]:~$ ssh-keygen -t rsa -C "[email protected]" -f .ssh/jenkins_deploy_rsa
[email protected]:~$ ssh-copy-id -i .ssh/jenkins_deploy_rsa [email protected]

su - root
cp /home/deploy/.ssh/jenkins_deploy_rsa* /var/lib/jenkins/.ssh
chown -R jenkins.jenkins /var/lib/jenkins/.ssh -R

su - jenkins
cat  >> $HOME/.ssh/config << EOF
Host *
    IdentityFile ~/.ssh/jenkins_deploy_rsa
    User deploy
    Port 22
EOF

create_jenkins_credentials

create_jenkins_credentials

Publish over ssh

jenkins_publish_over_ssh

创建任务,配置项目信息

首先按照提示创建一个任务,选择Maven Projecthello_jenkins_maven 进入Config页面,点击Source Code Management的git选项 hello_maven_source_code 构建触发器 build_trigger Build jenkins_build Post steps jenkins_post_steps jenkins_jobs_bash_detail_output 构建后操作 jenkins_build_done_steps 执行构建

Building in workspace /var/lib/jenkins/jobs/hello_maven/workspace
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
...
...
Established TCP socket on 39748
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.082 s
[INFO] Final Memory: 17M/147M
[INFO] ------------------------------------------------------------------------
[JENKINS] Archiving /var/lib/jenkins/jobs/hello_maven/workspace/pom.xml to springmvc-maven/easy-springmvc-maven/0.0.1-SNAPSHOT/easy-springmvc-maven-0.0.1-SNAPSHOT.pom
[JENKINS] Archiving /var/lib/jenkins/jobs/hello_maven/workspace/target/easy-springmvc-maven.war to springmvc-maven/easy-springmvc-maven/0.0.1-SNAPSHOT/easy-springmvc-maven-0.0.1-SNAPSHOT.war
SSH: Connecting from host [happycode]
SSH: Connecting with configuration [deploy] ...
SSH: Creating session: username [deploy], hostname [lucy.suzf.net], port [22]
SSH: Connecting session ...
channel stopped

SSH: Connected
SSH: Opening SFTP channel ...
SSH: SFTP channel open
SSH: Connecting SFTP channel ...
SSH: Connected
SSH: cd [/opt/data/jenkins/tomcat8]
SSH: OK
SSH: cd [/opt/data/jenkins/tomcat8]
SSH: OK
SSH: put [easy-springmvc-maven.war]
SSH: OK
SSH: Opening exec channel ...
SSH: EXEC: channel open
SSH: EXEC: STDOUT/STDERR from command [bash /opt/script/deploy/deploy_tomcat8.sh] ...
SSH: EXEC: connected
Hello world

SSH: EXEC: completed after 201 ms
SSH: Disconnecting configuration [deploy] ...
SSH: Transferred 1 file(s)

参考文档

[0] https://wiki.jenkins-ci.org/display/JENKINS/Publish+Over+SSH+Plugin

[1] https://segmentfault.com/a/1190000002895521

[2] http://www.cnblogs.com/binyue/p/4815459.html