【发布时间】:2019-09-22 10:04:34
【问题描述】:
我有大约 30 个 Wordpress 网站,因此配置 Jenkins 的方式是我为每个网站都有一份工作。开发过程如下(我不知道它是否是最优的,但我们就是这样):
- 由于我们有外包开发人员,他们在自己的存储库托管提供商中托管了自己的存储库。每当代码准备好进行 QA 时,他们就会将所有更改提交到我们在主分支中的存储库。
- 然后我使用 jenkinsfile 手动运行 Jenkins 作业,如下所示。
- 工作流必须是:部署到开发环境,当且仅当之前的部署成功,部署到阶段。在这里,我们必须停下来让 QA 人员检查网站是否存在任何断开的链接、错误等。
- 如果一切都和我们预期的一样,并且没有发现错误,那么最后部署到生产环境。
注意:有些人建议我只进行登台和制作。我们没有那个配置的原因是因为dev环境不能在线访问,而之所以这样是因为我使用这个环境来测试后端配置(例如apache conf等)。
另外,其他一些人建议为每个环境建立一个分支,这在理论上是有道理的,但我认为这将改变我们的外包开发人员将代码提交到存储库的方式,我的意思是,他们总是必须将代码提交到 dev 分支,然后合并到 stage 分支以部署到 stage,我认为这不是很好。
现在,步骤 2-4 如下所示: 为了给您举例说明该流程的外观,我们将创建一个名为“Bearitos”的示例网站和工作:
在名为“Bearitos”的工作中,有一个名为“Bearitos to any”的项目
这基本上意味着在该项目内部,我有一个配置有三个阶段的管道:dev、staging 和 prod,它们使用以下参数进行参数化:DEPLOY_TO: Dev/staging/prod 和 DEPLOY_DB: Yes/No。因此,根据用户的选择,Jenkins 将部署到我认为甚至没有必要拥有这些选项的特定环境,因为正确的部署流程应该是 dev -> staging -> prod,不应该有场景将跳过开发或登台,然后在生产旁边部署,所以我认为这应该更好地更新
在 Jenkinsfile 中,我定义了 Dev、Staging 或 Prod 三个阶段,以及是否选择构建数据库的选项,以下是我的 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"""
}
}
}
}
我目前使用这种方法面临的问题是:
我无法弄清楚如何使部署自动化,因为它是 参数化管道,所以我不确定如何使其自动化。这 期望的过程是使部署自动化一次 Jenkins 在 git 存储库上每 X 分钟轮询一次, 自动部署到 Dev > Stage(仅当 Dev 部署成功时)然后停止 在我们对 Staging 进行 QA 后手动部署到 Prod 之前。
当前Git配置只配置了一个分支 (master) 这是开发人员推送更改的地方 想要部署到 Dev -> Stage -> Prod。但我认为 理想的情况是有一个用于开发部署的开发分支然后 stage 分支,用于部署到 Stage 环境,然后 master for 一旦我们将这些 dev 和 staging 分支合并到 主分支。我不确定这是否是最佳的,所以我会 感谢您对此的任何建议或想法。
理想的方法是解决所提到的问题,并且一旦 dev -> staging 部署成功,还可以采用自动化的方式进行部署和通知。除了可以选择手动执行上述工作流程,就像我们现在正在做的那样(这不是那么重要,但如果有这个功能会很好)。
提前感谢您的帮助!
【问题讨论】:
标签: git jenkins jenkins-pipeline