每期一个小窍门: jenkins 生产环境流水线 标准构建方案

Posted 李昊轩的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每期一个小窍门: jenkins 生产环境流水线 标准构建方案相关的知识,希望对你有一定的参考价值。

下拉菜单选择镜像 retag为release
具体就是获取build列表
retag为release
同时tag到git release
最终执行 kubectl set image

pipeline 
    agent 
        node 
            label 'prod01'
        
    
    environment 
       dockerServer="tcp://10.0.x.x:2375"
       dockerRegistry="https://xx.xx.com"
       dockerRegistryCredentialsId="harbor-ci-jenkins"
       releaseImage="xx"
       repository="https://xx.git"
       repositoryCredentialsId="gitlab-jenkins-fetchxxx"  // 这里填写jenkins的账号
    
    parameters 
        imageTag(
            name: 'DOCKER_IMAGE',
            description: '请选择待发布的版本',
            image: 'xxx',
            filter: '.*',
            defaultTag: '',
            registry: 'https://xxx',  // 私有仓库地址
            credentialId: 'harbor-ci-jenkins',
            tagOrder: 'NATURAL'
        )
        string(
            name: 'RELEASE_TAG',
            defaultValue: '',
            description: '请输入生产环境对应的版本号'
        )
    

    stages 
        stage('user choice') 
            steps 
                echo "build node : $NODE_NAME"
                echo "jenkins workspace:  $WORKSPACE"
                echo "DOCKER_IMAGE : $DOCKER_IMAGE"
                echo "RELEASE_TAG  : $RELEASE_TAG"
            
        
        stage ("tagging release image") 
            steps
                script 
                    def buildImageTag = "仓库地址xx/$DOCKER_IMAGE"
                    env.releaseImageTag = "$releaseImage:$RELEASE_TAG"
                    echo "build image -----> $buildImageTag"
                    echo "release image  --> $releaseImageTag"
                    docker.withTool('docker') 
                        docker.withServer(dockerServer) 
                            docker.withRegistry(dockerRegistry, dockerRegistryCredentialsId) 
                                def buildDockerImage = docker.image(buildImageTag)
                                buildDockerImage.pull()
                                sh "docker tag $buildImageTag $releaseImageTag"
                                def releaseImage = docker.image(releaseImageTag)
                                releaseImage.push()
                            
                        
                        def label = sh(script:"docker -H $dockerServer inspect $releaseImageTag -f 'json .Config.Labels '",  returnStdout: true)
                        def json = new groovy.json.JsonSlurperClassic().parseText(label)
                        def commit = json."xx.commit"
                        def build = json."xx.build"
                        println(commit)
                        println(build)
                        def appSCM = checkout([
                            $class: 'GitSCM',
                            branches: [[name: commit]],
                            doGenerateSubmoduleConfigurations: false,
                            extensions: [],
                            submoduleCfg: [],
                            userRemoteConfigs:[[credentialsId: repositoryCredentialsId, url: repository]]
                        ])
                        withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: repositoryCredentialsId, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) 
                            def description = "new tag from jenkins pipeline [" + env.JOB_NAME + " ],build " + build
                            sh """
                            git tag -a release-$RELEASE_TAG -m "$description"
                            git push https://$env.GIT_USERNAME:$env.GIT_PASSWORD@仓库地址/xx.git release-$RELEASE_TAG
                            """
                         
                    
                
            
        

        stage ("release to production environment") 
            steps 
                script 
                	// 比如可以执行 kubectl set image -- record 
                    sh "echo $releaseImageTag"
                
            
        

    


以上是关于每期一个小窍门: jenkins 生产环境流水线 标准构建方案的主要内容,如果未能解决你的问题,请参考以下文章

每期一个小窍门: jenkins 生产环境流水线 标准构建方案

每期一个小窍门: filebeat 简单日志文件抓取到es

每期一个小窍门: filebeat 简单日志文件抓取到es

每期一个小窍门: 压测工具 Apache ab 安装与使用

每期一个小窍门: 压测工具 Apache ab 安装与使用

每期一个小窍门(001): 一个简单优雅的redis 工具类 (最简单版本)