|
| 1 | +# Attribution for a bunch of this goes to CloudPosse |
| 2 | +# https://github.com/cloudposse/actions/blob/master/.github/workflows/test-command.yml |
| 3 | + |
| 4 | +name: test |
| 5 | +on: |
| 6 | + repository_dispatch: |
| 7 | + types: [test-command] |
| 8 | + |
| 9 | +defaults: |
| 10 | + run: |
| 11 | + # We need -e -o pipefail for consistency with GitHub Actions's default behavior |
| 12 | + shell: bash -e -o pipefail {0} |
| 13 | + |
| 14 | +jobs: |
| 15 | + # Parse the command so we can decide which tests to run. Examples: "/test all", "/test validate", "/test e2e" |
| 16 | + # We can do as many of these as we want to get as granular as we want. |
| 17 | + parse: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + outputs: |
| 20 | + run-ping: ${{ steps.parse.outputs.ping }} |
| 21 | + run-hello: ${{ steps.parse.outputs.hello }} |
| 22 | + run-quote: ${{ steps.parse.outputs.quote }} |
| 23 | + steps: |
| 24 | + - name: Parse Args |
| 25 | + id: parse |
| 26 | + env: |
| 27 | + DEBUG: ${{ toJSON(github.event.client_payload.slash_command) }} |
| 28 | + ARGS_V1: ${{ github.event.client_payload.slash_command.arg1 }} |
| 29 | + ARGS_V2: ${{ github.event.client_payload.slash_command.args.unnamed.all }} |
| 30 | + shell: bash |
| 31 | + run: | |
| 32 | + ARGS="${ARGS_V1}${ARGS_V2}" |
| 33 | + printf "Args are %s\n" "$ARGS" |
| 34 | + printf "\n\nslash_command is %s\n\n" "$DEBUG" |
| 35 | + COMMANDS=(PING HELLO QUOTE) |
| 36 | + if printf "%s" "${ARGS^^}" | grep -qE '\bALL\b'; then |
| 37 | + # "all" explicitly does not include "ping" |
| 38 | + for cmd in "${COMMANDS[@]}"; do |
| 39 | + [[ $cmd == "PING" ]] && ! { printf "%s" "${ARGS^^}" | grep -qE '\bPING\b'; } && continue |
| 40 | + printf -v "$cmd" "true" |
| 41 | + done |
| 42 | + else |
| 43 | + for cmd in "${COMMANDS[@]}"; do |
| 44 | + if printf "%s" "${ARGS^^}" | grep -qE "\b${cmd}\b"; then |
| 45 | + printf -v "$cmd" "true" |
| 46 | + fi |
| 47 | + done |
| 48 | + fi |
| 49 | + for out in "${COMMANDS[@]}"; do |
| 50 | + printf "::set-output name=%s::%s\n" "${out,,}" "${!out:-false}" |
| 51 | + printf "%s=%s\n" "${out,,}" "${!out:-false}" |
| 52 | + done |
| 53 | +
|
| 54 | + # Do a simple ping/pong status update to validate things are working |
| 55 | + ping: |
| 56 | + runs-on: ubuntu-latest |
| 57 | + needs: parse |
| 58 | + if: needs.parse.outputs.run-ping == 'true' |
| 59 | + steps: |
| 60 | + # Update GitHub status for dispatch events |
| 61 | + - name: "Update GitHub Status for this ref" |
| 62 | + uses: "docker://cloudposse/github-status-updater" |
| 63 | + with: |
| 64 | + args: "-action update_state -ref ${{ github.event.client_payload.pull_request.head.sha }} -repo ${{ github.event.client_payload.github.payload.repository.name }}" |
| 65 | + env: |
| 66 | + GITHUB_TOKEN: ${{ secrets.PAT }} |
| 67 | + GITHUB_STATE: success |
| 68 | + GITHUB_CONTEXT: "/test ping" |
| 69 | + GITHUB_DESCRIPTION: "pong" |
| 70 | + GITHUB_TARGET_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 71 | + GITHUB_REF: ${{ github.event.client_payload.pull_request.head.ref }} |
| 72 | + GITHUB_OWNER: ${{ github.event.client_payload.github.payload.repository.owner.login }} |
| 73 | + |
| 74 | + # Do a "hello world" example pipeline run |
| 75 | + hello: |
| 76 | + runs-on: ubuntu-latest |
| 77 | + needs: parse |
| 78 | + if: needs.parse.outputs.run-hello == 'true' |
| 79 | + steps: |
| 80 | + # Update GitHub status for pending pipeline run |
| 81 | + - name: "Update GitHub Status for pending" |
| 82 | + uses: docker://cloudposse/github-status-updater |
| 83 | + with: |
| 84 | + args: "-action update_state -ref ${{ github.event.client_payload.pull_request.head.sha }} -repo ${{ github.event.client_payload.github.payload.repository.name }}" |
| 85 | + env: |
| 86 | + GITHUB_TOKEN: ${{ secrets.PAT }} |
| 87 | + GITHUB_STATE: pending |
| 88 | + GITHUB_CONTEXT: "/test hello" |
| 89 | + GITHUB_DESCRIPTION: "started by @${{ github.event.client_payload.github.actor }}" |
| 90 | + GITHUB_TARGET_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 91 | + GITHUB_REF: ${{ github.event.client_payload.pull_request.head.ref }} |
| 92 | + GITHUB_OWNER: ${{ github.event.client_payload.github.payload.repository.owner.login }} |
| 93 | + |
| 94 | + # Checkout the code from GitHub Pull Request |
| 95 | + - name: "Checkout the code" |
| 96 | + uses: actions/checkout@v2 |
| 97 | + with: |
| 98 | + token: ${{ secrets.PAT }} |
| 99 | + repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }} |
| 100 | + ref: ${{ github.event.client_payload.pull_request.head.ref }} |
| 101 | + |
| 102 | + # Echo "Hello world!" |
| 103 | + - name: "Echo something" |
| 104 | + run: | |
| 105 | + echo "Hello world!" |
| 106 | +
|
| 107 | + # # Throw a failure to test fail conditions |
| 108 | + # - name: "Fail" |
| 109 | + # run: | |
| 110 | + # exit 1 |
| 111 | + |
| 112 | + # # Wait a long time to give the dev time to test cancel conditions |
| 113 | + # - name: "Wait" |
| 114 | + # run: | |
| 115 | + # sleep 600 |
| 116 | + |
| 117 | + # Update GitHub status for failing pipeline run |
| 118 | + - name: "Update GitHub Status for failure" |
| 119 | + if: ${{ failure() }} |
| 120 | + uses: docker://cloudposse/github-status-updater |
| 121 | + with: |
| 122 | + args: "-action update_state -ref ${{ github.event.client_payload.pull_request.head.sha }} -repo ${{ github.event.client_payload.github.payload.repository.name }}" |
| 123 | + env: |
| 124 | + GITHUB_TOKEN: ${{ secrets.PAT }} |
| 125 | + GITHUB_STATE: failure |
| 126 | + GITHUB_CONTEXT: "/test hello" |
| 127 | + GITHUB_DESCRIPTION: "run failed" |
| 128 | + GITHUB_TARGET_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 129 | + GITHUB_REF: ${{ github.event.client_payload.pull_request.head.ref }} |
| 130 | + GITHUB_OWNER: ${{ github.event.client_payload.github.payload.repository.owner.login }} |
| 131 | + |
| 132 | + # Update GitHub status for successful pipeline run |
| 133 | + - name: "Update GitHub Status for success" |
| 134 | + uses: docker://cloudposse/github-status-updater |
| 135 | + with: |
| 136 | + args: "-action update_state -ref ${{ github.event.client_payload.pull_request.head.sha }} -repo ${{ github.event.client_payload.github.payload.repository.name }}" |
| 137 | + env: |
| 138 | + GITHUB_TOKEN: ${{ secrets.PAT }} |
| 139 | + GITHUB_STATE: success |
| 140 | + GITHUB_CONTEXT: "/test hello" |
| 141 | + GITHUB_DESCRIPTION: "run passed" |
| 142 | + GITHUB_TARGET_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 143 | + GITHUB_REF: ${{ github.event.client_payload.pull_request.head.ref }} |
| 144 | + GITHUB_OWNER: ${{ github.event.client_payload.github.payload.repository.owner.login }} |
| 145 | + |
| 146 | + # Update GitHub status for cancelled pipeline run |
| 147 | + - name: "Update GitHub Status for cancelled" |
| 148 | + if: ${{ cancelled() }} |
| 149 | + uses: docker://cloudposse/github-status-updater |
| 150 | + with: |
| 151 | + args: "-action update_state -ref ${{ github.event.client_payload.pull_request.head.sha }} -repo ${{ github.event.client_payload.github.payload.repository.name }}" |
| 152 | + env: |
| 153 | + GITHUB_TOKEN: ${{ secrets.PAT }} |
| 154 | + GITHUB_STATE: error |
| 155 | + GITHUB_CONTEXT: "/test hello" |
| 156 | + GITHUB_DESCRIPTION: "run cancelled" |
| 157 | + GITHUB_TARGET_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 158 | + GITHUB_REF: ${{ github.event.client_payload.pull_request.head.ref }} |
| 159 | + GITHUB_OWNER: ${{ github.event.client_payload.github.payload.repository.owner.login }} |
| 160 | + |
| 161 | + # Add a quote to the status section |
| 162 | + quote: |
| 163 | + runs-on: ubuntu-latest |
| 164 | + needs: parse |
| 165 | + if: needs.parse.outputs.run-quote == 'true' |
| 166 | + steps: |
| 167 | + # Update GitHub status |
| 168 | + - name: "Update GitHub Status for successful pipeline run" |
| 169 | + uses: "docker://cloudposse/github-status-updater" |
| 170 | + with: |
| 171 | + args: "-action update_state -ref ${{ github.event.client_payload.pull_request.head.sha }} -repo ${{ github.event.client_payload.github.payload.repository.name }}" |
| 172 | + env: |
| 173 | + GITHUB_TOKEN: ${{ secrets.PAT }} |
| 174 | + GITHUB_STATE: success |
| 175 | + GITHUB_CONTEXT: "/test quote" |
| 176 | + GITHUB_DESCRIPTION: "Insanity is doing the same thing over and over and expecting different results." |
| 177 | + GITHUB_TARGET_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 178 | + GITHUB_REF: ${{ github.event.client_payload.pull_request.head.ref }} |
| 179 | + GITHUB_OWNER: ${{ github.event.client_payload.github.payload.repository.owner.login }} |
0 commit comments