3

How can i set output variables when using shell: cmd on Windows? My repo is hosted at Github (not Gitlab).

The following is based on this accepted answer

jobs:
  job1:
    runs-on: my-windows-machine
    # Map a step output to a job output
    outputs:
      output1: ${{ steps.step1.outputs.test }}
      output2: ${{ steps.step2.outputs.test }}
    steps:
    - id: step1
      run: |
        echo '::echo::on'
        echo "::set-output name=test::hello"
      shell: cmd
    - id: step2
      run: |
        echo '::echo::on'
        echo "::set-output name=test::world"
      shell: cmd
  job2:
    runs-on: my-windows-machine
    needs: job1
    steps:
    - run: echo ok: ${{needs.job1.outputs.output1}} ${{needs.job1.outputs.output2}}
      shell: cmd

The echo stmt in job2 only shows ok: string if shell: cmd is used in the steps in job1.

Andrew
  • 147
  • 1
  • 2
  • 8
  • For testing, would an [`::echo::on` help](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#echoing-command-outputs)? Command echoing is disabled by default. – VonC May 27 '22 at 15:21
  • I changed the echo to : echo ok: ${{needs.job1.outputs.output1}} ${{needs.job1.outputs.output2}} The only output i see is "ok:", so there's nothing wrong with the echo stmt – Andrew May 27 '22 at 15:26
  • I agree, but I was not thinking about that echo stmt. I was thinking about the echos done in step 1 or 2. – VonC May 27 '22 at 15:33
  • Thx... i changed the code (see the updated code snippet) to add that extra `echo '::echo::on'` I still only see `ok:` in the echo stmt of job2 – Andrew May 27 '22 at 16:20
  • I would get back from an official example which is supposed to work, like https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-1. And from there, gradually transform it in your own code. – VonC May 27 '22 at 17:18
  • i guess setting output vars just isn't supported with `cmd.exe`. I went ahead and broke up the steps in my workflow to use `shell: cmd` for the things that need to be processed by cmd.exe and created a separate step (using the default shell) just to set the output vars. Thanks for your help. – Andrew May 27 '22 at 21:22

1 Answers1

1

As the OP Andrew concludes in the comments, output vars just is not supported with cmd.exe.

I went ahead and broke up the steps in my workflow to use shell: cmd for the things that need to be processed by cmd.exe and created a separate step (using the default shell) just to set the output vars.


As an alternative, you can see in "Github Actions, how to share a calculated value between job steps?" the $GITHUB_OUTPUT command, which can be used to define outputs for steps. The outputs can then be used in later steps and evaluated in with and env input sections.

You can see it used in "How can I use a GitHub action's output in a workflow?".

Note: the older ::set-output command has now (Oct. 2022) been deprecated.

name: 'Hello World'
runs:
  using: "composite"
  steps:
    - id: random-number-generator
      run: echo "name=random-id::$(echo $RANDOM)" >> $GITHUB_OUTPUT
      shell: bash
  ...


jobs:
  test-job:
    runs-on: self-hosted
    steps:
      - name: Call Hello World 
        id: hello-world
        uses: actions/hello-world-action@v1
      - name: Comment
        if: ${{ github.event_name == 'pull_request' }}
        uses: actions/github-script@v3
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            github.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: 'Output - ${{ steps.hello-world.outputs.random-number }}'
            })
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250