I have a Github Actions workflow and need to set the project environment conditionally so as to not duplicate code in multiple jobs. I have a simple logic to determine the branch then I am trying to set the output so the other jobs can use it. The output is not being passed through to the next job as the Deploy step is showing blank at the beginning of my planning job.
jobs:
set-env:
runs-on: #
outputs:
output_plan: ${{ steps.plan.outputs.env_plan }}
output_apply: ${{ steps.plan.outputs.env_apply }}
steps:
- name: Set environment based on branch
id: env
run: |
if [ ${{ github.ref }} != 'refs/heads/main' ]; then
echo "IS NOT main branch"
echo "env_plan=dev-default-plan" >> "$GITHUB_OUTPUT"
echo "env_apply=dev-default-apply" >> "$GITHUB_OUTPUT"
else
echo "IS main branch"
echo "env_plan=prod-default-plan" >> "$GITHUB_OUTPUT"
echo "env_apply=prod-default-apply" >> "$GITHUB_OUTPUT"
fi
- name: Debug Outputs
run: |
echo "env_plan=${{ steps.env.outputs.env_plan }}"
echo "env_apply=${{ steps.env.outputs.env_apply }}"
terragrunt-plan:
if: github.event_name == 'pull_request'
needs: [set-env]
permissions:
id-token: write
contents: read
name: 'Terragrunt Plan'
environment: ${{ needs.set-env.outputs.output_plan }}
env:
AWSRoleARN: ${{ vars.AWS_ROLE_ARN }}
AWSRegion: ${{ vars.AWS_REGION }}
WHAT_ENV: ${{ needs.set-env.outputs.output_plan }}
runs-on: #
steps:
- name: Deploy
run: echo "Deploying to ${{ env.WHAT_ENV }} environment"
I’ve tried using Env variables to pass it alone but no luck there. Is this possible?
jc99 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.