psteinroe
psteinroe

Reputation: 533

GitHub Actions: Can't set env variable

I am trying to set a simple environment variable based on the branch name, but I cannot get it to work. Based on the logs, the env var seems to be set correctly, but it is still empty afterwards:

TF_VAR_project_name=staging >> /home/runner/work/_temp/_runner_file_commands/set_env_51dc1c8d-60b9-47bd-b91e-f315cd9349a7

--

name: "Terraform"

on:
  push:
    branches:
      - main
      - staging
  pull_request:

jobs:
  terraform:
    name: "Terraform"
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Get branch names
        id: branch-name
        uses: tj-actions/branch-names@v4

      - name: For PR, get base ref branch name
        if: github.event_name == 'pull_request'
        run: |
          echo "TF_VAR_project_name=${{ steps.branch-name.outputs.base_ref_branch }} >> $GITHUB_ENV"

      - name: For push, get current branch name
        if: github.event_name == 'push'
        run: |
          echo "TF_VAR_project_name=${{ steps.branch-name.outputs.current_branch }} >> $GITHUB_ENV"

      - name: Test1
        run: |
          echo "$TF_VAR_project_name"

      - name: Set is prod based on the branch
        run: |
          echo "TF_VAR_is_prod=$(( $TF_VAR_project_name == prod ? true : false ))" >> $GITHUB_ENV

   

Complete Log: enter image description here

Thanks!

Upvotes: 1

Views: 5309

Answers (1)

GuiFalourd
GuiFalourd

Reputation: 22890

There are two syntax problems with your workflow:

FIRST:

The syntax used to set variables has " incorrectly used:

    run: |
      echo "TF_VAR_project_name=${{ steps.branch-name.outputs.base_ref_branch }} >> $GITHUB_ENV"

Should be:

    run: |
      echo "TF_VAR_project_name=${{ steps.branch-name.outputs.base_ref_branch }}" >> $GITHUB_ENV

Before the >> $GITHUB_ENV (documentation reference)

SECOND:

The syntax used at the last step uses $(( ... )) instead of ${{ .. }}:

  - name: Set is prod based on the branch
    run: |
      echo "TF_VAR_is_prod=$(( $TF_VAR_project_name == prod ? true : false ))" >> $GITHUB_ENV

Should be:

  - name: Set is prod based on the branch
    run: |
      echo "TF_VAR_is_prod=${{ $TF_VAR_project_name == prod ? true : false }}" >> $GITHUB_ENV

As you used on other steps.

Upvotes: 2

Related Questions