Save a persistent variable in github action

Hi,

I need to save a variable that I have to execute the following execution of the same action.

this variable is an incremental value.

Thanks for your help

Angelo

2 Likes

There is currenly no way to save state across runs from a single action. Also given you can have multiple runs going for the same workflow you would have a race condition on updating and consuming the value without some additional feature work.

is there a counter that I can use in my case?

an incremental id of the action is ok.

Thank you very much

Angelo

(As far as I know) There’s no way to persist variables across jobs when the variables are created within steps.

You’ll have to use artifacts to perist data across jobs.  Here’s a good write up on artifacts along with an example:

And here’s an example I made up that just uses run steps; it dosen’t use actions though.

name: Using run
on: [push]
env:
  START_VALUE: 1
  NEXT_VALUE_FILE: next_value.txt
jobs:
  first:
    runs-on: ubuntu-latest
    steps:
    - name: Initialize the variable in a file with start_value == ${{ env.START_VALUE }}
      run: expr $START_VALUE + 10 | tee ${{ env.NEXT_VALUE_FILE }}
    - name: Upload artifact
      uses: actions/upload-artifact@v1.0.0
      with:
        name: NEXT_VALUE
        path: ${{ env.NEXT_VALUE_FILE }}

  second:
    runs-on: ubuntu-latest
    needs: [first]
    steps:
    - name: Download artifact
      uses: actions/download-artifact@v1.0.0
      with:
        name: NEXT_VALUE
    - name: Show the workspace so you can see where the files are
      run: find .
    - name: Update the variable in a file
      run: |
        variable=$(cat ./NEXT_VALUE/${{ env.NEXT_VALUE_FILE }})
        expr $variable + 10 | tee ${{ env.NEXT_VALUE_FILE }}
    - name: Upload artifact
      uses: actions/upload-artifact@v1.0.0
      with:
        name: NEXT_VALUE
        path: ${{ env.NEXT_VALUE_FILE }}
  third:
    runs-on: ubuntu-latest
    needs: [second]
    steps:
    - name: Download artifact
      uses: actions/download-artifact@v1.0.0
      with:
        name: NEXT_VALUE
    - name: Show the workspace so you can see where the files are
      run: find .
    - name: Update the variable in a file
      run: |
        variable=$(cat ./NEXT_VALUE/${{ env.NEXT_VALUE_FILE }})
        expr $variable + 10 | tee ${{ env.NEXT_VALUE_FILE }}
    - name: Upload artifact
      uses: actions/upload-artifact@v1.0.0
      with:
        name: NEXT_VALUE
        path: ${{ env.NEXT_VALUE_FILE }}

I imagine you could write your workflow to download the file containing the variable to a known place in the filesystem and then write your actions to pick up the variable from that place.  Also, if you need to persist complex data, you could consider using a JSON object or even writting multiple files and then zipping them up and using that as the artifact.

One thing to note, your artifacts (along with uploading and downloading them via automation) are only available in the workflow where the artifacts are created.  So if you need to persist data across workflows, you may have to have the initial action create a release and then download the release from future actions.

Let us know how you fare!! :smiley:

Hi @manang,

Were you able to find a solution for this?

I’ve also had a need for having a persistent variable and created a couple (self-explanatory) actions for this.

This might be a solution to your problem if you don’t want to use artifacts.