Home > OS >  self-hosted runner, Windows, Environment variables, savin paths
self-hosted runner, Windows, Environment variables, savin paths

Time:11-09

I'm trying to calculate paths for a pip install from a internal devpi server. I'm running a self-hosted runner on a Windows server virtual machine. I'm trying to install the latest PIP package to the tool directory by calculating the path as follows;

     - name: pip install xmlcli
        env:
          MYTOOLS: ${{ runner.tool_cache }}\mytools

        run: |
          echo "${{ runner.tool_cache }}"
          $env:MYTOOLS
          pip install --no-cache-dir --upgrade mytools.xmlcli --target=$env:MYTOOLS -i ${{secrets.PIP_INDEX_URL}}
          echo "XMLCLI={$env:MYTOOLS}\mytools\xmlcli" >> $GITHUB_ENV`

      - name: test xmlcli
        run: echo "${{ env.XMLCLI }}"

As you can see; I've had some noob issues trying to output the env variable in windows. I came to the conclusion that under windows; the "run" command is being sent via powershell. Hence the "$env:MYTOOLS" usage.

The problem is the echo "XMLCLI=..." back to the git_env doesn't seem to be working properly as the test xmlcli step returns empty string.

I'm pretty sure I tried several different iterations of the echo command; but, haven't been successful. Is there a video/docs/something that will clearly lays out the usage of "path arithmetic" from within the github action environment?

CodePudding user response:

You need to append to $env:GITHUB_ENV, or you can set the script execution engine on your run action.

When using shell pwsh, then you can use:

"{environment_variable_name}={value}" >> $env:GITHUB_ENV

When using shell powershell

"{environment_variable_name}={value}" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append

But in your case, if you're more familiar with bash, you can force the run action to always use bash

- run: |
    // your stuff here
  shell: bash

See:

CodePudding user response:

@jessehouwing gave me enough information to test a solution to my particular problem. Assuming Windows runners always runs on Powershell, the answer is:

     - name: pip install xmlcli
        env:
          MYTOOLS: ${{ runner.tool_cache }}\mytools

        run: |
          echo "${{ runner.tool_cache }}"
          $env:MYTOOLS
          pip install --no-cache-dir --upgrade mytools.xmlcli --target=$env:MYTOOLS -i ${{secrets.PIP_INDEX_URL}}
          "XMLCLI=$env:MYTOOLS\mytools\xmlcli" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append

      - name: test xmlcli
        run: echo "${{ env.XMLCLI }}"

Incidentally; I was using https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable as a reference; it didn't seem to give the powershell equivalent.

  • Related