'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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source