4

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.

6
  • For testing, would an ::echo::on help? Command echoing is disabled by default. Commented May 27, 2022 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 Commented May 27, 2022 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. Commented May 27, 2022 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 Commented May 27, 2022 at 16:20
  • I would get back from an official example which is supposed to work, like docs.github.com/en/actions/using-workflows/…. And from there, gradually transform it in your own code. Commented May 27, 2022 at 17:18

1 Answer 1

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 }}'
            })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.