Search code examples
github-actions

Why shell for loop doesn't work in github actions


I have an action that is supposed to run a benchmark multiple times, however the step containing the loop ends after first execution of a command inside the loop.

I could reproduce the issue with a simpler case - with plain shell commands below

name: ci-pipeline
on: ["push"]
jobs:
  benchmark:
    steps:
      - name: run a loop
        run: |
          for run in {1..3}; do
            date;sleep 1;
          done

When I run the for loop locally (everything below pipe above), I get:

Fri Jun 24 13:25:57 CEST 2022
Fri Jun 24 13:25:58 CEST 2022
Fri Jun 24 13:25:59 CEST 2022

However, in GitHub Action, when using:

  for run in {1..3}; do
    date;sleep 1;
  done
  shell: sh -e {0}
  env:
    PIP_EXTRA_INDEX_URL: ***url1 ***url2

The return I get is only:

Fri Jun 24 10:15:40 UTC 2022

Solution

  • It may be related to the shell field you used.

    You can check the available values you can use with the shell field on the official documentation

    I made a test on a Github actions workflow using shell: bash:

    jobs:
      job1:
        runs-on: ubuntu-latest
        steps:
          - name: run a loop
            run: |
              for run in {1..3}; do
                date;sleep 1;
              done
            shell: bash
    

    And it returned as expected:

    Fri Jun 24 12:17:42 UTC 2022
    Fri Jun 24 12:17:43 UTC 2022
    Fri Jun 24 12:17:44 UTC 2022
    

    If you want to have a look: