AWS CodeBuild Buildspec: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 91: Line 91:
     commands:
     commands:
       - if [ -z "${AWS_REGION}" ]; then echo "AWS_REGION variable not set" 1>&2; exit 1; fi
       - if [ -z "${AWS_REGION}" ]; then echo "AWS_REGION variable not set" 1>&2; exit 1; fi
=Organizatorium=
An environment variable exported with the following command in an early phase (install) is visible to subsequent phases:
- export PIPELINE_RUN_ID=1

Revision as of 00:39, 20 March 2019

External

Build Specification Reference for CodeBuild

Internal

Overview

Example

version: 0.2

env:
  variables:
    DEPLOYMENT_STACK_CONFIG_FILE: "deployment-stack-config.json"
    MY_LOCAL_VAR: "some value"

phases:

  install:
    commands:
      - echo "'install' phase started on $(date)"
      #
      # check pre-conditions, like environment variable availability. The environment 'MY_VAR' tested
      # below is supposed to be set by the invoking layer (CodeBuild driven by CodePipeline):
      #
      - if [ -z "${MY_VAR}" ]; then echo "required MY_VAR variable not set" 1>&2; exit 1; fi

  build:
    commands:
    - echo "'build' phase started on $(date)"

  post_build:
    commands:
      - echo "'post_build' phase started on $(date)"
      - echo "{\"Parameters\":{\"MyConfigurationParameterA\":\"blue\", \"MyConfigurationParameterB\":\"red\"}}" > ./${DEPLOYMENT_STACK_CONFIG_FILE}

artifacts:
  files:
    - ${DEPLOYMENT_STACK_CONFIG_FILE}

Structure

version

env

variables

This section sets environment variables that will propagate to the container that performs the build:

env:
  variables:
    MY_VARIABLE: 'some value'

Note that if this build project is part of a CodePipeline pipeline, which is set up with a CloudFormation stack template, additional environment variables can be set at CodeBuild project/CloudFormation stack template level and passed to the build container, as shown here: CodePipeline-Driven CodeBuild Builds. The availability of such environment variable can be tested in the install phase of the build, as shown in the example, above.

phases

install

commands

build

commands

post_build

commands

artifacts

If this build is driven by CodePipeline, the artifacts declared here are placed, as S3 objects, into the CodePipeline Build action's Output Artifact, which is an S3 "folder". For more details about CodePipeline output artifacts, see:

CodePipeline Artifacts

files

Examples

Verifying that an Environment Variable is Set

...
phases:
  install:
    commands:
      - if [ -z "${AWS_REGION}" ]; then echo "AWS_REGION variable not set" 1>&2; exit 1; fi

Organizatorium

An environment variable exported with the following command in an early phase (install) is visible to subsequent phases:

- export PIPELINE_RUN_ID=1