5

I have a workflow which creates a new branch with a name that I save as an env variable. the reason is I need the workflow to run on a new clean branch.

1 Job after that I want to check out the branch. the problem is I cant seem to use env variables on the "ref" in order to check it out.

is there a way to do this ? or does github not support this yet.

example code:

  - name: Checkout to branch
    uses: actions/checkout@v2
    with:
      repository: org/repo
      token: ${{secrets.GIT_TOKEN}}
      ref: $NEW_BRANCH_NAME

[edit] ANSWER:

for anyone that is stuck on the same problem, this is how I ended up doing it: first I created a job that just creates and pushed a new branch:

  Create-new-Branch:
runs-on: [ self-hosted ]
outputs:
  branch_name: ${{ steps.create-branch.outputs.BRANCH_NAME}}

steps:
  - name: Checkout master
    uses: actions/checkout@v2
    with:
      repository: org/repo
      token: ${{secrets.GIT_TOKEN}}
      ref: master

  - name: create and push branch
    id: create-branch
    run: |
      export TEST="new-branch-name-`date '+%F'`"
      git checkout -b $TEST origin/master
      git push -u origin $TEST
      echo "::set-output name=BRANCH_NAME::$TEST"

then in the next job when I wanted to use it I use the job output from the job above as the repo name

  Job-Name:
needs: Create-new-Branch
runs-on: [self-hosted]

steps:

  - name: Checkout to branch
    uses: actions/checkout@v2
    with:
      repository: org/repo
      token: ${{secrets.GIT_TOKEN}}
      ref: ${{needs.Create-new-Branch.outputs.branch_name}} # the new branch name
ShaiB
  • 111
  • 8

1 Answers1

1

This question asked the same thing.

What you want to use here are not env variables but outputs.

Job outputs

You can specify a set of outputs that you want to pass to subsequent jobs and then access those values from your needs context.

See documentation:

jobs.<jobs_id>.outputs

A map of outputs for a job

Job outputs are available to all downstream jobs that depend on this job.
For more information on defining job dependencies, see jobs.<job_id>.needs.

Job outputs are strings, and job outputs containing expressions are evaluated on the runner at the end of each job. Outputs containing secrets are redacted on the runner and not sent to GitHub Actions.

To use job outputs in a dependent job, you can use the needs context.
For more information, see "Context and expression syntax for GitHub Actions."

To use job outputs in a dependent job, you can use the needs context.

Example

jobs:
  job1:
    runs-on: ubuntu-latest
    # Map a step output to a job output
    outputs:
      output1: ${{ steps.step1.outputs.test }}
    steps:
    - id: step1
      run: echo "::set-output name=test::hello-world"
   
  job2:
    runs-on: ubuntu-latest
    needs: job1
    steps:
    - run: echo ${{needs.job1.outputs.output1}}
GuiFalourd
  • 15,523
  • 8
  • 44
  • 71