我一直在使用 AWS CDK,我認為這是與 AWS 合作的好方法。最近我遇到了一個我無法解決的問題。查閱了檔案和資源,但沒有人解釋如何在 CDK 中執行此操作。所以我有兩個代碼管道,每個管道要么部署到暫存區,要么部署到生產環境中。現在我想要在代碼部署到生產之前手動批準階段。我將在下面展示我的簡單代碼以供參考:
import * as cdk from '@aws-cdk/core';
import { AppsPluginsCdkStack } from './apps-plugins-services/stack';
import {
CodePipeline,
ShellStep,
CodePipelineSource
} from '@aws-cdk/pipelines';
class ApplicationStage extends cdk.Stage {
constructor(scope: cdk.Construct, id: string) {
super(scope, id);
new CdkStack(this, 'cdkStack');
}
}
class ProductionPipelineStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props: cdk.StackProps) {
super(scope, id, props);
//Create the CDK Production Pipeline
const prodPipeline = new CodePipeline(this, 'ProductionPipeline', {
pipelineName: 'ProdPipeline',
synth: new ShellStep('ProdSynth', {
// Use a connection created using the AWS console to authenticate to GitHub
input: CodePipelineSource.connection(
'fahigm/cdk-repo',
'develop',
{
connectionArn:
'AWS-CONNECTION-ARN' // Created using the AWS console
}
),
commands: ['npm ci', 'npm run build', 'npx cdk synth']
})
});
prodPipeline.addStage(new ApplicationStage(this, 'Production'));
}
}
class StagingPipelineStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props: cdk.StackProps) {
super(scope, id, props);
//Create the CDK Staging Pipeline
const stagePipeline = new CodePipeline(this, 'StagingPipeline', {
pipelineName: 'StagePipeline',
synth: new ShellStep('StageSynth', {
// Use a connection created using the AWS console to authenticate to GitHub
input: CodePipelineSource.connection(
'fahigm/cdk-repo',
'master',
{
connectionArn:
'AWS-CONNECTION-ARN' // Created using the AWS console
}
),
commands: ['npm ci', 'npm run build', 'npx cdk synth']
})
});
stagePipeline.addStage(new ApplicationStage(this, 'Staging'));
}
}
//
const app = new cdk.App();
new ProductionPipelineStack(app, 'ProductionCDKPipeline', {
env: { account: 'ACCOUNT', region: 'REGION' }
});
new StagingPipelineStack(app, 'StagingCDKPipeline', {
env: { account: 'ACCOUNT', region: 'REGION' }
});
app.synth();
現在我不知道從這里去哪里。該檔案僅討論如何從控制臺執行此操作,但我想將其添加到代碼中。真的很感激任何幫助!
uj5u.com熱心網友回復:
CDK 檔案實際上并未討論如何從控制臺執行此操作,而是討論了如何使用 CDK 執行此操作,并提供了示例。這是直接來自檔案的示例:
以下示例顯示了 ShellStep 形式的自動批準和添加到管道中的 ManualApprovalStep 形式的手動批準。兩者都必須通過才能從 PreProd 升級到 Prod 環境:
declare const pipeline: pipelines.CodePipeline;
const preprod = new MyApplicationStage(this, 'PreProd');
const prod = new MyApplicationStage(this, 'Prod');
pipeline.addStage(preprod, {
post: [
new pipelines.ShellStep('Validate Endpoint', {
commands: ['curl -Ssf https://my.webservice.com/'],
}),
],
});
pipeline.addStage(prod, {
pre: [
new pipelines.ManualApprovalStep('PromoteToProd'),
],
});
以下是有關手動批準步驟的檔案:https : //docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_pipelines.ManualApprovalStep.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/371363.html
