Dynamically change branches on AWS CodePipeline

自古美人都是妖i 提交于 2019-12-10 10:47:16

问题


I am looking for a good solution on how to run parametrized (customized) builds in CodePipeline where branch can be changed dynamically?

A little background on the problem: I need an on-demand environment that will be started on certain branch. We already use Bamboo CI server for part of the infrastructure and this is easily achievable with customized build also in Jenkins.

So basically I need a way to trigger a build with branch as a variable on CodePipeline in AWS.


回答1:


Currently CodePipeline does not support branch based builds. Typically CodePipeline works best for running validations and automating the release of your "release" branch.

One option for pre merge validation is to use CodeBuild pull request support to validate pull requests then use CodePipeline to validate the merged code: https://aws.amazon.com/about-aws/whats-new/2017/09/aws-codebuild-now-supports-building-github-pull-requests/




回答2:


For your use case it is best to create a pipeline specifically for each branch, as it sounds like your branch will have a fixed name for a given environment.

This works well where branches represent environments, where CodePipeline struggles is performing Continuous Delivery for more dynamic branches, such as feature branches/pull requests.

For the latter scenario I use CodeBuild to process pull requests, and then publish build artefacts in an S3 archive that I then use to trigger CodePipeline to run integration tests and staging deployments. There are a few traps along the way, but it allows you to leverage some of the more powerful features of CodePipeline (e.g. ability to only have a single stage execution running at a time, which is important for environments with shared resources).




回答3:


We can very well have dynamic branching support with the following approach.

We follow this approach in our organisation and it works very well.

One of the limitations in AWS code-pipeline is that we have to specify branch names while creating the pipeline. We can however overcome this issue using the architecture shown below.

flow diagram

Create a Lambda function which takes the GitHub web-hook data as input, using boto3 integrate it with AWS pipeline(pull the pipeline and update), have an API gateway to make the call to the Lambda function as a rest call and at last create a web-hook to the GitHub repository.

External links:

  • https://aws.amazon.com/quickstart/architecture/git-to-s3-using-webhooks/

  • https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/codepipeline.html




回答4:


I've got the same requirement after doing some R&D got the answer, in the AWS codebuild section didn't have a conditioning base trigger, There is a trigger for scheduling cron base job which only runs after a minute.. So if need to run trigger or dynamically run the codebuild after checking your condition then you have to use aws sdk

I use aws cli and nodejs

For command (cli):

aws codebuild start-build --project-name project1

--environment-variables-override name=variablename,value=variablevalue

for node js

     var AWS = require("aws-sdk");
      var codebuild = new AWS.CodeBuild();

     var params = {
    projectName: machineName, /* required */
    environmentVariablesOverride: [
        {
          name: 'APPID', /* required */
          value: appid, /* required */
        },
         {
          name: 'BUILDID', /* required */
          value: buildidnew, /* required */
        },{
          name: 'REBUILD', /* required */
          value: rebuildid, /* required */
        }

      ],
    };

    codebuild.startBuild(params, function(err, data) {

      if (err) {
       callback(err, null);

      }
      else {

     callback(null, data);
      }  



    });

Remember the variable value should be a string



来源:https://stackoverflow.com/questions/48829167/dynamically-change-branches-on-aws-codepipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!