|
5 | 5 | branches: [main] |
6 | 6 | pull_request: {} |
7 | 7 |
|
| 8 | +permissions: |
| 9 | + contents: read # for actions/checkout |
| 10 | + |
| 11 | +env: |
| 12 | + test_stacks_directory: test_tf_stacks |
| 13 | + |
8 | 14 | jobs: |
9 | 15 | ci: |
| 16 | + name: Continuous Integration |
10 | 17 | runs-on: ubuntu-latest |
11 | 18 | env: |
12 | | - GITHUB_TEST_ORGANIZATION: 'kfcampbell-terraform-provider' |
| 19 | + GITHUB_TEST_ORGANIZATION: kfcampbell-terraform-provider |
13 | 20 | steps: |
14 | 21 | - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
15 | 22 | - uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0 |
16 | 23 | with: |
17 | | - go-version-file: 'go.mod' |
| 24 | + go-version-file: go.mod |
18 | 25 | cache: true |
19 | 26 | - run: make tools |
20 | 27 | - run: make lint |
21 | 28 | - run: make website-lint |
22 | 29 | - run: make build |
23 | 30 | - run: make test |
| 31 | + |
| 32 | + generate-matrix: |
| 33 | + name: Generate matrix for test stacks |
| 34 | + runs-on: ubuntu-latest |
| 35 | + outputs: |
| 36 | + matrix: ${{ steps.set-matrix.outputs.matrix }} |
| 37 | + has-tests: ${{ steps.set-matrix.outputs.has-tests }} |
| 38 | + steps: |
| 39 | + - name: Checkout |
| 40 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 41 | + |
| 42 | + - name: Generate matrix |
| 43 | + id: set-matrix |
| 44 | + run: | |
| 45 | + if [ -d "${{ env.test_stacks_directory }}" ]; then |
| 46 | + # find all directories and validate their names |
| 47 | + VALID_TESTS=() |
| 48 | + INVALID_TESTS=() |
| 49 | +
|
| 50 | + while IFS= read -r dir; do |
| 51 | + dirname=$(basename "$dir") |
| 52 | + # validate that directory name only contains alphanumeric, hyphens, underscores, and dots |
| 53 | + if [[ "$dirname" =~ ^[a-zA-Z0-9_.-]+$ ]]; then |
| 54 | + VALID_TESTS+=("$dirname") |
| 55 | + else |
| 56 | + INVALID_TESTS+=("$dirname") |
| 57 | + fi |
| 58 | + done < <(find ${{ env.test_stacks_directory }} -mindepth 1 -maxdepth 1 -type d) |
| 59 | +
|
| 60 | + # report invalid directory names if any |
| 61 | + if [ ${#INVALID_TESTS[@]} -gt 0 ]; then |
| 62 | + echo "::warning::Invalid test directory names found (must contain only alphanumeric, hyphens, underscores, and dots):" |
| 63 | + printf ' - %s (will be skipped)\n' "${INVALID_TESTS[@]}" |
| 64 | + fi |
| 65 | +
|
| 66 | + # create JSON array from valid tests |
| 67 | + if [ ${#VALID_TESTS[@]} -gt 0 ]; then |
| 68 | + TESTS=$(printf '%s\n' "${VALID_TESTS[@]}" | jq -R -s -c 'split("\n")[:-1]') |
| 69 | + echo "matrix=${TESTS}" >> $GITHUB_OUTPUT |
| 70 | + echo "has-tests=true" >> $GITHUB_OUTPUT |
| 71 | + echo "Found valid test directories: ${TESTS}" |
| 72 | + else |
| 73 | + echo "matrix=[]" >> $GITHUB_OUTPUT |
| 74 | + echo "has-tests=false" >> $GITHUB_OUTPUT |
| 75 | + echo "No valid test directories found" |
| 76 | + fi |
| 77 | + else |
| 78 | + echo "Test directory ${{ env.test_stacks_directory }} does not exist" |
| 79 | + echo "matrix=[]" >> $GITHUB_OUTPUT |
| 80 | + echo "has-tests=false" >> $GITHUB_OUTPUT |
| 81 | + fi |
| 82 | +
|
| 83 | + tests: |
| 84 | + name: Run tests for Terraform test stacks |
| 85 | + needs: [ci, generate-matrix] |
| 86 | + if: ${{ needs.generate-matrix.outputs.has-tests == 'true' }} # only run if there are some test stacks |
| 87 | + runs-on: ubuntu-latest |
| 88 | + |
| 89 | + strategy: |
| 90 | + fail-fast: false |
| 91 | + matrix: |
| 92 | + tests: ${{ fromJson(needs.generate-matrix.outputs.matrix) }} |
| 93 | + |
| 94 | + steps: |
| 95 | + - name: Checkout |
| 96 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 97 | + |
| 98 | + - name: Setup Go |
| 99 | + uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0 |
| 100 | + with: |
| 101 | + go-version-file: go.mod |
| 102 | + cache: true |
| 103 | + |
| 104 | + - name: Build provider |
| 105 | + run: go build -o terraform-provider-github |
| 106 | + |
| 107 | + - name: Setup dev overrides |
| 108 | + run: | |
| 109 | + ROOT_DIR=$(pwd) |
| 110 | + cat > ~/.terraformrc << EOF |
| 111 | + provider_installation { |
| 112 | + dev_overrides { |
| 113 | + "integrations/github" = "${ROOT_DIR}" |
| 114 | + } |
| 115 | + direct {} |
| 116 | + } |
| 117 | + EOF |
| 118 | +
|
| 119 | + - name: Verify dev overrides setup |
| 120 | + run: cat ~/.terraformrc |
| 121 | + |
| 122 | + - name: Setup Terraform |
| 123 | + uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2 |
| 124 | + with: |
| 125 | + terraform_version: 1.x |
| 126 | + |
| 127 | + - name: Check Terraform version |
| 128 | + run: terraform version |
| 129 | + |
| 130 | + - name: Terraform init |
| 131 | + continue-on-error: true # continue even if init fails |
| 132 | + run: terraform -chdir=./${{ env.test_stacks_directory }}/${{ matrix.tests }} init |
| 133 | + |
| 134 | + - name: Terraform validate |
| 135 | + run: terraform -chdir=./${{ env.test_stacks_directory }}/${{ matrix.tests }} validate |
| 136 | + |
| 137 | + - name: Clean up |
| 138 | + run: rm -f ~/.terraformrc terraform-provider-github |
0 commit comments