0

I set my GITHUB_ENV as uuid.

  - name: Generate UUID
    run: uuid=$(uuidgen)

  - name: Set ENV
    run: echo "UUID_stable=$uuid>> $GITHUB_ENV"

I want to upload a tar file by runner1, and name this file as GITHUB_ENV.

tar -zcvf {}.tar'.format(GITHUB_ENV)

The runner2 is used to download this file, I use the command

wget http://xxx.xx.xx.xxx:xxxx/{}.tar'.format(GITHUB_ENV)

However, the file I uploaded was named 4978d4.tar, and what I downloaded was f88d1c.tar

How can I fix it?

I want the file name which I DOWNLOAD is the same as which I UPLOAD. thank you!

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
yolic
  • 3
  • 2
  • Can you please include the rest of your workflow as well? What do you mean by `runner1` and `runner2` here? Each step runs in a different shell. Generating UUID in one step and setting it in another won't work because `$uuid` is not available in the shell where you set it. It'll be accessible via the `env` context in the subsequent steps i.e. via `env.uuid`. – Azeem Mar 02 '23 at 08:28
  • thanks for your reply! runner1 is used to upload file, runner 2 is used to download file. Both of them will work in github workflow(runner2 needs runner1), can you please tell me more information about env.uuid? thanks! – yolic Mar 02 '23 at 08:44
  • Right. Please include the relevant parts of your workflow, especially where you upload and download that file along with `format()` function. See [Setting an environment variable](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable) and [`env` context](https://docs.github.com/en/actions/learn-github-actions/contexts#env-context) for more details. Thanks! – Azeem Mar 02 '23 at 08:49
  • my upload and download code is written in my python file , I use a .sh file to run them. and generate uuid in github_env is written in ci.yml. Thank you ! – yolic Mar 02 '23 at 08:54
  • Right. Then, shouldn't you be using [`os.environ`](https://docs.python.org/3/library/os.html#os.environ) to access the environment variables? – Azeem Mar 02 '23 at 09:00
  • thanks for your reply. I have used os.environ in my python file. – yolic Mar 11 '23 at 03:18
  • Right. Please include an [MCVE](https://meta.stackoverflow.com/questions/366988/what-does-mcve-mean) in your question that can be tested in isolation. Thanks! – Azeem Mar 11 '23 at 04:59

1 Answers1

0

The environment variables in GitHub are reset each job, so the value is lost when the workflow jumps from runner1 (job1) to runner2 (job2).

But you can use output variables in GitHub actions to pass values from one job to the next:

jobs:
  runner1:
    outputs:
      UUID_stable: ${{ steps.generate_id.outputs.UUID_stable }}

    steps:
    - run: |
        uuid=$(uuidgen)
        echo "UUID_stable=$uuid" >> $GITHUB_OUTPUT
      id: generate_id
    - run: |
        echo $UUID_stable
      env:
        UUID_stable: ${{ steps.generate_id.outputs.UUID_stable }}

  runner2:
    needs: runner1
    steps:
    - run: |
        'tar -zcvf {}.tar'.format($UUID_stable)
      env:
        UUID_stable: ${{ needs.runner1.outputs.UUID_stable }}

Note: It's better from a security practice to pass the variables in to your run steps using the env: section. That way you won't accidentally cause script insertion when the script gets evaluated.

See:

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • thanks for your reply! I would like to ask if UUID_stable can be used in some python files in runner1/runner2. I tried to use os.environ['UUID_stable'], but I failed. – yolic Mar 11 '23 at 03:17
  • As long as you pass the value to the environment if the step executing the Python file. As i did in the 2 run steps at the end of each job. – jessehouwing Mar 11 '23 at 07:57
  • Otherwisez update your Q with more details. – jessehouwing Mar 11 '23 at 07:57
  • Or are you trying to access the environment variables from your python environment *after* the workflow has finished? Because that won't work. You'll either have to update system level or user level environment variables through te OS or you'll need to persist the value in a env or config file during the workflow so your app can read the at a later point in time. – jessehouwing Mar 11 '23 at 09:04