参数化构建

jenkins支持参数化构建,类似于脚本中的参数,可以实现灵活的构建任务

1、字符参数实现实现不同分支的部署

图片[1]-参数化构建-李佳程的个人主页
图片[2]-参数化构建-李佳程的个人主页
图片[3]-参数化构建-李佳程的个人主页
# 脚本
[root@jenkins01 ~]# cat /data/jenkins/scripts/hello_job1.sh
#!/bin/bash
DATE=`date +%F-%s`
HOST_LIST="
192.168.1.51
192.168.1.52
"
tar -C $WORKSPACE/ -cf hello.tar .

for host in $HOST_LIST;do
    scp hello.tar $host:/data/tomcat/appdir/hello-${DATE}.tar
    ssh $host "systemctl stop tomcat && \
       mkdir -p /data/tomcat/webdir/hello-${DATE} && \
       tar xf /data/tomcat/appdir/hello-${DATE}.tar -C /data/tomcat/webdir/hello-${DATE} && \
       rm -f /usr/local/tomcat/webapps/hello && \
       ln -s /data/tomcat/webdir/hello-${DATE} /usr/local/tomcat/webapps/ROOT/hello && \
       systemctl start tomcat"
done
图片[4]-参数化构建-李佳程的个人主页

2、选项参数实现实现不同分支的部署

创建新分支

图片[5]-参数化构建-李佳程的个人主页
图片[6]-参数化构建-李佳程的个人主页

确认分支创建成功

图片[7]-参数化构建-李佳程的个人主页

准备脚本

[root@jenkins01 git]# cat /data/git/scripts/deploy.sh
#!/bin/bash

BRANCH=$1

cd /data/git && rm -rf testproject
git clone -b $BRANCH git@192.168.1.71:testgroup/testproject.git
cd testproject
case  $BRANCH in
main)
    scp -r * 192.168.1.51:/usr/share/nginx/html/
    ;;
devel)
    scp -r * 192.168.1.52:/usr/share/nginx/html/
    ;;
*)
    echo $BRANCH is error
    ;;
esac

[root@jenkins01 git]# chmod +x scripts/deploy.sh
图片[8]-参数化构建-李佳程的个人主页
图片[9]-参数化构建-李佳程的个人主页
图片[10]-参数化构建-李佳程的个人主页

修改不同分支的代码

[root@client data]# git clone git@192.168.1.71:testgroup/testproject.git
Cloning into 'testproject'...
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 10
Receiving objects: 100% (13/13), done.

[root@client data]# cd testproject/
[root@client testproject]# ll
total 4
-rw-r--r-- 1 root root 116 Dec 20 09:24 index.html

[root@client testproject]# vim index.html
<h1> index html v111111</h1>
<h1> index html v222222</h1>
<h1> index html v333333</h1>
<h1> index html v444444</h1>
<h1> index html v555555-main</h1>

[root@client testproject]# git add .
[root@client testproject]# git commit -m v5
[main ff5b9b9] v5
 1 file changed, 1 insertion(+)
[root@client testproject]# git push

# 切换至devel分支进行修改代码
[root@client testproject]# git checkout develop
M	index.html
Already on 'develop'

[root@client testproject]# git branch
* develop

[root@client testproject]# vim index.html

<h1> index html v111111</h1>
<h1> index html v222222</h1>
<h1> index html v333333</h1>
<h1> index html v444444</h1>
<h1> index html v555555-develop</h1>

[root@client testproject]# git add .
[root@client testproject]# git commit -m v5
[develop bc28428] v5
 1 file changed, 1 insertion(+)

[root@client testproject]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 275 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote:
remote: To create a merge request for develop, visit:
remote:   https://192.168.1.71/testgroup/testproject/-/merge_requests/new?merge_request%5Bsource_branch%5D=develop
remote:
To git@192.168.1.71:testgroup/testproject.git
   b072659..bc28428  develop -> develop

执行修改过的构建

图片[11]-参数化构建-李佳程的个人主页
图片[12]-参数化构建-李佳程的个人主页

执行devel分支的构建

图片[13]-参数化构建-李佳程的个人主页
图片[14]-参数化构建-李佳程的个人主页

3、选项参数实现不同分支的部署和回滚

图片[15]-参数化构建-李佳程的个人主页
图片[16]-参数化构建-李佳程的个人主页
图片[17]-参数化构建-李佳程的个人主页
[root@jenkins01 ~]# cat /data/git/scripts/deploy.sh
#!/bin/bash

PROJECT="testproject"

PROJECT_DIR="/data"

APPDIR="/data/tomcat/appdir"

WEBDIR="/data/tomcat/webdir"

WEBAPPS="/usr/local/tomcat/webapps"

HOSTS="
192.168.1.51
192.168.1.52
"

DATE=`date +%F_%H-%M-%S`

pull_code(){
    rm -rf $WORKSPACE
    cd ${JENKINS_HOME}/workspace
    git clone -b $BRANCH git@192.168.1.71:testgroup/$PROJECT.git
    cd ${WORKSPACE}
    tar zcf ${PROJECT_DIR}/${PROJECT}-${DATE}.tar.gz ./*
}

push_code_webservers () {
    for host in $HOSTS;do
        scp ${PROJECT_DIR}/${PROJECT}-${DATE}.tar.gz $host:${APPDIR}
        ssh $host mkdir $WEBDIR/${PROJECT}-${DATE}
        ssh $host tar xf ${APPDIR}/${PROJECT}-${DATE}.tar.gz -C $WEBDIR/${PROJECT}-${DATE}
        ssh $host systemctl stop tomcat
        ssh $host rm -rf ${WEBAPPS}/ROOT
        ssh $host ln -s ${WEBDIR}/${PROJECT}-${DATE}  ${WEBAPPS}/ROOT
        ssh $host chown -R tomcat.tomcat $WEBAPPS
        ssh $host systemctl start tomcat
    done
}

deploy (){
    pull_code
    push_code_webservers
}

rollback (){
    local current_version previous_version
    for host in $HOSTS;do
        current_version=$(ssh $host "readlink /usr/local/tomcat/webapps/ROOT")
        current_version=`basename $current_version`
        previous_version=`ssh $host "ls /data/tomcat/webdir/ |grep -B1 $current_version |head -n1"`
        echo $current_version
        echo $before_version
        ssh $host systemctl stop tomcat
        ssh $host rm -f ${WEBAPPS}/ROOT
        ssh $host ln -s ${WEBDIR}/$previous_version ${WEBAPPS}/ROOT
        ssh $host systemctl start tomcat
   done
}

case $OPS in
deploy)
    deploy
    ;;
rollback)
    rollback
    ;;
*)
    echo "Usage: `basename $0` deploy|rollback"
esac

第一次部署

图片[18]-参数化构建-李佳程的个人主页

第二次更新

图片[19]-参数化构建-李佳程的个人主页

回滚到第一次部署

图片[20]-参数化构建-李佳程的个人主页
图片[21]-参数化构建-李佳程的个人主页

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享