【发布时间】: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://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.
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.SITEID}docs/
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.SITEID}docs/'
# copy files from control server to dev
rsync --exclude=Jenkinsfile -rav -e ssh --delete ${WORKSPACE}/httpdocs/ webadmin@devserver2:/var/opt/httpd/${env.SITEID}docs/
# fix the owner/permissions on the dev server
ssh webadmin@devserver2 'sudo chown -R apache:${env.SITEID}-web /var/opt/httpd/${env.SITEID}docs/ && sudo chmod -R g+w /var/opt/httpd/${env.SITEID}docs/ && 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: "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.SITEID}docs/ /tmp/${env.SITEID}docs/
#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.SITEID}docs/'
# copy files from control server to staging
rsync --exclude=.svn --exclude=.git -rav -e ssh --delete /tmp/${env.SITEID}docs/ webadmin@stageserver:/var/opt/httpd/${env.SITEID}docs/
# fix the owner/permissions on the staging server
ssh webadmin@stageserver 'sudo chown -R apache:${env.SITEID}-web /var/opt/httpd/${env.SITEID}docs/ && sudo chmod -R g+w /var/opt/httpd/${env.SITEID}docs/ && sudo find /var/opt/httpd/${env.SITEID}docs/ -type d -exec chmod g+s {} \\;'
#delete the temporary files on the control server
rm -Rf /tmp/${env.SITEID}docs/
# 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.SITEID}docs/ /tmp/${env.SITEID}docs/
# prepare the production server to receive files by changing the owner
ssh webadmin@prodserver1 'sudo chown -R webadmin:webadmin /var/opt/httpd/${env.SITEID}docs'
ssh webadmin@prodserver2 'sudo chown -R webadmin:webadmin /var/opt/httpd/${env.SITEID}docs'
# copy files from control server to production
rsync --exclude=.svn --exclude=.git -rav -e ssh --delete /tmp/${env.SITEID}docs/ webadmin@prodserver1:/var/opt/httpd/${env.SITEID}docs/
rsync --exclude=.svn --exclude=.git -rav -e ssh --delete /tmp/${env.SITEID}docs/ webadmin@prodserver2:/var/opt/httpd/${env.SITEID}docs/
# fix the owner/permissions on the production server
ssh webadmin@prodserver1 'sudo chown -R apache:${env.SITEID}-web /var/opt/httpd/${env.SITEID}docs/'
ssh webadmin@prodserver2 'sudo chown -R apache:${env.SITEID}-web /var/opt/httpd/${env.SITEID}docs/'
ssh webadmin@prodserver1 'sudo chmod -R g+w /var/opt/httpd/${env.SITEID}docs/'
ssh webadmin@prodserver2 'sudo chmod -R g+w /var/opt/httpd/${env.SITEID}docs/'
ssh webadmin@prodserver1 'sudo find /var/opt/httpd/${env.SITEID}docs/ -type d -exec chmod g+s {} \\;'
ssh webadmin@prodserver2 'sudo find /var/opt/httpd/${env.SITEID}docs/ -type d -exec chmod g+s {} \\;'
# delete the temporary files on the control server
rm -Rf /tmp/${env.SITEID}docs/
# 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 开始构建开发和暂存阶段,并且只有当我想部署到生产时,我手动完成。然后,我想保留这个,但就像我在上面问的那样,我希望轮询会触发临时分支的部署,当我想手动执行时,我想使用主分支,换句话说,我希望轮询仅在一个分支上工作,而不是所有分支列表。
【问题讨论】: