jenkins集成gitlab自动docker build上传到Harbor

**

一、jenkins构建docker镜像

**
1、创建一个自由风格的流水线,如下:
在这里插入图片描述
添加gitlab的仓库以及credentail,如下:
在这里插入图片描述
增加执行动作,使用shel脚本方式,如下:
在这里插入图片描述

#!/bin/sh
#
DATE=`date +%m%d%H%M`
DIR="/var/jenkins_home/workspace/nginx"
 
/bin/docker build -t nginx_$DATE $DIR | tee $DIR/Docker_build_result.log
 
RESULT=$(cat $DIR/Docker_build_result.log | tail -n 1)
 
if [["$RESULT" != *Successfully*]];then
  exit -1
fi

点击保存之后,进行构建,如下:
在这里插入图片描述

Started by user root
Running as SYSTEM
Building on master in workspace /var/jenkins_home/workspace/nginx
The recommended git tool is: NONE
using credential jenkins to gitlab passwd
 > /usr/bin/git rev-parse --resolve-git-dir /var/jenkins_home/workspace/nginx/.git # timeout=10
Fetching changes from the remote Git repository
 > /usr/bin/git config remote.origin.url http://172.20.150.168:48080/root/docker_build.git # timeout=10
Fetching upstream changes from http://172.20.150.168:48080/root/docker_build.git
 > /usr/bin/git --version # timeout=10
 > git --version # 'git version 2.20.1'
using GIT_ASKPASS to set credentials jenkins to gitlab passwd
 > /usr/bin/git fetch --tags --force --progress -- http://172.20.150.168:48080/root/docker_build.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > /usr/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision 6311f7087bb9069674a5d3c93d07f93d4cf5c845 (refs/remotes/origin/master)
 > /usr/bin/git config core.sparsecheckout # timeout=10
 > /usr/bin/git checkout -f 6311f7087bb9069674a5d3c93d07f93d4cf5c845 # timeout=10
Commit message: "update Harbor login"
 > /usr/bin/git rev-list --no-walk 543b974cc4c5fea1d9725cbd02fd4e8adbf02650 # timeout=10
[nginx] $ /bin/sh /tmp/jenkins7598503226480488927.sh
Sending build context to Docker daemon  1.418MB

Step 1/13 : FROM nginx
 ---> 62d49f9bab67
Step 2/13 : MAINTAINER dkey
 ---> Using cache
 ---> 0e234f411125
Step 3/13 : ENV RUN_USER nginx
 ---> Using cache
 ---> 06e756991cf4
Step 4/13 : ENV RUN_GROUP nginx
 ---> Using cache
 ---> 52d08247ead6
Step 5/13 : ENV DATA_DIR /data/web
 ---> Using cache
 ---> be45bd47df4e
Step 6/13 : ENV LOG_DIR /data/log/nginx
 ---> Using cache
 ---> 8ed96abf4251
Step 7/13 : RUN mkdir /data/log/nginx -p
 ---> Using cache
 ---> bb017c753f64
Step 8/13 : RUN chown nginx.nginx -R /data/log/nginx
 ---> Using cache
 ---> 2a513908c565
Step 9/13 : ADD web.zip /data/web
 ---> Using cache
 ---> b3ed9bf47cab
Step 10/13 : ADD nginx.conf /etc/nginx/nginx.conf
 ---> Using cache
 ---> 2bcff9c54def
Step 11/13 : ADD default.conf /etc/nginx/conf.d/default.conf
 ---> Using cache
 ---> 79ed88b1d676
Step 12/13 : EXPOSE 80
 ---> Using cache
 ---> bf957bd080b5
Step 13/13 : ENTRYPOINT nginx -g "daemon off;"
 ---> Using cache
 ---> da8cd66822b7
Successfully built da8cd66822b7
Successfully tagged nginx_06240616:latest
/tmp/jenkins7598503226480488927.sh: 10: /tmp/jenkins7598503226480488927.sh: [[Successfully tagged nginx_06240616:latest: not found
Finished: SUCCESS

2、登录操作系统查看构建好的镜像,如下:

[root@weizb-2 ~]# docker images | grep nginx_0624
nginx_06240616  latest     da8cd66822b7   21 hours ago  134MB

**

二、jenkins集成gitlab自动docker build上传到Harbor

**
1、创建一个pipeline流水线项目,如下:
在这里插入图片描述
在这里插入图片描述
生成token,如下:
在这里插入图片描述
如下依次选择SCM以及对应的gitlab仓库
在这里插入图片描述
选择要克隆的分支以及脚本路径,默认为Jenkins
在这里插入图片描述
2、gitlab添加webhooks,如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
3、gitlab编写Jenkins,并上传到gitlab,如下:

Jnekins文件内容如下:
pipeline {
    agent any
    environment {
        Name = "daemon3"                                                                                                                                                   
        DIR = "/var/jenkins_home/workspace/daemon-nginx"
    }    
    stages {
        stage("build") {
            steps {
            sh "docker build -t nginx_${Name} ${DIR} | tee ${DIR}/Docker_build_result.log"
            }
        }
        stage("push"){
            steps {
            sh "docker tag nginx_${Name} 172.20.150.168/daemon/nginx_${Name}:latest"
            ######调用之前创建的harbor用户名和密码的credentail
            withCredentials([usernamePassword(credentialsId: 'harbor-credential', passwordVariable: 'dockerPassword', usernameVariable: 'dockerUser')]) {
            sh "docker login -u ${dockerUser} -p ${dockerPassword} 172.20.150.168"
            sh "docker push 172.20.150.168/daemon/nginx_${Name}:latest"
            }
            }
        }
    }    
}        

上传到gitlab,如下:
[root@weizb-3 docker_build]# vim Jenkinsfile 
[root@weizb-3 docker_build]# git add .
[root@weizb-3 docker_build]# git commit -m "update image name daemon3"
[master b7bd0ec] update image name daemon3
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@weizb-3 docker_build]# git remote -v
origin	http://172.20.150.168:48080/root/docker_build.git (fetch)
origin	http://172.20.150.168:48080/root/docker_build.git (push)
[root@weizb-3 docker_build]# git push -u origin
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 287 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To http://172.20.150.168:48080/root/docker_build.git
   6311f70..b7bd0ec  master -> master
Branch master set up to track remote branch master from origin.

登录jenkins,会自动触发docker build操作,如下:
在这里插入图片描述

Started by GitLab push by Administrator
Obtained Jenkinsfile from git http://172.20.150.168:48080/root/docker_build.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/daemon-nginx
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
The recommended git tool is: NONE
using credential jenkins to gitlab passwd
 > /usr/bin/git rev-parse --resolve-git-dir /var/jenkins_home/workspace/daemon-nginx/.git # timeout=10
Fetching changes from the remote Git repository
 > /usr/bin/git config remote.origin.url http://172.20.150.168:48080/root/docker_build.git # timeout=10
Fetching upstream changes from http://172.20.150.168:48080/root/docker_build.git
 > /usr/bin/git --version # timeout=10
 > git --version # 'git version 2.20.1'
using GIT_ASKPASS to set credentials jenkins to gitlab passwd
 > /usr/bin/git fetch --tags --force --progress -- http://172.20.150.168:48080/root/docker_build.git +refs/heads/*:refs/remotes/origin/* # timeout=10
skipping resolution of commit remotes/origin/master, since it originates from another repository
 > /usr/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision b7bd0ec6dd2219bc6ce5ac69ff134f70dcdb528a (refs/remotes/origin/master)
 > /usr/bin/git config core.sparsecheckout # timeout=10
 > /usr/bin/git checkout -f b7bd0ec6dd2219bc6ce5ac69ff134f70dcdb528a # timeout=10
Commit message: "update image name daemon3"
 > /usr/bin/git rev-list --no-walk 6311f7087bb9069674a5d3c93d07f93d4cf5c845 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (build)
[Pipeline] sh
+ docker build -t nginx_daemon3 /var/jenkins_home/workspace/daemon-nginx
+ tee /var/jenkins_home/workspace/daemon-nginx/Docker_build_result.log
Sending build context to Docker daemon  1.424MB

Step 1/13 : FROM nginx
 ---> 62d49f9bab67
Step 2/13 : MAINTAINER dkey
 ---> Using cache
 ---> 0e234f411125
Step 3/13 : ENV RUN_USER nginx
 ---> Using cache
 ---> 06e756991cf4
Step 4/13 : ENV RUN_GROUP nginx
 ---> Using cache
 ---> 52d08247ead6
Step 5/13 : ENV DATA_DIR /data/web
 ---> Using cache
 ---> be45bd47df4e
Step 6/13 : ENV LOG_DIR /data/log/nginx
 ---> Using cache
 ---> 8ed96abf4251
Step 7/13 : RUN mkdir /data/log/nginx -p
 ---> Using cache
 ---> bb017c753f64
Step 8/13 : RUN chown nginx.nginx -R /data/log/nginx
 ---> Using cache
 ---> 2a513908c565
Step 9/13 : ADD web.zip /data/web
 ---> Using cache
 ---> b3ed9bf47cab
Step 10/13 : ADD nginx.conf /etc/nginx/nginx.conf
 ---> Using cache
 ---> 2bcff9c54def
Step 11/13 : ADD default.conf /etc/nginx/conf.d/default.conf
 ---> Using cache
 ---> 79ed88b1d676
Step 12/13 : EXPOSE 80
 ---> Using cache
 ---> bf957bd080b5
Step 13/13 : ENTRYPOINT nginx -g "daemon off;"
 ---> Using cache
 ---> da8cd66822b7
Successfully built da8cd66822b7
Successfully tagged nginx_daemon3:latest
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (push)
[Pipeline] sh
+ docker tag nginx_daemon3 172.20.150.168/daemon/nginx_daemon3:latest
[Pipeline] sh
+ docker login -u admin -p Harbor12345 172.20.150.168
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /var/jenkins_home/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[Pipeline] sh
+ docker push 172.20.150.168/daemon/nginx_daemon3:latest
The push refers to repository [172.20.150.168/daemon/nginx_daemon3]
2a4c0ca7ebfd: Preparing
950ef405b921: Preparing
b72884871a83: Preparing
4259a28c7d18: Preparing
74ee82ee2c1b: Preparing
64ee8c6d0de0: Preparing
974e9faf62f1: Preparing
15aac1be5f02: Preparing
23c959acc3d0: Preparing
4dc529e519c4: Preparing
7e718b9c0c8c: Preparing
974e9faf62f1: Waiting
15aac1be5f02: Waiting
23c959acc3d0: Waiting
4dc529e519c4: Waiting
7e718b9c0c8c: Waiting
74ee82ee2c1b: Mounted from daemon/nginx_daemon2
4259a28c7d18: Mounted from daemon/nginx_daemon2
b72884871a83: Mounted from daemon/nginx_daemon2
2a4c0ca7ebfd: Mounted from daemon/nginx_daemon2
950ef405b921: Mounted from daemon/nginx_daemon2
64ee8c6d0de0: Mounted from daemon/nginx_daemon2
974e9faf62f1: Mounted from daemon/nginx_daemon2
15aac1be5f02: Mounted from daemon/nginx_daemon2
23c959acc3d0: Mounted from daemon/nginx_daemon2
4dc529e519c4: Mounted from daemon/nginx_daemon2
7e718b9c0c8c: Mounted from daemon/nginx_daemon2
latest: digest: sha256:b2f95416378eb1d3405123efadfe07102da4bec98120847cadaeb469c6d03d35 size: 2609
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

4、登录系统查看build的镜像daemon3,如下:

[root@weizb-2 ~]# docker images | grep daemon3
172.20.150.168/daemon/nginx_daemon3   latest   da8cd66822b7   21 hours ago    134MB
nginx_daemon3                         latest   da8cd66822b7   21 hours ago    134MB

5、登录docker harbor查看上传好的镜像,如下:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值