Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Pass in variable into buildspec.yml from CodePipeline

I have an AWS CodePipeline that invokes CodeBuild in the Build Stage.

The question is how do I pass in an environment variable from CodePipeline that can be read in the CodeBuild's buildspec.yml?

I know I can set environment variables in CodeBuild, but I want to use the same CodeBuild project for dev, qa, and prod environments. I don't see how I can pass an environment variable from CodePipeline that makes it all the way to the buildspec.yml

Example buildspec.yml

version: 0.1  phases:      build:     commands:       - npm install       - npm build -- --env ${CURRENT_ENVIRONMENT} 

Where CURRENT_ENVIRONMENT would be the variable I set in the CodePipeline Stage action.

like image 221
user1432403 Avatar asked Jan 17 '17 18:01

user1432403


People also ask

How do you use variables in Buildspec?

In a buildspec file, you define environment variables under the env section. You place your simple environment variables by defining the variables section in env . In the example below, we define an S3_BUCKET variable and assign “my-website-bucket” as its value.

How do you write Yml for Buildspec?

Create this file, name it buildspec. yml , and then save it in the root (top level) directory. Because a build spec declaration must be valid YAML, the spacing in a build spec declaration is important. If the number of spaces in your build spec declaration does not match this one, the build might fail immediately.

Where should the Buildspec Yml file be placed in your code for CodeBuild to work properly?

If you include a buildspec as part of the source code, by default, the buildspec file must be named buildspec. yml and placed in the root of your source directory.


2 Answers

As of today, you can set environment variables for CodeBuild build jobs in your pipeline. This improvement makes it possible to reuse the same build project for multiple actions and simplify deployments to staging and production environments in your pipeline.

name: Build actions:   - name: MyBuildJob     actionTypeId:       category: Build       owner: AWS       provider: CodeBuild       version: '1'     runOrder: 1     configuration:       ProjectName: my-build-project       PrimarySource: MyApplicationSource1       EnvironmentVariables: '[{"name":"CURRENT_ENVIRONMENT","value":"Production","type":"PLAINTEXT"},                               {"name":"UseParamStore","value":"CURRENT_ENVIRONMENT","type":"PARAMETER_STORE"}]' 

https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CodeBuild.html#action-reference-CodeBuild-config

like image 101
Vikyol Avatar answered Sep 16 '22 22:09

Vikyol


You can actually pass environment variables in CodeBuild cloudformation as below:

Build:     Type: AWS::CodeBuild::Project     Properties:       Name:         !Sub Build-${AWS::StackName}       Description: Build your project       Environment:         Type: LINUX_CONTAINER         ComputeType: BUILD_GENERAL1_SMALL         Image: node8         EnvironmentVariables:           - Name: CURRENT_ENVIRONMENT             Type: PLAINTEXT             Value: staging 

And in your buildspec.yml you can reference the environment like this,

version: 0.2  phases:   install:     commands:       - npm install    build:     commands:       - npm build -- --env ${CURRENT_ENVIRONMENT} 
like image 27
Ashwin Aggarwal Avatar answered Sep 19 '22 22:09

Ashwin Aggarwal