Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I replace a globally defined repository variable in GitHub from within a GitHub Action?

I have defined a GitHub repository variable via Settings -> Secrets and variables -> Actions --> Tab Variables -> Repository Variable's presetting with a start/default value. This variable holds the last commit SHA.

How can I replace this variable's value in a GitHub workflow (or job respectively) with the new SHA after a new commit so that the new value is available in the next run?

The reason to do this is that I have a repository containing separated sources for frontend and backend. I am using GitHub Actions to trigger the build, creating an image, put it into a registry, ...

When committing/pushing into the repo I am already able to recognize if changes where made in FE, BE, both or none.

If, for instance, changes where only made in BE, I only want to do the whole stuff for BE but not for the FE and update the repository variable named VERSION_BE with the SHA of the last (BE) commit. For the FE part I don't want to execute all the steps. Instead, I want to use the FE image with the SHA stored in the repository variable VERSION_FE during the last build. When creating/tagging the newly created image I simply want to read out the (updated) repository variables VERSION_BE and VERSION_FE.

like image 250
du-it Avatar asked Mar 12 '26 22:03

du-it


1 Answers

You can use github CLI and run it from workflow.

You should use gh variable set

on: 
    workflow_dispatch

jobs:
  comment:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: |
            gh variable list
            gh variable set TEST_VAR --body "Set from workflow"
            gh variable list
        env:
          GITHUB_TOKEN: ${{ secrets.FULL_PAT }}

enter image description here

This issue

'failed to set variable "VERSION_BE": HTTP 403: Resource not accessible by integration

is related to not enough permissions in token you used. I had the same and switched to Personal Token with full access.

I tried adding permission via permisions: write-all field but I got the same result.

like image 187
Krzysztof Madej Avatar answered Mar 16 '26 07:03

Krzysztof Madej



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!