How to set workflow env variables depending on branch

后端 未结 2 821
天涯浪人
天涯浪人 2021-01-06 02:31

I currently have two GitHub actions workflow files that are pretty much identical, only one is configured to react to push/pull_requests on branch master the ot

2条回答
  •  生来不讨喜
    2021-01-06 03:05

    You can drop the global env statement, combine the event triggers to

    on:
      push:
        branches:
          - master
          - production
      pull_request:
        branches:
          - master
          - production
    

    and then add a first step that checks which branch the workflow is running on and set the environment there:

          - name: Set environment for branch
            run: |
              if [[ $GITHUB_REF == 'refs/heads/master' ]]; then
                  echo "GLCOUD_PROJECT=my-project-stg" >> "$GITHUB_ENV"
                  echo "VERCEL_TARGET=staging" >> "$GITHUB_ENV"
              else
                  echo "GLCOUD_PROJECT=my-project-prd" >> "$GITHUB_ENV"
                  echo "VERCEL_TARGET=production" >> "$GITHUB_ENV"
              fi
    

提交回复
热议问题