简体   繁体   中英

How Do I Obtain Exposed Variables From CodeBuild In The CDK?

AWS notes there are a variety of Code Build Environment Variables available to use.

Problem: I am trying to access the environment variables, to dynamically name stacks. However, within the CDK code, I am not sure where to write such variables and assign them values.

Current Code:

this.codePipeline = new CodePipeline(this, `${environment}-${appName}-`, {
  pipelineName: `${environment}-${appName}-`,
  selfMutation: true,
  crossAccountKeys: false,
  role: this.codePipelineRole,
  synth: new ShellStep("Deployment", {
    input: CodePipelineSource.codeCommit(this.codeRepository, environment, {
      codeBuildCloneOutput: true,
    }),
    installCommands: ["npm i -g npm@latest"],
    commands: [
      "cd backend",
      "npm ci",
      "npm run build",
      "cdk synth",
    ],
    primaryOutputDirectory: "backend/cdk.out",
  })
});

Environment variable I wish to use CODEBUILD_SOURCE_VERSION .

Note: I have tried writing env: { ["CODEBUILD_SOURCE_VERSION"]: branchName }, in the synth ShellStep props, but obtained the error: No user environment variables can start with CODEBUILD_ .

Those variables like CODEBUILD_SOURCE_VERSION are available in the node environment so you could just directly access them in your CDK code with process.env.CODEBUILD_SOURCE_VERSION

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM