|
| 1 | +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-shell-task.md |
| 2 | +name: Check Shell Scripts |
| 3 | + |
| 4 | +# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows |
| 5 | +on: |
| 6 | + create: |
| 7 | + push: |
| 8 | + paths: |
| 9 | + - ".github/workflows/check-shell-task.ya?ml" |
| 10 | + - "Taskfile.ya?ml" |
| 11 | + - "**/.editorconfig" |
| 12 | + - "**.bash" |
| 13 | + - "**.sh" |
| 14 | + pull_request: |
| 15 | + paths: |
| 16 | + - ".github/workflows/check-shell-task.ya?ml" |
| 17 | + - "Taskfile.ya?ml" |
| 18 | + - "**/.editorconfig" |
| 19 | + - "**.bash" |
| 20 | + - "**.sh" |
| 21 | + schedule: |
| 22 | + # Run every Tuesday at 8 AM UTC to catch breakage caused by tool changes. |
| 23 | + - cron: "0 8 * * TUE" |
| 24 | + workflow_dispatch: |
| 25 | + repository_dispatch: |
| 26 | + |
| 27 | +jobs: |
| 28 | + run-determination: |
| 29 | + runs-on: ubuntu-latest |
| 30 | + outputs: |
| 31 | + result: ${{ steps.determination.outputs.result }} |
| 32 | + steps: |
| 33 | + - name: Determine if the rest of the workflow should run |
| 34 | + id: determination |
| 35 | + run: | |
| 36 | + RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x" |
| 37 | + # The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead. |
| 38 | + if [[ |
| 39 | + "${{ github.event_name }}" != "create" || |
| 40 | + "${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX |
| 41 | + ]]; then |
| 42 | + # Run the other jobs. |
| 43 | + RESULT="true" |
| 44 | + else |
| 45 | + # There is no need to run the other jobs. |
| 46 | + RESULT="false" |
| 47 | + fi |
| 48 | +
|
| 49 | + echo "result=$RESULT" >> $GITHUB_OUTPUT |
| 50 | +
|
| 51 | + lint: |
| 52 | + name: ${{ matrix.configuration.name }} |
| 53 | + needs: run-determination |
| 54 | + if: needs.run-determination.outputs.result == 'true' |
| 55 | + runs-on: ubuntu-latest |
| 56 | + |
| 57 | + env: |
| 58 | + # See: https://github.com/koalaman/shellcheck/releases/latest |
| 59 | + SHELLCHECK_RELEASE_ASSET_SUFFIX: .linux.x86_64.tar.xz |
| 60 | + |
| 61 | + strategy: |
| 62 | + fail-fast: false |
| 63 | + |
| 64 | + matrix: |
| 65 | + configuration: |
| 66 | + - name: Generate problem matcher output |
| 67 | + # ShellCheck's "gcc" output format is required for annotated diffs, but inferior for humans reading the log. |
| 68 | + format: gcc |
| 69 | + # The other matrix job is used to set the result, so this job is configured to always pass. |
| 70 | + continue-on-error: true |
| 71 | + - name: ShellCheck |
| 72 | + # ShellCheck's "tty" output format is most suitable for humans reading the log. |
| 73 | + format: tty |
| 74 | + continue-on-error: false |
| 75 | + |
| 76 | + steps: |
| 77 | + - name: Set environment variables |
| 78 | + run: | |
| 79 | + # See: https://docs.github.com/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable |
| 80 | + echo "INSTALL_PATH=${{ runner.temp }}/shellcheck" >> "$GITHUB_ENV" |
| 81 | +
|
| 82 | + - name: Checkout repository |
| 83 | + uses: actions/checkout@v3 |
| 84 | + |
| 85 | + - name: Install Task |
| 86 | + uses: arduino/setup-task@v1 |
| 87 | + with: |
| 88 | + repo-token: ${{ secrets.GITHUB_TOKEN }} |
| 89 | + version: 3.x |
| 90 | + |
| 91 | + - name: Download latest ShellCheck release binary package |
| 92 | + id: download |
| 93 | + |
| 94 | + with: |
| 95 | + repository: koalaman/shellcheck |
| 96 | + excludes: prerelease, draft |
| 97 | + asset: ${{ env.SHELLCHECK_RELEASE_ASSET_SUFFIX }} |
| 98 | + target: ${{ env.INSTALL_PATH }} |
| 99 | + |
| 100 | + - name: Install ShellCheck |
| 101 | + run: | |
| 102 | + cd "${{ env.INSTALL_PATH }}" |
| 103 | + tar --extract --file="${{ steps.download.outputs.name }}" |
| 104 | + EXTRACTION_FOLDER="$(basename "${{ steps.download.outputs.name }}" "${{ env.SHELLCHECK_RELEASE_ASSET_SUFFIX }}")" |
| 105 | + # Add installation to PATH: |
| 106 | + # See: https://docs.github.com/actions/using-workflows/workflow-commands-for-github-actions#adding-a-system-path |
| 107 | + echo "${{ env.INSTALL_PATH }}/$EXTRACTION_FOLDER" >> "$GITHUB_PATH" |
| 108 | +
|
| 109 | + - name: Run ShellCheck |
| 110 | + uses: liskin/gh-problem-matcher-wrap@v2 |
| 111 | + continue-on-error: ${{ matrix.configuration.continue-on-error }} |
| 112 | + with: |
| 113 | + linters: gcc |
| 114 | + run: task --silent shell:check SHELLCHECK_FORMAT=${{ matrix.configuration.format }} |
| 115 | + |
| 116 | + formatting: |
| 117 | + needs: run-determination |
| 118 | + if: needs.run-determination.outputs.result == 'true' |
| 119 | + runs-on: ubuntu-latest |
| 120 | + |
| 121 | + steps: |
| 122 | + - name: Set environment variables |
| 123 | + run: | |
| 124 | + # See: https://docs.github.com/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable |
| 125 | + echo "SHFMT_INSTALL_PATH=${{ runner.temp }}/shfmt" >> "$GITHUB_ENV" |
| 126 | +
|
| 127 | + - name: Checkout repository |
| 128 | + uses: actions/checkout@v3 |
| 129 | + |
| 130 | + - name: Install Task |
| 131 | + uses: arduino/setup-task@v1 |
| 132 | + with: |
| 133 | + repo-token: ${{ secrets.GITHUB_TOKEN }} |
| 134 | + version: 3.x |
| 135 | + |
| 136 | + - name: Download shfmt |
| 137 | + id: download |
| 138 | + |
| 139 | + with: |
| 140 | + repository: mvdan/sh |
| 141 | + excludes: prerelease, draft |
| 142 | + asset: _linux_amd64 |
| 143 | + target: ${{ env.SHFMT_INSTALL_PATH }} |
| 144 | + |
| 145 | + - name: Install shfmt |
| 146 | + run: | |
| 147 | + # Executable permissions of release assets are lost |
| 148 | + chmod +x "${{ env.SHFMT_INSTALL_PATH }}/${{ steps.download.outputs.name }}" |
| 149 | + # Standardize binary name |
| 150 | + mv "${{ env.SHFMT_INSTALL_PATH }}/${{ steps.download.outputs.name }}" "${{ env.SHFMT_INSTALL_PATH }}/shfmt" |
| 151 | + # Add installation to PATH: |
| 152 | + # See: https://docs.github.com/actions/using-workflows/workflow-commands-for-github-actions#adding-a-system-path |
| 153 | + echo "${{ env.SHFMT_INSTALL_PATH }}" >> "$GITHUB_PATH" |
| 154 | +
|
| 155 | + - name: Format shell scripts |
| 156 | + run: task --silent shell:format |
| 157 | + |
| 158 | + - name: Check formatting |
| 159 | + run: git diff --color --exit-code |
| 160 | + |
| 161 | + executable: |
| 162 | + needs: run-determination |
| 163 | + if: needs.run-determination.outputs.result == 'true' |
| 164 | + runs-on: ubuntu-latest |
| 165 | + |
| 166 | + steps: |
| 167 | + - name: Checkout repository |
| 168 | + uses: actions/checkout@v3 |
| 169 | + |
| 170 | + - name: Install Task |
| 171 | + uses: arduino/setup-task@v1 |
| 172 | + with: |
| 173 | + repo-token: ${{ secrets.GITHUB_TOKEN }} |
| 174 | + version: 3.x |
| 175 | + |
| 176 | + - name: Check for non-executable scripts |
| 177 | + run: task --silent shell:check-mode |
0 commit comments