amazon-web-servicesxcodebuildaws-codepipelineaws-codebuild

Pass environment created in codebuild action to downstream action in codepipeline


I'm new to codepipeline & I have multiple actions in a build stage in codepipeline, which 1 buildspec similar to below , and i want to dynamically create a variable here in the python file which i need for a downstream action.

version: 1
phases:
  install:
    runtime-version:
      python: 3.9
    commands:
      - python -m pip install --upgrade pip 
  build:
    commands:
      - python code/get_date.py

get_date.py

import datetime
if __name__ == "__main__":
    current_date = strftime("%Y-%m-%d %H:%M:%S", gmtime())

Then I have a second buildspec file which runs in the second action

version: 1
phases:
  install:
    runtime-version:
      python: 3.9
    commands:
      - python -m pip install --upgrade pip 
  build:
    commands:
      - python code/process_date.py

process_date.py

if __name__ == "__main__":
    # get current date somehow from previous action
    print(current_date)

How do I accomplish something like this? And what is the best recommendation? Would i use output artifacts or variables? And how are they used to pass values like this between stages?

Any help greatly appreciated.


Solution

  • CodeBuild can output variables to be used in other stages or actions in your CodePipeline. These variables are called CodeBuild action output variables and are defined using env/exported-variables in your buildspec.yml.

    General info how to consume and use these variables in a CP are described in Working with variables AWS docs.