如何在 jenkins 中部署,根据参数选择从特定的 git 分支获取源代码

Posted

技术标签:

【中文标题】如何在 jenkins 中部署,根据参数选择从特定的 git 分支获取源代码【英文标题】:How to deploy in jenkins taking the source code from specific git branches depending on the parameter choice 【发布时间】:2020-03-19 01:25:51 【问题描述】:

我希望能够根据从我的项目的参数中选择的环境进行构建,例如,如果用户在参数中选择暂存选项,我希望从暂存分支构建作业,如果运行 jenkins 作业的用户选择生产,我想从 master 分支构建。顺便说一句,我正在使用管道部署。

我当前的 Jenkinsfile 目前看起来如下:

// Deployment template for CMS-based websites (Drupal or Wordpress)
//
//
pipeline 
    agent any

    parameters 
        choice choices: ['Dev', 'Staging', 'Production'], description: "Choose which environment to push changes to.", name: "DEPLOY_TO"
        booleanParam defaultValue: true, "Choose whether to deploy the database.", name: "DEPLOY_DB"
    

    environment 
         SITEID = "lb"
        NOFLAGS = "0"
        DBNAME = "wpress_myproject"
        DBSERVER = "dbserver"
        DBUSER = "WordpressUser"
        DBPASS = "hiddenpassword"
        EXCLUDE = "domain_commentmeta,domain_comments"  // separate multiple tables with commas
        DEPLOY_TO = "$params.DEPLOY_TO"
        DEPLOY_DB = "$params.DEPLOY_DB"
    

    stages 
        stage("deploy-db-dev") 
            when 
                allOf 
                    environment ignoreCase: true, name: "DEPLOY_TO", value: "dev";
                    environment ignoreCase: true, name: "DEPLOY_DB", value: "true";
                
            
            steps 
                // this stage only required until we make our dev the master DB
                // copy full dev database from bolwebdev1
                // import latest database dump to dev server
                script 
                    FILENM = sh(script: 'ls -t myproject-s-dump* | head -1', returnStdout: true)
                
                //Fixing the problem with the collation existing in the sql dump file, refer to: https://***.com/questions/42385099/1273-unknown-collation-utf8mb4-unicode-520-ci
                //apparently, this is due to a version of mysql issue. Once the problem is fixed from the server side we can then remove the following lines.

                sh """sed -i s/utf8mb4_unicode_520_ci/utf8mb4_unicode_ci/g $FILENM
                # The following line was added because the site is pointing to a staging server which we don't have control over, again, once this is fixed we can delete the following line of code.
                sed -i s/myproject.staging.websites.3pth.com/myproject.example.net/g $FILENM
                mysql -h devserver2 -u $env.DBUSER --password='$env.DBPASS' $env.DBNAME_dev < $WORKSPACE/$FILENM
                rm -f $WORKSPACE/$FILENM"""
        
        
        stage("deploy-dev") 
            when 
                environment ignoreCase: true, name: "DEPLOY_TO", value: "dev"
            
            steps 
                // copy files to devserver2
                // NOTE: if we move the repo to SVN, we should change httpdocs/ to $env.SITEIDdocs/
                sh """sudo chown jenkins:jenkins *

                #Replace the wp-config.php file with our domain file with our information.
        /bin/cp httpdocs/wp-config-domain.php httpdocs/wp-config.php

                # prepare the dev server to receive files by changing the owner
                ssh webadmin@devserver2 'sudo chown -R webadmin:webadmin /var/opt/httpd/$env.SITEIDdocs/'
                # copy files from control server to dev
                rsync --exclude=Jenkinsfile -rav -e ssh --delete $WORKSPACE/httpdocs/ webadmin@devserver2:/var/opt/httpd/$env.SITEIDdocs/
                # fix the owner/permissions on the dev server
        ssh webadmin@devserver2 'sudo chown -R apache:$env.SITEID-web /var/opt/httpd/$env.SITEIDdocs/ && sudo chmod -R g+w /var/opt/httpd/$env.SITEIDdocs/ && sudo find /var/opt/httpd/$env.SITEIDdocs/ -type d -exec chmod g+s  \\;'"""
            
        
        stage("deploy-db-staging") 
            when 
                allOf 
                    environment ignoreCase: true, name: "DEPLOY_TO", value: "staging";
                    environment ignoreCase: true, name: "DEPLOY_DB", value: "true";
                
            
            steps 
                script 
                    def myexcludes = env.EXCLUDE.split(',').toList()
                    MYFLAGS = "-Q -K -c -e --default-character-set=utf8 "
                    if (env.NOFLAGS == "0") 
                        myexcludes.each 
                            MYFLAGS = "$MYFLAGS --ignore-table=$env.DBNAME_dev.$it"
                        
                    
                
                // pull a backup of the current dev database (may exclude some tables)
                sh """mysqldump -h devserver2 -u $env.DBUSER --password='$env.DBPASS' $env.DBNAME_dev $MYFLAGS > $env.DBNAME_dev.sql
        #Searching and replace for the URL to change from the dev sever to the staging server
                sed -i s/myproject.example.net/stage-myproject.example.net/g $env.DBNAME_dev.sql

        # create a backup copy of the current staging database (full backup)
                mysqldump -h $env.DBSERVER -u $env.DBUSER --password='$env.DBPASS' $env.DBNAME_stage > $env.DBNAME_stage_bak.sql
                # upload the dev database dump to the staging database
                mysql -h $env.DBSERVER -u $env.DBUSER --password='$env.DBPASS' $env.DBNAME_stage < $WORKSPACE/$env.DBNAME_dev.sql
                rm -f $WORKSPACE/$env.DBNAME_dev.sql"""
       
        
        stage("deploy-staging") 
            when 
                environment ignoreCase: true, name: "DEPLOY_TO", value: "staging"
            
            steps 
                // copy files from dev to control server
                sh """rsync --exclude=.svn --exclude=.git -rav -e ssh webadmin@devserver2:/var/opt/httpd/$env.SITEIDdocs/ /tmp/$env.SITEIDdocs/

                #Replace the wp-config.php file with our domain file with our information.
            /bin/cp httpdocs/wp-config-domain.php httpdocs/wp-config.php

                #prepare the staging server to receive files by changing the owner
                ssh webadmin@stageserver 'sudo chown -R webadmin:webadmin /var/opt/httpd/$env.SITEIDdocs/'
                # copy files from control server to staging
                rsync --exclude=.svn --exclude=.git -rav -e ssh --delete /tmp/$env.SITEIDdocs/ webadmin@stageserver:/var/opt/httpd/$env.SITEIDdocs/
                # fix the owner/permissions on the staging server
                ssh webadmin@stageserver 'sudo chown -R apache:$env.SITEID-web /var/opt/httpd/$env.SITEIDdocs/ && sudo chmod -R g+w /var/opt/httpd/$env.SITEIDdocs/ && sudo find /var/opt/httpd/$env.SITEIDdocs/ -type d -exec chmod g+s  \\;'

                #delete the temporary files on the control server
                rm -Rf /tmp/$env.SITEIDdocs/
                # clear the Incapsula caches
                if [[ \$( curl -sS -X POST \"http://www.example.net/incapcache.php?api_key=asdaswwGR)feasdsdda&site_id=stage&resource_url=stage-myproject.example.net\" | jq -r .debug_info.id_info) != \"incapsula cache cleared successfuly\" ]]; then exit 255; fi"""
            
        
        stage("deploy-db-production") 
            when 
                allOf 
                    environment ignoreCase: true, name: "DEPLOY_TO", value: "production";
                    environment ignoreCase: true, name: "DEPLOY_DB", value: "true";
                
            
            steps 
                script 
                    def myexcludes = env.EXCLUDE.split(',').toList()
                    MYFLAGS = "-Q -K -c -e --default-character-set=utf8 "
                    if (env.NOFLAGS == "0") 
                        myexcludes.each 
                            MYFLAGS = "$MYFLAGS --ignore-table=$env.DBNAME_stage.$it"
                        
                    
                
                sh """cd $WORKSPACE
                # pull a backup of the current staging database (may exclude some tables)
                mysqldump -h $env.DBSERVER -u $env.DBUSER --password='$env.DBPASS' $env.DBNAME_stage $MYFLAGS > $env.DBNAME_stage.sql
        #Searching and replace for the URL to change from the stage sever to the prod server
                sed -i s/stage-myproject.example.net/www.myproject.com/g $env.DBNAME_stage.sql

                # create a backup copy of the current production database (full backup)
                mysqldump -h $env.DBSERVER -u $env.DBUSER --password='$env.DBPASS' $env.DBNAME_prod > $env.DBNAME_prod_bak.sql
                # upload the staging database dump to the production database
                mysql -h $env.DBSERVER -u $env.DBUSER --password='$env.DBPASS' $env.DBNAME_prod < $WORKSPACE/$env.DBNAME_stage.sql
                rm -f $WORKSPACE/$env.DBNAME_stage.sql"""
        
        
        stage("deploy-production") 
            when 
                environment ignoreCase: true, name: "DEPLOY_TO", value: "production"
            
            steps 
                // copy files from staging to control server
                sh """rsync --exclude=.svn --exclude=.git -rav -e ssh webadmin@stageserver:/var/opt/httpd/$env.SITEIDdocs/ /tmp/$env.SITEIDdocs/

                # prepare the production server to receive files by changing the owner
                ssh webadmin@prodserver1 'sudo chown -R webadmin:webadmin /var/opt/httpd/$env.SITEIDdocs'
                ssh webadmin@prodserver2 'sudo chown -R webadmin:webadmin /var/opt/httpd/$env.SITEIDdocs'
                # copy files from control server to production
                rsync --exclude=.svn --exclude=.git -rav -e ssh --delete /tmp/$env.SITEIDdocs/ webadmin@prodserver1:/var/opt/httpd/$env.SITEIDdocs/
                rsync --exclude=.svn --exclude=.git -rav -e ssh --delete /tmp/$env.SITEIDdocs/ webadmin@prodserver2:/var/opt/httpd/$env.SITEIDdocs/
                # fix the owner/permissions on the production server
                ssh webadmin@prodserver1 'sudo chown -R apache:$env.SITEID-web /var/opt/httpd/$env.SITEIDdocs/'
                ssh webadmin@prodserver2 'sudo chown -R apache:$env.SITEID-web /var/opt/httpd/$env.SITEIDdocs/'
                ssh webadmin@prodserver1 'sudo chmod -R g+w /var/opt/httpd/$env.SITEIDdocs/'
                ssh webadmin@prodserver2 'sudo chmod -R g+w /var/opt/httpd/$env.SITEIDdocs/'
                ssh webadmin@prodserver1 'sudo find /var/opt/httpd/$env.SITEIDdocs/ -type d -exec chmod g+s  \\;'
                ssh webadmin@prodserver2 'sudo find /var/opt/httpd/$env.SITEIDdocs/ -type d -exec chmod g+s  \\;'

                # delete the temporary files on the control server
                rm -Rf /tmp/$env.SITEIDdocs/
                # clear the Incapsula caches
                if [[ \$( curl -sS -X POST \"http://www.example.net/incapcache.php?api_key=asdaswwGR)feasdsdda&site_id=088&resource_url=www.myproject.com\" | jq -r .debug_info.id_info) != \"incapsula cache cleared successfuly\" ]]; then exit 255; fi"""
            
        
    

我知道我可以在名为“要构建的分支”的选项中选择多个分支 在我项目的选项 UI 中,这里的问题变成了,如何让 Jenkins 根据参数选项选择一个分支或另一个?

下一个问题是,因为我启用了轮询,目前它的工作方式如下:如果有新的推送到主分支 jenkins 开始构建开发和暂存阶段,并且只有当我想部署到生产时,我手动完成。然后,我想保留这个,但就像我在上面问的那样,我希望轮询会触发临时分支的部署,当我想手动执行时,我想使用主分支,换句话说,我希望轮询仅在一个分支上工作,而不是所有分支列表。

【问题讨论】:

【参考方案1】:

由于您已经使用 when 指令向阶段添加了适当的运行条件,因此您只需进行如下最小更改即可获得所需的结果:

根据所选环境签出特定分支

    在作业配置页面的来自 SCM 的管道脚本下,选择轻量级结帐。这是为了让 Jenkins 在构建开始时只从存储库中检索 Jenkinsfile。 在Jenkinsfile 中,添加一个功能以按需签出存储库,然后禁用自动签出的默认行为。
    // Checkout repository on demand
    def gitCheckout(branch) 
        checkout(
            [$class: 'GitSCM', branches: [[name: "refs/heads/$branch"]], 
                doGenerateSubmoduleConfigurations: false,
                extensions: [
                    [$class: 'CleanBeforeCheckout'],
                    [$class: 'CheckoutOption', timeout: 30],
                    [$class: 'CloneOption', noTags: true, reference: '', shallow: true, timeout: 30],
                    [$class: 'AuthorInChangelog'],
                    [$class: 'PathRestriction']
                ],
                submoduleCfg: [],
                userRemoteConfigs: [
                    [credentialsId: 'myGitCrdentials', url: 'https://github.com']
                ]
            ]
        )
    

    pipeline 
        agent any
        options 
            skipDefaultCheckout(true)  // Disable automatic checkout
        
        parameters 
            ...
        
        ...
    
    现在,在相应的阶段,调用 gitCheckout() 函数并使用所需的分支进行构建。
    stage("deploy-staging") 
        when 
            environment ignoreCase: true, name: "DEPLOY_TO", value: "staging"
        
        steps 
            gitCheckout('staging')  // Checkout the staging branch
            // Do stuff here
        
    

只轮询一个分支

    由于您只想轮询暂存分支,请将Jenkinsfile 复制或移动到此分支。

    在作业配置页面中,在来自 SCM 的管道脚本 > 要构建的分支下,输入refs/heads/staging

这应该满足你的两个要求。

【讨论】:

感谢您的回复。现在,当你提到 gitCheckout('staging') 时,gitCheckout 是在哪里定义的?【参考方案2】:

我在 Job DSL Plugin 中的所有工作都使用 gitBranch

您可以使用 sn-p 生成器。可用于任何管道作业。左侧“管道语法”>“片段生成器”> git

def gitChekout(String gitBranch) 
   git( branch: gitBranch, 
        credentialsId: 'your-credentials', 
        url: 'git@ssh://path-ti/repo.git')

管道中的某个地方

gitChekout('master')

【讨论】:

以上是关于如何在 jenkins 中部署,根据参数选择从特定的 git 分支获取源代码的主要内容,如果未能解决你的问题,请参考以下文章

如何将 Jenkins 中的特定工件部署到 Nexus 中?

Jenkins - 选择参数 - mysql 数据库查询返回列表

部署流水线搭建小记:DockerJenkinsJava和Couchbase

具有多个部署的单个 Jenkins 作业

如何在 Jenkins 执行期间管理每个构建的特定文件?

技术分享 | Jenkins 如何参数化job ?