I have some GitHub repositories with my project source codes and I build them through CDK Pipelines on AWS. I basically grab the source code, build the docker images and push them to the ECR. I was wondering if I could tag the versions on the code on GitHub through any step or code on the Pipeline, so I can keep track of the builds on the code. I tried looking it up but didn't find anything so I thought maybe I would have more luck here if anyone has done that.
Here a working example of a pipeline initialization with CDKv2 using a CodeCommit repo:
import { Stack, StackProps, Stage, StageProps } from 'aws-cdk-lib';
import { Repository } from 'aws-cdk-lib/aws-codecommit';
import { PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { CodeBuildStep, CodePipeline, CodePipelineSource, ManualApprovalStep } from 'aws-cdk-lib/pipelines';
import { Construct } from 'constructs';
// This will be your infrastructure code
import { InfrastructureStack } from './infrastructure-stack';
export interface PipelineStackProps extends StackProps {
readonly repository: string;
}
export class PipelineStack extends Stack {
constructor(scope: Construct, id: string, props: PipelineStackProps) {
super(scope, id, props);
const pipeline = new CodePipeline(this, 'pipelineID', {
pipelineName: "pipeline-name",
synth: new CodeBuildStep('Synth', {
input: CodePipelineSource.codeCommit(Repository.fromRepositoryArn(this, `repository`, props.repository), 'master'),
commands: [
'npm ci',
'npm run build',
'npx cdk synth',
],
rolePolicyStatements: [
new PolicyStatement({
actions: ['ssm:GetParameter'],
resources: [`*`],
})
]
}),
dockerEnabledForSynth: true
});
pipeline.addStage(new InfrastructureStage(this, 'qa'));
pipeline.addStage(new InfrastructureStage(this, 'prod'), {
pre: [new ManualApprovalStep('PromoteToProd')]
});
}
}
class InfrastructureStage extends Stage {
constructor(scope: Construct, id: string, props?: StageProps) {
super(scope, id, props);
new InfrastructureStack(this, "InfrastructureStack", {
environment: id
})
}
}
If you have a look at the following link you can view that CodePipelineSource can interact with Github using GitHubSourceOptions options and gitHub method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With