Ở bước này, chúng ta chỉ đang tạo các giai đoạn này - Source, Build và UpdatePipelineand thì thế nó là một đường ống rỗng. Trong phần tiếp theo, chúng ta sẽ thêm các giai đoạn (PublishAssets, Stage1) và hành động phù hợp với nhu cầu của ứng dụng.
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
}
}
Code xác định các thuộc tính cơ bản của đường ống như sau:
#!/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();
Thay thế ACCOUNT với Account ID của bạn và REGION với Region ID, Ví dụ: ap-southeast-1
cho vùng Singapore.
Sao chép account ID của bạn tại đây.
{
...
"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 CodePipeline đọc từ kho lưu trữ GitHub này, chúng ta cũng cần định cấu hình access token cá nhân GitHub mà chúng ta đã tạo trước đó. Token này phải được lưu trữ dưới dạng bí mật văn bản gốc (không phải bí mật JSON) trong AWS Secrets Manager dưới tên chính xác là 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
.