For this step, we are only creating these predefined stages - Source, Build and UpdatePipelineand hence it is an empty pipeline. In the next section, we will add stages (PublishAssets, Stage1) and actions to it to suit the needs of our application.
import { CodePipeline, CodePipelineSource, ShellStep } from 'aws-cdk-lib/pipelines';
import { Construct } from 'constructs';
import { Stack, StackProps } from 'aws-cdk-lib';
/**
* The stack that defines the application pipeline
*/
export class CdkPipelineStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const pipeline = new CodePipeline(this, 'Pipeline', {
// The pipeline name
pipelineName: 'MyServicePipeline',
// How it will be built and synthesized
synth: new ShellStep('Synth', {
// Where the source can be found
input: CodePipelineSource.gitHub('OWNER/REPO', 'main'),
// Build and run cdk synth
commands: [
'npm ci',
'npm run build',
'npx cdk synth'
],
}),
});
// This is where we add the application stages
}
}
The code defines the following basic properties of the pipeline:
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { CdkPipelineStack } from '../lib/cdk-pipeline-stack';
const app = new cdk.App();
new CdkPipelineStack(app, 'CdkPipelineStack', {
env: { account: 'ACCOUNT', region: 'REGION' },
});
app.synth();
Replace ACCOUNT with your Account ID and REGION with Region ID, ex: ap-southeast-1
for region Singapore.
Copy your account ID here.
{
...
"context": {
"@aws-cdk/core:newStyleStackSynthesis": true
}
}
For AWS CodePipeline to read from this GitHub repo, we also need to configure the GitHub personal access token we created earlier. This token should be stored as a plaintext secret (not a JSON secret) in AWS Secrets Manager under the exact name github-token.
aws secretsmanager create-secret --name github-token --description "Github access token for cdk" --secret-string GITHUB_ACCESS_TOKEN --region REGION
ap-southeast-1
.