zoukankan      html  css  js  c++  java
  • jenkins构建docker镜像上传到harbor并发布到kubernetes

    很早之前写过一篇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 git@git.oupeng.com:sa/k8sdesktop.git # timeout=10
    Fetching upstream changes from git@git.oupeng.com:sa/k8sdesktop.git
     > git --version # timeout=10
    using 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=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
    
  • 相关阅读:
    必懂的wenpack优化
    必懂的webpack高级配置
    webpack基础知识
    vue-cli
    codemirror使用
    js实现二叉树
    react-生命周期
    window 批量修改或去除文件后缀名
    十分钟搞清字符集和字符编码
    php判断一个值是否在一个数组中,区分大小写-也可以判断是否在键中
  • 原文地址:https://www.cnblogs.com/keithtt/p/8440134.html
Copyright © 2011-2022 走看看