jenkins触发自动构建部署

2022-03-11 12:08:27 浏览数 (1)

jenkins触发自动构建部署(三)

环境要求: jenkins主机:192.168.12.26 gitlab主机:192.168.12.23 实验目的:

用户通过git客户端push代码到gitlab服务器,触发jenkins拉取代码,打包并部署到目标服务器。

一.Jenkins新建Item

1、创建java的maven项目到gitlab

我们需要准备一个java的maven项目来实验,这里从网上找了一个jeesns的项目: 先克隆gitlab上我们新建的项目,然后再把jeesns代码推上去:

代码语言:javascript复制
[root@jenkins1 ~]# mkdir project
[root@jenkins1 ~]# cd project/
[root@jenkins1 project]# git clone ssh://git@192.168.12.23:10022/lym/test.git
[root@jenkins1 project]# ls
test

test是我在gitlab上创建的项目,下面把jeesns代码拷贝过来并push。 在另外一个目录下先clone jeesns源码下来:

代码语言:javascript复制
[root@jenkins1 jess]# git clone https://gitee.com/LeoMs1996/jeesns.git
[root@jenkins1 jess]# ls
jeesns
[root@jenkins1 jess]# cd jeesns/
[root@jenkins1 jeesns]# ls
_config.yml  LICENSE  pom.xml  README.md  src
[root@jenkins1 jeesns]# cp -rv * /root/project/test

现在进入project目录下去push

代码语言:javascript复制
[root@jenkins1 test]# pwd
/root/project/test
[root@jenkins1 test]# ls
_config.yml  LICENSE  pom.xml  README.md  src
[root@jenkins1 test]# git add .
[root@jenkins1 test]# git commit -m "jessns"
...
Writing objects: 100% (3/3), 282 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To ssh://git@192.168.12.23:10022/lym/test.git
   da6da25..a21b641  master -> master

现在代码就推到gitlab上去了。

2、新建maven Item

新建Item -- 构建一个maven项目 -- 确定。

需要填入的项:
(1)源码管理:
  • GIT
  • Repository URL:ssh://git@192.168.12.23:10022/lym/test.git
  • Credentials: 选择之前设置好的gitlab_root_ssh凭据。
(2)构建触发器:
  • [ ] Build when a change is pushed to GitLab. 勾选✔
  • 点击高级 -- Generate
gitlab添加webhook

登录gitlab,进入到项目页面下:

project -- Settings -- Integrations -- 填写URL和Secret Token -- Add webhook 填入内容:

  • URL:http://192.168.12.26:8080/project/tomcat
  • Secret Tokenl:b4a52ce3d4da2e4bfbc58e817ba6d798

这里添加完webhook后,做Test push events会报错Hook executed successfully but returned HTTP 403,解决:

  • 1、进入jenkins

Manage Jenkins- >Configure Global Security -> 授权策略 -> Logged-in users can do anything (登录用户可以做任何事情) 点选 -> 匿名用户具有可读权限 点选

  • 2、去掉跨站点请求伪造 点选 放开

Manage Jenkins- >Configure Global Security -> 跨站请求伪造保护(取消勾选)

  • 3、去掉Gitlab enable authentication 点选 放开

Manage Jenkins -> Configure System -> Enable authentication for '/project' end-point(取消勾选)

现在去gitlab系统中Test push events就不报错了,提示“Hook executed successfully:HTTP 200”

(3) Build
  • Root POM :pom.xml (这个文件在项目目录下)
  • Goals and options : clean install
  • MAVEN_OPTS : -Dfile.encoding=UTF-8 (JVM参数)

保存之后--Build Now就可以构建了,在本地shell环境下push后也可以实现自动触发构建。

代码语言:javascript复制
[root@jenkins1 workspace]# pwd
/var/lib/jenkins/workspace
[root@jenkins1 workspace]# ls tomcat/target/
classes  generated-sources  jeesns  jeesns.war  maven-archiver  maven-status

在jenkins目录下的workspace也出现了target下的jeesns.war

下面没用得东西 待整理吧

webhook地址:http://192.168.20.14:8080/gitlab/build_now/job1

执行shell参考脚本:

代码语言:javascript复制
#source /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_202
export JRE_HOME=/usr/local/jdk1.8.0_202/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$PATH:$JAVA_HOME/bin

export SONAR_HOME=/usr/local/sonarqube-7.7
export PATH=$PATH:$SONAR_HOME/bin/linux-x86-64

export SONAR_RUNNER_HOME=/usr/local/sonar-scanner-3.3.0.1492
export PATH=$PATH:$SONAR_RUNNER_HOME/bin 
export SONAR_RUNNER_OPTS="-Xmx512m -XX:MaxPermSize=128m"

export MAVEN_HOME=/usr/local/apache-maven-3.6.0
export PATH=$PATH:$MAVEN_HOME/bin

DIR_Backup=/backup/Test
DIR_Upfile=/data/paas/Test

case $Status in
  Deploy)
    echo "Status:$Status"
    cd ${DIR_Backup}
    if [ -d ${JOB_NAME} ]
    then
      echo "The files is already exists"
    else
      mkdir ${DIR_Backup}/${JOB_NAME}
    fi
    
    cd ${JOB_NAME}
    if [ -d ${BUILD_NUMBER} ]
    then
      echo "The files is already exists"
    else
      mkdir ${DIR_Backup}/${JOB_NAME}/${BUILD_NUMBER}
    fi
    
    cd ${WORKSPACE}
    /usr/local/apache-maven-3.6.0/bin/mvn sonar:sonar -Dsonar.projectKey=${JOB_NAME} -Dsonar.projectName=${JOB_NAME} -Dsonar.host.url=http://sonar.yunjingit.cn -Dsonar.java.binaries=. -Dsonar.login=ec8913dc51a95ebd0773845c0b2b2f0adec4f742
    /usr/local/apache-maven-3.6.0/bin/mvn -B -f ${WORKSPACE}/pom.xml clean install -Dmaven.test.skip=true -Ptest
    /bin/cp ${WORKSPACE}/target/*.jar ${DIR_Upfile}/${JOB_NAME}/server/
    /bin/cp ${WORKSPACE}/target/*.jar ${DIR_Backup}/${JOB_NAME}/${BUILD_NUMBER}/
    sudo su - ansible -c "cd /etc/ansible/playbook/commit && ./commit.sh ${JOB_NAME}"
    echo "Completin!"
    ;;
  Rollback)
    echo "Status:${Status}"
    echo "Version:${Version}"
    cp ${DIR_Backup}/${JOB_NAME}/${Version}/*.jar ${DIR_Upfile}/${JOB_NAME}/server/
    sudo su - ansible -c "cd /etc/ansible/playbook/commit && ./commit.sh ${JOB_NAME}"
    echo "Completin!"
    ;;
  *)
  exit
    ;;
esac

0 人点赞