使用 Jenkinsfile 和 AWS Code deploy 部署应用程序

Deploy application using Jenkinsfile and AWS Code deploy

我正在从 Jenkins 1.x 迁移到 Jenkins 2。我想使用 Jenkinsfile 构建和部署应用程序。 我能够构建 gradle 应用程序,但我对使用 Jenkinsfile 通过 AWS Codedeploy 部署应用程序感到困惑。

这是我的 jenkinsfile

node {
   // Mark the code checkout 'stage'....
   stage 'Checkout'
   // Get some code from a GitHub repository
      git branch: 'master', 
       credentialsId: 'xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxx', 
       url: 'https://github.com/somerepo/someapplication.git'

   // Mark the code build 'stage'....
   stage 'Build'
   // Run the gradle build
      sh '/usr/share/gradle/bin/gradle build -x test -q buildZip -Pmule_env=aws-dev -Pmule_config=server'

    stage 'Deploy via Codedeploy'
    //Run using codedeploy agent
}

我搜索了很多教程,但他们使用的是 AWS 代码部署插件。 你能帮我使用 Jenkinsfile 通过 AWS Codedeploy 部署应用程序吗?

谢谢。

或者,您可以使用 AWS CLI 命令进行代码部署。这涉及两个步骤。

步骤 1 - 将部署包推送到 S3 存储桶。请参阅以下命令:

aws --profile {profile_name} deploy push --application-name {code_deploy_application_name} --s3-location s3://<s3_file_path>.zip

其中:

  1. profile_name = AWS 配置文件的名称(如果使用多个账户)
  2. code_deploy_application_name = AWS 代码部署应用程序的名称。
  3. s3_file_path = 部署包 zip 文件的 S3 文件路径。

步骤 2 - 启动代码部署 第二个命令用于触发代码部署。请参阅以下命令:

aws --profile {profile} deploy create-deployment  --application-name {code_deploy_application_name} --deployment-group-name {code_deploy_group_name} --s3-location bucket={s3_bucket_name},bundleType=zip,key={s3_bucket_zip_file_path}

其中:

  1. profile = 您的 AWS 配置文件的名称(如果使用多个账户)
  2. code_deploy_application_name = 与步骤 1 相同。
  3. code_deploy_group_name = 代码部署组的名称。这与您的代码部署应用程序相关联。
  4. s3_bucket_name = 将存储您的部署工件的 S3 存储桶的名称。 (确保您执行代码部署的角色具有对 s3 存储桶的权限。)
  5. s3_bucket_zip_file_path = 与步骤 1 相同。