Github Actions is setting $GITHUB_WORKSPACE incorrectly

Hi.

I am seeing the $GITHUB_WORKSPACE variable being set incorrectly on pull request builds.

I’ve attached the relevant parts of our workflow script below.

What I am seeing is that for e.g. PR #8410, the $GITHUB_WORKSPACE variable has the value /actions-runner/gh-ci-04/_work/myOrg/refs/pull/8401/merge in one job, and /actions-runner/GH-CI-07/_work/myOrg/refs/heads/1.15-release in another, within the same CI run. I’ve also seen it check out several other unrelated PR branches.

This is confusing the actions/checkout@v2 action, and causing it to check out the wrong branches.

name: CI
env:
  JAVA_VERSION: 11.0.7
  NOW: $(($(date +%s%N)/1000000))
on:
  pull_request:
  push:
    branches:
      - master
jobs:
  evaluate-environment:
    name: Look at the environment
    runs-on: [self-hosted, our-machines]
    steps:
      - name: Print github workspace
        run: echo $GITHUB_WORKSPACE
... Some other jobs to build the PR here ...

You can specifically tell checkout what to checkout:

      uses: actions/checkout@v2
      with:
        ref: ${{github.event.pull_request.head.ref}}
        repository: ${{github.event.pull_request.head.repo.full_name}}

@srdo ,

Normally, the value of “GITHUB_WORKSPACE” should like as below:

<runner-installation-directory>/_work/<repo-name>/<repo-name>
OR
<runner-installation-directory>/work/<repo-name>/<repo-name>

Its value also should be equal to that of the property “github.workspace” in the github context.

You can also print the “github.workspace” and the github context to see if the values of the workspace are consistent.

jobs:
  check_workspace:
    name: Check the value of workspace
    runs-on: [self-hosted, our-machines]
    steps:
      - name: Print context
        run: |
          echo "GITHUB_WORKSPACE = $GITHUB_WORKSPACE"
          echo "github.workspace = ${{ github.workspace }}"
          echo "$GITHUB_CONTEXT"
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}

About the checkout action, how did you set the inputs of the action?
If possible, please share the detailed complete configurations of your workflow, so that we can get more information to analyze the root cause.

I’ve stripped down our workflow to demonstrate the issue:

name: CI
env:
  JAVA_VERSION: 11.0.7
  NOW: $(($(date +%s%N)/1000000))
on:
  pull_request:
jobs:
  evaluate-environment:
    name: Look at the environment
    runs-on: [self-hosted, humio, special]
    steps:
      - name: Print github workspace
        run: |
          echo "GITHUB_WORKSPACE = $GITHUB_WORKSPACE"
          echo "github.workspace = ${{ github.workspace }}"
          echo "pr.ref = ${{github.event.pull_request.head.ref}}"
          echo "github.ref = ${{ github.ref }}"
          echo "$GITHUB_CONTEXT"
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
  checkout-code:
    name: Check out the code and see what ref comes out
    runs-on: [self-hosted, humio, special]
    steps:
      - name: Remove existing git hooks
        run: git config --unset core.hooksPath && rm -rf .git/hooks/ || true
      - uses: actions/checkout@v2
        with:
          path: ${{ github.ref }}
          clean: true
          lfs: true
      - name: Checkout LFS # Workaround issue in checkout GH action, see https://github.com/actions/checkout/issues/270
        run: git lfs checkout
      - name: Print workspace path
        run: echo $GITHUB_WORKSPACE && pwd && echo ${{github.ref}}
      - name: current checkout
        run: git log -10

I’ve attached the outputs from the two jobs. Notice that github.ref in the output does not match the branch name set in GITHUB_WORKSPACE.

evaluate-environment: https://pastebin.pl/view/bbb2fbb9
checkout-code: https://pastebin.pl/view/88cb4e68

We have worked around this by downgrading to checkout V1 for now, but I suspect this failure to set the environment correctly could be the reason so many people are having problems with V2 in this GH issue https://github.com/actions/checkout/issues/299

@srdo ,

Looks like, the problem may be caused by some issue on the runner and the checkout action.

I have created and issue ticket (actions/runner#711) to report the problem to the appropriate engineering team for further investigation and evaluation.

You can follow this issue ticket and add your comment to it.