|
| 1 | +name: Infrastructure as Code Smoke Tests (Pull Requests) |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [master] |
| 6 | + |
| 7 | +jobs: |
| 8 | + check_for_changed_iac_files: |
| 9 | + name: Check for changed IaC files |
| 10 | + runs-on: ubuntu-latest |
| 11 | + outputs: |
| 12 | + is_changed: ${{ steps.check_iac_files_changed.outputs.is_changed }} |
| 13 | + |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v2 |
| 16 | + |
| 17 | + - name: Install jq |
| 18 | + run: | |
| 19 | + sudo apt-get install jq |
| 20 | +
|
| 21 | + - name: Parse CODEOWNERS file |
| 22 | + id: codeowners |
| 23 | + |
| 24 | + with: |
| 25 | + file_match_info: 'true' |
| 26 | + path: ./.github/CODEOWNERS |
| 27 | + |
| 28 | + - name: Get changed files |
| 29 | + id: changed-files |
| 30 | + uses: tj-actions/[email protected] |
| 31 | + |
| 32 | + - name: Get all IaC files |
| 33 | + id: get_all_iac_files |
| 34 | + run: | |
| 35 | + ALL_IAC_FILES=$( |
| 36 | + echo ${{ toJSON(steps.codeowners.outputs.filematches) }} | |
| 37 | + jq '[ |
| 38 | + to_entries[] | |
| 39 | + select( |
| 40 | + .value.owners | |
| 41 | + index("@snyk/group-infrastructure-as-code") |
| 42 | + ) | |
| 43 | + .key |
| 44 | + ]' |
| 45 | + ) |
| 46 | +
|
| 47 | + echo "::set-output name=all_iac_files::$( |
| 48 | + echo $ALL_IAC_FILES |
| 49 | + )" |
| 50 | +
|
| 51 | + - id: check_iac_files_changed |
| 52 | + name: Check for changed files owned by IaC |
| 53 | + run: | |
| 54 | + ALL_IAC_FILES=${{ toJson(steps.get_all_iac_files.outputs.all_iac_files) }} |
| 55 | +
|
| 56 | + CHANGED_FILES=$( |
| 57 | + echo ${{ steps.changed-files.outputs.all_changed_files }} | |
| 58 | + jq -R 'split(" ")' |
| 59 | + ) |
| 60 | +
|
| 61 | + CHANGED_IAC_FILES=$( |
| 62 | + echo $CHANGED_FILES | |
| 63 | + jq --argjson ALL_IAC_FILES "$ALL_IAC_FILES" '[ |
| 64 | + .[] | |
| 65 | + . as $changed_file | |
| 66 | + select( |
| 67 | + $ALL_IAC_FILES | |
| 68 | + index($changed_file) |
| 69 | + ) |
| 70 | + ]' |
| 71 | + ) |
| 72 | +
|
| 73 | + CHANGED_IAC_FILES_COUNT=$( |
| 74 | + echo $CHANGED_IAC_FILES | jq 'length' |
| 75 | + ) |
| 76 | +
|
| 77 | + IS_CHANGED=$( |
| 78 | + echo $CHANGED_IAC_FILES_COUNT | jq '. > 0' |
| 79 | + ) |
| 80 | +
|
| 81 | + $IS_CHANGED && |
| 82 | + echo "Found $CHANGED_IAC_FILES_COUNT changed IaC files: $CHANGED_IAC_FILES"|| |
| 83 | + echo "No changed IaC files found!" |
| 84 | +
|
| 85 | + echo "::set-output name=is_changed::$IS_CHANGED" |
| 86 | +
|
| 87 | + run_iac_smoke_tests: |
| 88 | + name: Run IaC smoke tests |
| 89 | + uses: ./.github/workflows/iac-smoke-tests.yml |
| 90 | + needs: check_for_changed_iac_files |
| 91 | + if: ${{ needs.check_for_changed_iac_files.outputs.is_changed == 'true' }} |
| 92 | + secrets: inherit |
0 commit comments