当前位置: 首页 > 知识库问答 >
问题:

如何让我的项目在Jenkins中只使用一个主git分支自动从开发部署到试运行,并手动部署到生产

徐皓君
2023-03-14

我有大约30个Wordpress网站,所以Jenkins的配置方式是,我为每个网站都有一份工作。开发过程如下(我不知道它是否是最佳的,但这就是我们拥有它的方式):

  1. 由于我们有外包开发人员,因此他们在自己的存储库托管提供商中托管了自己的存储库。每当代码准备好进行 QA 时,他们就会将所有更改提交到 master 分支中的存储库。
  2. 然后,我使用 jenkinsfile 手动运行 Jenkins 作业,如下所示。
  3. 工作流必须是:部署到开发环境,当且仅当以前的部署成功时,部署到暂存。在这里,我们必须停止并让QA人员审查网站是否有任何损坏的链接,错误等。
  4. 如果一切看起来都符合我们的预期,并且没有发现任何错误,则最终部署到生产环境。

注意:有些人建议我只有暂存和生产。我们没有该配置的原因是开发环境无法在线访问,原因是我使用该环境来测试后端配置(例如apache conf等)。

此外,其他一些人建议为每个环境建立一个分支,这在理论上是有意义的,但我认为这将改变我们的外包开发人员将代码提交到存储库的方式,我的意思是,他们将总是必须将代码提交到开发分支,然后合并到阶段分支以部署到阶段,我认为这不是很好。

现在,步骤2-4如下所示:为了给您一个关于该过程的示例,我们将有一个名为“Bearitos”的示例网站和工作:

在名为“bear tos”的工作中,有一个名为“Bearitos to any”的项目

这基本上意味着在该项目内部,我有一个配置了三个阶段的管道:dev,staging和prod,它们使用以下参数进行参数化:DEPLOY_TO:Dev/staging/prod和DEPLOY_DB:Yes/No。因此,根据用户的选择,Jenkins将部署到该特定环境,我认为甚至没有必要拥有这些选项,因为正确的部署流程应该是dev -

在 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,就可以使部署自动化

    当前的Git配置仅配置了一个分支(master),一旦开发人员想要将更改部署到Dev-

    所需的方法是解决提到的问题,并且还有一种自动化的方式来部署和通知开发人员 -

    提前感谢您的帮助!

  • 共有3个答案

    越正阳
    2023-03-14
    pipeline 
    {
            stages 
            {
                   stage('Build')
                   {
                         steps
                         {
                            echo 'building the codes from the git'
                          }
                    }
                    stage('developer-branch-stuff')
                    {
                       when
                       {
                           branch 'developer'
                       }
                       steps
                       {
                          echo 'run this stage - only if the branch = developer branch'
                       }
                    }
            stage('Deliver for development') 
            {
                when 
                {
                    branch 'developer'
                }
                steps 
                {
                    sh 'your_filename_along_with_your_filepath'
                    input message: 'shall we deploy it? (Click "Proceed" to continue)'
                }
            }
            stage('Deploy for production') 
            {
                when
                {
                    branch 'developer'
                }
                steps
                {
                    sh 'your_filename_along_with_your_filepath'
                    input message: 'shall we proceed to production? (Click "Proceed" to continue)'
                }
            }
        }
    }
    
    屠兴旺
    2023-03-14

    什么是实现一个能够部署到所有环境并被参数化的单独部署管道,并实现另一个管道,该管道被调度并触发管道部署到开发(阶段开发),当此工作成功时,然后再次触发管道部署到阶段(阶段qa)。然后可以手动完成对prod的部署。

    https://jenkins.io/doc/pipeline/steps/pipeline-build-step/#-build - 构建作业

    陆展
    2023-03-14
    1. 去掉你的参数,它们是不需要的(会阻止你的自动化
    2. 部署到prod阶段进行手动输入
    pipeline {
        agent any
        stages {
            stage('Deploy to prod') {
                input {
                    message "Should we continue?"
                    ok "Yes, we should."
                }
                steps {
                    echo "Deploying."
                }
            }
        }
    }
    
    pipeline {
        agent any
        stages {
            stage('Example Build') {
                steps {
                    echo 'Hello World'
                }
            }
            stage('Example Deploy') {
                when {
                    branch 'production'
                }
                steps {
                    echo 'Deploying'
                }
            }
        }
    }
    

    至于建议——我会说你需要将你的git流与你的CI/CD流相匹配。给定git分支类型的生命周期是什么?给定阶段的结果是什么?您是否希望执行所有分支的阶段,并且仅针对一个分支< code >部署到生产?

     类似资料:
    • 可以用一句话描述:离线训练、在线识别 完整的深度学习开发周期从逻辑上分为开发和部署两个阶段。 开发阶段(离线训练阶段): 数据专家选择训练数据 算法专家设计模型参数 开发专家对训练过程进行优化和调试,得到满足发布的模型,在Caffe中即为*.caffemodel文件。 部署阶段(在线识别阶段): 由线上负责生产的工程师利用开发团队提供的可发布模型部署到线上生产机器。 Docker简介 Docker

    • 问题内容: 如果您曾经参与过Salesforce项目的部署或为SF项目设置持续集成,那么您应该知道,在部署元数据组件(类,页面等)之后,您通常还需要执行一些手动任务,例如填充自定义设置值或设置配置文件的字段级安全性。有很多这样无聊的动作。 在我当前的项目中,我再次面对它,我正在寻找如何最大程度地自动化它的方法。为了将项目部署到Org,我们使用Jenkins + Git。因此,我希望例如在每次部署后

    • 我配置了一个简单的maven freestyle项目。我能够成功构建该项目,但无法部署到Nexus。我得到这个错误: [错误]无法执行目标org.apache.maven.plugins:maven部署插件:2.8.1:部署(默认部署)项目eqs_utility:部署工件失败:找不到工件com.companyName.eqs:eqs_utility:jar:1.0.1-20190529.19124

    • 问题内容: 我只想运行一个已经使用pom.xml在Maven项目中配置的Web项目。它使用maven tomcat7-maven- plugin部署Web应用程序工件,并且此时一切正常。 现在,我想在tomcat配置中添加自己的server.xml和tomcat-users.xml。我读到我需要添加以下几行。 那很好。它现在可以正常工作,并且使用上面的配置文件部署了tomcat,但是问题是> We

    • 问题内容: 我正在寻找一种自动将Master分支合并到一个或多个开发分支的方法。换句话说,我想在多个项目中每天将团队开发部门与Master同步。 我一直在考虑使用Jenkins,但经验不足。Jenkins是否可以通过添加多个存储库URL,然后在配置中指定“合并到的分支”来提供此功能?与合并问题相比,我不太关心项目的构建。詹金斯将如何应对失败的合并? 詹金斯会很理想,但是如果有其他方法,我很想听听。

    • faygo支持跨平台编译、运行,支持多Web服务、平滑关闭、平滑重启(升级)、热编译、元编程等。 另外,用户可以自定义服务关闭/重启前的回调函数,用于处理一些收尾工作。