cdk-pipelines-github icon indicating copy to clipboard operation
cdk-pipelines-github copied to clipboard

Allow per PR/branch deployment

Open braska opened this issue 1 year ago • 1 comments

I want to have ability to do isolated deployments per PR/branch. Every such deployment should be done to the same account.

When I try to describe such PR/branch deployment pipeline with CDK, there is no way to use ${{ github.ref_name }} or ${{ github.event.pull_request.number }} in stage name.

Ideally, I would like to pass stageName while calling .addStage(...) and make it dependent on variables that will be later accessible in Github Workflow during execution.

Is there any trick that would allow me to do per PR/branch deployments?

braska avatar Aug 18 '24 23:08 braska

@braska I think you could accomplish this by patching the generated workflow file to add a top-level environment variable set to your chosen value, and then referencing that variable in your stage definition:

declare const pipeline = gh.GitHubWorkflow;

pipeline.workflowFile.patch(
  gh.JsonPatch.add('/env', { STAGE_ID: '${{ github.ref_name }}' }),
);

const stageId = process.env.STAGE_ID || 'IdUsedInLocalDevEnvironment'
pipeline.addStage(new MyStage(this, stageId, { env: ENV }));

jcuffe avatar Sep 14 '24 21:09 jcuffe