首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使Jenkins在从bitbucket轮询后自动构建,而不是使用特定参数手动构建作业

如何使Jenkins在从bitbucket轮询后自动构建,而不是使用特定参数手动构建作业
EN

Stack Overflow用户
提问于 2018-12-12 08:10:17
回答 3查看 89关注 0票数 1

我已经准备好了我的Jenkinsfile,我可以使用两个参数手动执行它,这两个参数必须根据我是为dev、staging还是prod构建的。

我现在想要做的是,每次Jenkins发现bitbucket存储库中是否有新内容时,它都会使用特定的参数构建一个新作业(首先使用数据库更新构建dev ),如果成功,则使用staging参数和数据库构建参数进行另一次构建。

我希望为生产部署(或者更确切地说,创建一个新作业)的过程是一项手动任务,直到我检查到所有东西都在staging中看起来很好。

顺便说一句,我的jenkins管道按照预期工作,轮询也按照预期工作,我只需要让这个过程自动化即可。

笔记

我没有使用Bitbucket的webhook,因为我的jenkins服务器运行在一个不允许有外部连接的主机上,当bitbucket向我的jenkins服务器发送信号时,它就会超时。

感谢您提前提出的建议。

我的Jenkinsfile如下:

代码语言:javascript
复制
// Deployment template for CMS-based websites (Drupal or Wordpress)
// 
//
pipeline {
agent any

parameters {
    choice(choices: "Dev\nStaging\nProduction", description: "Choose which environment to push changes to.", name: "DEPLOY_TO")
    choice choices: "No\nYes", description: "Choose whether to deploy the database as well.", name: "DEPLOY_DB"
}

environment {
    SITEID = "ge"
    NOFLAGS = "0"
    DBNAME = "wpress_website"
    DBSERVER = "dbserver"
    DBUSER = "geWordpress"
    DBPASS = "akjh23kas"
    EXCLUDE = "comp_commentmeta,comp_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: "yes"; 
            }
        }
        steps {
            // this stage only required until we make our dev the master DB
            // copy full dev database from appserv1
            // import latest database dump to dev server
            script {
                FILENM = sh(script: 'ls -t goewp-s-dump* | head -1', returnStdout: true)
            }
            //Fixing the problem with the collation existing in the sql dump file, refer to: https://stackoverflow.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. 
            sh "sed -i s/edit.staging.websites.3pth.com/stage.goewpfoods.hcgweb.net/g ${FILENM}"

            sh "mysql -h appserver -u ${env.DBUSER} --password='${env.DBPASS}' ${env.DBNAME}_dev < ${WORKSPACE}/${FILENM}"
        }
    }
    stage("deploy-dev") {
        when {
            environment ignoreCase: true, name: "DEPLOY_TO", value: "dev"
        }
        steps {
            // copy files to appserv2
            // NOTE: if we move the repo to SVN, we should change httpdocs/ to ${env.SITEID}docs/
            sh "sudo chown jenkins:jenkins *"

            //Replace the wp-config.php file with our comp file with our information. 
            sh "/bin/cp httpdocs/wp-config-comp.php httpdocs/wp-config.php"

            // prepare the dev server to receive files by changing the owner
            sh "ssh webadmin@appserv2 \"sudo chown -R webadmin:webadmin /var/opt/httpd/${env.SITEID}docs/\""
            // copy files from control server to dev
            sh "rsync --exclude=Jenkinsfile -rav -e ssh --delete ${WORKSPACE}/httpdocs/ webadmin@appserv2:/var/opt/httpd/${env.SITEID}docs/"
            // fix the owner/permissions on the dev server
            sh "ssh webadmin@appserv2 \"sudo chown -R apache:${env.SITEID}-web /var/opt/httpd/${env.SITEID}docs/\""
            sh "ssh webadmin@appserv2 \"sudo chmod -R g+w /var/opt/httpd/${env.SITEID}docs/\""
            sh "ssh webadmin@appserv2 \"sudo find /var/opt/httpd/${env.SITEID}docs/ -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: "yes"; 
            }
        }
        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}"
                    }
                }
            }
            sh "cd ${WORKSPACE}"
            // pull a backup of the current dev database (may exclude some tables)
            sh "mysqldump -h appserv2 -u ${env.DBUSER} --password='${env.DBPASS}' ${env.DBNAME}_dev ${MYFLAGS} > ${env.DBNAME}_dev.sql"
            // create a backup copy of the current staging database (full backup)
            sh "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
            sh "mysql -h ${env.DBSERVER} -u ${env.DBUSER} --password='${env.DBPASS}' ${env.DBNAME}_stage < ${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@appserv2:/var/opt/httpd/${env.SITEID}docs/ /tmp/${env.SITEID}docs/"

            //Replace the wp-config.php file with our comp file with our information. 
            sh "/bin/cp httpdocs/wp-config-comp.php httpdocs/wp-config.php"

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

            // delete the temporary files on the control server
            sh "rm -Rf /tmp/${env.SITEID}docs/"
            // clear the caches
            sh "wget -O - \"http://www.web.net/incacache.php?api_key=yoiVbjgtL&site_id=088\""
        }
    }
    stage("deploy-db-production") {
        when {
            allOf { 
                environment ignoreCase: true, name: "DEPLOY_TO", value: "production"; 
                environment ignoreCase: true, name: "DEPLOY_DB", value: "yes"; 
            }
        }
        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)
            sh "mysqldump -h ${env.DBSERVER} -u ${env.DBUSER} --password='${env.DBPASS}' ${env.DBNAME}_stage ${MYFLAGS} > ${env.DBNAME}_stage.sql"
            // create a backup copy of the current production database (full backup)
            sh "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
            sh "mysql -h ${env.DBSERVER} -u ${env.DBUSER} --password='${env.DBPASS}' ${env.DBNAME}_prod < ${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@stageserv:/var/opt/httpd/${env.SITEID}docs/ /tmp/${env.SITEID}docs/"

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

            // delete the temporary files on the control server
            sh "rm -Rf /tmp/${env.SITEID}docs/"
             // clear the caches
            sh "wget -O - \"http://www.web.net/incacache.php?api_key=yoiVbjgtL&site_id=088\""
        }
    }
}
EN

回答 3

Stack Overflow用户

发布于 2018-12-12 14:24:01

您可以创建可被视为阶段的函数,然后使用某些参数按您想要的顺序调用这些函数。

例如:

代码语言:javascript
复制
def deployDBDev(param1, param2){
  //some steps
}

def deployDev(diffParam1, diffParam2){
  //some steps
}

//then call it in the sequence you want

deployDBDev(param1, param2)
deployDev(diffParam1, diffParam2)
票数 2
EN

Stack Overflow用户

发布于 2018-12-12 16:43:11

我建议在bitbucket中创建3个分支

  1. dev
  2. Stage master)。

在jenkins端,您可以为每个环境创建3个作业,并为自动触发添加特定分支上的轮询。

您的开发人员将在dev中提交,这将触发dev的构建一旦您发现所有事情都很好,您可以在阶段分支中合并代码,这将触发阶段作业。

最后,当您确认暂存一切正常时,只需将暂存合并到生产分支,这将再次触发生产作业。

票数 1
EN

Stack Overflow用户

发布于 2018-12-12 09:21:26

对于在推送代码或创建拉取请求时触发构建,您可以遵循下面的文章。

https://support.cloudbees.com/hc/en-us/articles/115000051132-How-to-Trigger-Multibranch-Jobs-from-BitBucket-Cloud-

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53734276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档