jenkins構建docker鏡像上傳到harbor並發布到kubernetes
阿新 • • 發佈:2018-02-11
info cache mkdir body 執行 pip ble 一段 一次
很早之前寫過一篇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=10 Fetching changes from the remote Git repository > git config remote.origin.url [email protected]:sa/k8sdesktop.git # timeout=10 Fetching upstream changes from [email protected]:sa/k8sdesktop.git > git --version # timeout=10 using GIT_ASKPASS to set credentials > git fetch --tags --progress [email protected]: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=10 Checking out Revision 7593b720066259d291a31a8eb25121c260dd6e00 (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f 7593b720066259d291a31a8eb25121c260dd6e00 Commit 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.25MB Step 1/10 : FROM alpine:3.7 ---> 3fd9065eaf02 Step 2/10 : MAINTAINER "weiduan" ---> Using cache ---> d7053b014ba5 Step 3/10 : RUN rm /etc/apk/repositories ---> Using cache ---> a73bff2a847a Step 4/10 : ADD /sources/repositories /etc/apk/repositories ---> Using cache ---> dfbb18dfbfbd Step 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 ---> 9b318af7da67 Step 6/10 : ADD sources/ /data ---> Using cache ---> 600dcbcc0935 Step 7/10 : WORKDIR /data ---> Using cache ---> 4b4b2687ac2f Step 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 ---> 4f3f962c0635 Step 9/10 : EXPOSE 80 ---> Using cache ---> 2604a91e376d Step 10/10 : ENTRYPOINT ["supervisord", "-n"] ---> Using cache ---> dde892e6136a Successfully built dde892e6136a Successfully 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.5 The push refers to repository [harbor.oupeng.com/k8sdesktop/k8sdesktop] 950915917e6f: Preparing 7d26b9ed8228: Preparing 82a138967623: Preparing ba67b311ae84: Preparing 68b0d545b5ec: Preparing cd7100a72410: Preparing cd7100a72410: Waiting 68b0d545b5ec: Layer already exists cd7100a72410: Layer already exists 950915917e6f: Pushed ba67b311ae84: Pushed 7d26b9ed8228: Pushed 82a138967623: Pushed 0.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.yaml kubectl apply -f /root/k8sdesktop.yaml [SSH] executing... deployment "k8sdesktop-deployment" unchanged service "k8sdesktop-deployment" unchanged [SSH] completed [SSH] exit-status: 0 [k8sdesktop] $ /bin/sh -xe /tmp/tomcat8-tomcat8-tmp/jenkins7903574837135018812.sh + rm -f tmp.tags Finished: SUCCESS
jenkins構建docker鏡像上傳到harbor並發布到kubernetes