Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub define GITHUB_REF_NAME as default environment variable #104

Closed
rlespinasse opened this issue Apr 8, 2022 · 7 comments · Fixed by #100
Closed

GitHub define GITHUB_REF_NAME as default environment variable #104

rlespinasse opened this issue Apr 8, 2022 · 7 comments · Fixed by #100
Assignees
Labels
bug Something isn't working released on @v4.x

Comments

@rlespinasse
Copy link
Owner

rlespinasse commented Apr 8, 2022

Describe the bug
GitHub releases a new environment variable: GITHUB_REF_NAME.
Since this action also uses GITHUB_REF_NAME name.
The GitHub version cannot be overwritten by this action, and the export is silently discarded.
The action version doesn't fill this variable the same way GitHub does.
And the current content doesn't match the content produced by this action (before the GitHub release) on pull request event.

On GitHub-side, GITHUB_REF_NAME is GITHUB_REF without the refs prefix.

  • pull-request event GITHUB_REF = refs/pull/42/merge
  • GitHub version of GITHUB_REF_NAME = 42/merge
  • Action version of GITHUB_REF_NAME = <Name of the head branch of PR 42>

To Reproduce
Use this action on a pull request-based workflow.

Expected behavior
GITHUB_REF_NAME contains the branch behind the Pull request.

@rlespinasse rlespinasse added the bug Something isn't working label Apr 8, 2022
@rlespinasse rlespinasse self-assigned this Apr 8, 2022
@rlespinasse
Copy link
Owner Author

Since GitHub default environment variables cannot be overwritten, the GITHUB_REF_NAME (of this action) will never be able to work as intended anymore.

This impacts the v3, and v4 series.

@rlespinasse
Copy link
Owner Author

rlespinasse commented Apr 9, 2022

In order to fix that in the current series, we need to choose a new name for the GITHUB_REF_NAME from this action.

I kind of mix between

  • declaring it as a breaking change (and creating a v5 series)
  • consider it a bug we can't fix and need to live with (and add a new feature to support the need behind the GITHUB_REF_NAME from this action)

In addition, if the current GITHUB_REF_NAME from this action doesn't have existed prior to the GitHub version, this action would have exposed GITHUB_REF_NAME and slug variables like others.

@rlespinasse
Copy link
Owner Author

rlespinasse commented Apr 9, 2022

Another possibility would be to quit the GITHUB_ prefix which is not reserved anymore as stated by this note but the result is the same if GitHub starts using another GITHUB_ environment variable used in this action, we will have possibly the same kind of issue again.
This action uses GITHUB_ prefix in order to stay close to the associated GITHUB_ environment variable when the slugs ones are created

GITHUB_REF
GITHUB_REF_SLUG
GITHUB_REF_SLUG_URL

Instead of (for example)

GITHUB_REF
CI_REF_SLUG
CI_REF_SLUG_URL

But we will still need to choose a new name to avoid confusion between GITHUB_REF_NAME and CI_REF_NAME which ends the same way but have different behaviors in opposition of GITHUB_REF and CI_REF for example.

@rlespinasse
Copy link
Owner Author

After some tests, when you define a new environment variable, the env context and the shell environment variables contain the new variable with the same content.

steps:
  - name: create NEW_ENVIRONMENT_VARIABLE using shell
    run: echo "NEW_ENVIRONMENT_VARIABLE=SOME_VALUE" >>$GITHUB_ENV
    shell: bash

  - name: Echo from 'env.NEW_ENVIRONMENT_VARIABLE' will print 'SOME_VALUE'
    run: echo "${{ env.NEW_ENVIRONMENT_VARIABLE }}"
    shell: bash

  - name: Echo from '$NEW_ENVIRONMENT_VARIABLE' will print 'SOME_VALUE'
    run: echo "${NEW_ENVIRONMENT_VARIABLE}"
    shell: bash

But the default environment variables are not available in the env context, only in the shell environment variables

steps:
  - name: Echo from 'env.GITHUB_REF_NAME' will print nothing
    run: echo "${{ env.GITHUB_REF_NAME }}"
    shell: bash

  - name: Echo from '$GITHUB_REF_NAME' will print the branch or tag name
    run: echo "${GITHUB_REF_NAME}"
    shell: bash

When you used the rlespinasse/github-slug-action@v4, the action basically does something like

steps:
  - name: create GITHUB_REF_NAME using shell
    run: echo "GITHUB_REF_NAME=SOME_VALUE" >>$GITHUB_ENV
    shell: bash

But since a default environment variable cannot be overwritten, the shell environment variables remain unchanged, which is not the case in the env context.

steps:
  - name: create GITHUB_REF_NAME using shell
    run: echo "GITHUB_REF_NAME=SOME_VALUE" >>$GITHUB_ENV
    shell: bash
    
  - name: Echo from 'env.GITHUB_REF_NAME' will print 'SOME_VALUE'
    run: echo "${{ env.GITHUB_REF_NAME }}"
    shell: bash

  - name: Echo from '$GITHUB_REF_NAME' will print the branch or tag name
    run: echo "${GITHUB_REF_NAME}"
    shell: bash

Since the GITHUB_REF_NAME variable GitHub behavior does not map exactly this action behavior, the variable content differs depending on which method you used to access it, but only on Pull Request-related events.

The v3 and the v4 aren't impacted in the same way.

  • v3 is impacted only by the presence of GITHUB_REF_NAME as the default variable. (a.k.a ${{ env.GITHUB_REF_NAME }} != ${GITHUB_REF_NAME}) and expose the slug variables as intended initially (based on ${{ env.GITHUB_REF_NAME }})
  • v4 exposes the slug variables based on ${GITHUB_REF_NAME} and doesn't work as intended due to another bug. Without this bug, v4 would act in the same way as v3.

@rlespinasse
Copy link
Owner Author

To "fix" it, I choose to document this behavior in v3 and v4

  • in the GITHUB_REF_NAME section
  • in the troubleshooting section

And I will also fix the bug in v4 to have the same v3 behavior.

In the near future, env.GITHUB_REF_NAME behavior will move to another named variable

  • v3
    • will not have this new variable
    • will mark env.GITHUB_REF_NAME as deprecated
  • v4
    • will have this new variable
    • will mark env.GITHUB_REF_NAME as deprecated in profit to the new variable
  • the next v5
    • will have this new variable
    • env.GITHUB_REF_NAME will behave in the same way as $GITHUB_REF_NAME (like any other default variables used in this action)

@rlespinasse
Copy link
Owner Author

This could have been detected much sooner with #77 😁

@github-actions
Copy link

🎉 This issue has been resolved in version 4.2.4 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released on @v4.x
Projects
None yet
1 participant