很早之前写过一篇jenkins集成docker的文章,使用的是CloudBees Docker Build and Publish plugin插件。这篇文章是直接使用shell脚本做的,主要是这次有一个需求是检测harbor仓库里面是否已经存在要构建的镜像,如果存在就放弃构建,如果不存在则构建镜像并上传到harbor仓库,我这里是通过请求harbor的api来检测的。
整个过程大致分为四个步骤,画了一个简单图:
1、在jenkins上构建一个自由风格的软件项目。
2、源码管理使用git。
这里的账号是gitlab上的账号。
3、构建触发器,我这里使用的是Poll SCM定时检测,即每隔一段时间检测一次代码是否有更新。
4、添加构建步骤。
这个过程是这样:
- 读取自定义tag,并赋值给一个变量。
- 检测harbor仓库上是否有该镜像。如果没有则构建镜像并上传到harbor,如果有则退出。
- 通用ssh发布到kubernetes。
需要单独说一下的是,我这里需要使用自定义tag,我使用一个插件Environment Injector Plugin将这个变量变成jenkins环境变量,让其能在jenkins上调用。
另外,在远程机器上ssh执行命令需要添加授权。系统设置 -> SSH remote hosts
这里的账号是ssh连接的远程主机的用户和私钥(或者账号和密码)。
5、到这里整个过程基本完成了,进入项目,尝试构建。
控制台输出,由于太长我就直接贴文字了:
Started by user shengyongp[EnvInject] - Loading node environment variables.Building in workspace /var/lib/tomcat8/.jenkins/workspace/k8sdesktop > git rev-parse --is-inside-work-tree # timeout=10Fetching changes from the remote Git repository > git config remote.origin.url git@git.oupeng.com:sa/k8sdesktop.git # timeout=10Fetching upstream changes from git@git.oupeng.com:sa/k8sdesktop.git > git --version # timeout=10using GIT_ASKPASS to set credentials ?> git fetch --tags --progress git@git.oupeng.com:sa/k8sdesktop.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10Checking out Revision 7593b720066259d291a31a8eb25121c260dd6e00 (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f 7593b720066259d291a31a8eb25121c260dd6e00Commit message: "update" > git rev-list --no-walk 7593b720066259d291a31a8eb25121c260dd6e00 # timeout=10[k8sdesktop] $ /bin/sh -xe /tmp/tomcat8-tomcat8-tmp/jenkins6696265847877639661.sh+ cat last_tag+ LAST_TAG=0.0.5+ echo LAST_TAG=0.0.5+ curl -i -s -k https://harbor.oupeng.com/api/repositories/k8sdesktop%2Fk8sdesktop/tags/0.0.5+ awk {print $2}+ head -1+ is_exsist=404+ [ 404 -ne 200 ]+ echo Image not exsist, prepare to build it.Image not exsist, prepare to build it.+ docker build -t harbor.oupeng.com/k8sdesktop/k8sdesktop:0.0.5 .Sending build context to Docker daemon ?60.25MBStep 1/10 : FROM alpine:3.7 ---> 3fd9065eaf02Step 2/10 : MAINTAINER "weiduan" ---> Using cache ---> d7053b014ba5Step 3/10 : RUN rm /etc/apk/repositories ---> Using cache ---> a73bff2a847aStep 4/10 : ADD /sources/repositories /etc/apk/repositories ---> Using cache ---> dfbb18dfbfbdStep 5/10 : RUN apk add --no-cache python3 supervisor nginx python3-dev build-base linux-headers pcre-dev openldap-dev ????&& pip3 install --no-cache-dir uwsgi django django-auth-ldap PyMySQL ---> Using cache ---> 9b318af7da67Step 6/10 : ADD sources/ /data ---> Using cache ---> 600dcbcc0935Step 7/10 : WORKDIR /data ---> Using cache ---> 4b4b2687ac2fStep 8/10 : RUN ln -s /usr/lib/python3.6/site-packages/django/contrib/admin/static/admin /data/k8sdesktop/static/ ????&& rm -f /etc/nginx/nginx.conf /etc/supervisord.conf /usr/bin/python ????&& mkdir -p /etc/nginx/sites-enabled /var/log/uwsgi /run/nginx ????&& ln -s /usr/bin/python3 /usr/bin/python ????&& ln -s /data/supervisord.conf /etc/ ????&& ln -s /data/nginx.conf /etc/nginx/ ????&& ln -s /data/k8sdesktop.conf /etc/nginx/sites-enabled/ ---> Using cache ---> 4f3f962c0635Step 9/10 : EXPOSE 80 ---> Using cache ---> 2604a91e376dStep 10/10 : ENTRYPOINT ["supervisord", "-n"] ---> Using cache ---> dde892e6136aSuccessfully built dde892e6136aSuccessfully tagged harbor.oupeng.com/k8sdesktop/k8sdesktop:0.0.5+ echo Push the image to Harbor.Push the image to Harbor.+ docker push harbor.oupeng.com/k8sdesktop/k8sdesktop:0.0.5The push refers to repository [harbor.oupeng.com/k8sdesktop/k8sdesktop]950915917e6f: Preparing7d26b9ed8228: Preparing82a138967623: Preparingba67b311ae84: Preparing68b0d545b5ec: Preparingcd7100a72410: Preparingcd7100a72410: Waiting68b0d545b5ec: Layer already existscd7100a72410: Layer already exists950915917e6f: Pushedba67b311ae84: Pushed7d26b9ed8228: Pushed82a138967623: Pushed0.0.5: digest: sha256:43efbc46adef6eb7644ebaa7fe2c46dd91cfc6be9926457f276fc001e2c6543e size: 1574[EnvInject] - Injecting environment variables from a build step.[EnvInject] - Injecting as environment variables the properties file path ‘tmp.tags‘[EnvInject] - Variables injected successfully.[SSH] script:LAST_TAG="0.0.5"sed -ri "s@(image\:.*\:).*@\1${LAST_TAG}@" /root/k8sdesktop.yamlkubectl apply -f /root/k8sdesktop.yaml[SSH] executing...deployment "k8sdesktop-deployment" unchangedservice "k8sdesktop-deployment" unchanged[SSH] completed[SSH] exit-status: 0[k8sdesktop] $ /bin/sh -xe /tmp/tomcat8-tomcat8-tmp/jenkins7903574837135018812.sh+ rm -f tmp.tagsFinished: SUCCESS