Wrote an article about Jenkins integrated Docker very early, using the cloudbees Docker Build and Publish plugin plugin. This article is done directly with the shell script, mainly this time there is a need to detect whether the harbor warehouse is already existing to build the image, if it exists to abandon the build, if not exist then build the image and upload to the Harbor warehouse, I am here by Request Harbor API to detect.
The whole process is roughly divided into four steps, drawing a simple diagram:
1. Build a free-style software project on Jenkins.
2, source management use Git.
The account number here is the account number on the Gitlab.
3, build the trigger, I use the Poll SCM Timing detection, that is, every time to detect whether the code is updated.
4. Add the build step.
This process is like this:
- Read the custom tag and assign a value to a variable.
- Detects if there is an image on the Harbor warehouse. If not, build the image and upload it to harbor, or exit if there is one.
- Universal SSH is published to Kubernetes.
What needs to be said separately is that I need to use a custom tag here, I use a plug-in environment Injector Plugin This variable into the Jenkins environment variable, so that it can be called on Jenkins.
In addition, SSH execution commands on the remote machine require authorization to be added. System settings, SSH remote hosts
The account number here is the user and private key (or account number and password) of the remote host that SSH connects to.
5, to here the whole process basically completed, into the project, try to build.
Console output, because it is too long I put the text directly:
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 [email protected]:sa/k8sdesktop . Git # timeout=10fetching upstream changes from [Email protected]:sa/k8sdesktop.git > Git--version # timeout=10u Sing 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=10checking out Revision 7593b720066259d291a31a8eb25121c260dd6e00 ( Refs/remotes/origin/master) > Git config core.sparsecheckout # timeout=10 > Git checkout-f 7593b720066259d291a31a8 Eb25121c260dd6e00commit 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]+ 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 "Wei Duan "---> Using cache---> D7053b014ba5step 3/10:run rm/etc/apk/repositories---> Using cache---> A73BFF2 A847astep 4/10:add/sources/repositories/etc/apk/repositories---> Using cache---> Dfbb18dfbfbdstep 5/10:run a PK 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---> 600dcbcc093 5Step 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---> 4f3f962 C0635step 9/10:expose---> Using cache---> 2604a91e376dstep 10/10:entrypoint ["Supervisord", "-n"]---> U Sing 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.ou Peng.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 SUCCE Ssfully. [SSH] script:last_tag= "0.0.5" Sed-ri "[email protected] (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
Jenkins build Docker image upload to harbor and publish to kubernetes