03.pipeline实现项目自动编译docker镜像自动打包

https://jenkins.io/zh/doc/book/pipeline/ 官方教程,可以中文。Jenkinsfile就是把pipeline内容保存到文件,然后提交到svn等版本控制库中。安装blue ocean流水线插件。

 打开后如下图所示:

 修改Jenkins有别的命名空间的权限

# rbac.yaml文件添加内容如下,后提交
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: jenkins-myself
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: admin
subjects:
- kind: ServiceAccount
  name: jenkins2
  namespace: kube-ops

添加svn-get项目,使用SCM方式获取pipeline流水线文件

 其中Jenkinsfile-svn-get文件内容如下,流水线定义步骤为:从svn上拉取nginx源码 --> 安装编译时依赖文件 --> 编译 --> 把编译后的文件打包 --> 调用定义的dockerfile开始构建镜像 --> 推送镜像到仓库 --> 调用kubectl完成镜像部署(这里也可以定义yaml文件来实现)

扫描二维码关注公众号,回复: 9455265 查看本文章
node('haimaxy-jnlp'){
  checkout(
    [
      $class: 'SubversionSCM', 
      additionalCredentials: [], 
      excludedCommitMessages: '', 
      excludedRegions: '', 
      excludedRevprop: '', 
      excludedUsers: '', 
      filterChangelog: false, 
      ignoreDirPropChanges: false, 
      includedRegions: '', 
      locations: [
        [cancelProcessOnExternalsFail: true, 
        credentialsId: '1', 
        depthOption: 'infinity', 
        ignoreExternalsOption: true, 
        local: '.', remote: 'svn://192.168.31.9/nginx']
        ], 
      quietOperation: true, 
      workspaceUpdater: [$class: 'UpdateUpdater']
    ]
  )
  stage('yum'){
       sh 'rpm --rebuilddb && yum --nogpgcheck -y install pcre-devel zlib-devel gcc'
   }
  stage("build nginx"){
      sh './configure --prefix=/usr/local/nginx && make && make install'
  }
  stage('tar'){
       sh 'tar zcvf nginx.tar.gz /usr/local/nginx'
   }
   stage('docker build'){
       sh 'docker build -t 192.168.31.9:5000/jenkins-build-nginx -f dockerfile . '
   }
   stage('docker push'){
    sh 'docker push 192.168.31.9:5000/jenkins-build-nginx'
   }
         stage('kubectl run'){
                sh 'kubectl run nginx --image=192.168.31.9:5000/jenkins-build-nginx --image-pull-policy=Always --namespace=default'
         }
}

dockerfile文件内容如下

FROM 192.168.31.9:5000/openvz-centos7
ADD nginx.tar.gz /
RUN echo 'daemon off;' >> /usr/local/nginx/conf/nginx.conf
CMD /usr/local/nginx/sbin/nginx

最终效果如下:

 

猜你喜欢

转载自www.cnblogs.com/djoker/p/12375900.html