Refactor: Move packer validator to non-voting job
[releng/builder.git] / .github / workflows / gerrit-packer-verify.yaml
1 ---
2 name: Packer Verify
3
4 # yamllint disable-line rule:truthy
5 on:
6   workflow_dispatch:
7     inputs:
8       GERRIT_BRANCH:
9         description: "Branch that change is against"
10         required: true
11         type: string
12         default: master
13       GERRIT_CHANGE_ID:
14         description: "The ID for the change"
15         required: true
16         type: string
17       GERRIT_CHANGE_NUMBER:
18         description: "The Gerrit number"
19         required: true
20         type: string
21       GERRIT_CHANGE_URL:
22         description: "URL to the change"
23         required: true
24         type: string
25       GERRIT_EVENT_TYPE:
26         description: "Type of Gerrit event"
27         required: true
28         type: string
29       GERRIT_PATCHSET_NUMBER:
30         description: "The patch number for the change"
31         required: true
32         type: string
33       GERRIT_PATCHSET_REVISION:
34         description: "The revision sha"
35         required: true
36         type: string
37       GERRIT_PROJECT:
38         description: "Project in Gerrit"
39         required: true
40         type: string
41         default: releng/builder
42       GERRIT_REFSPEC:
43         description: "Gerrit refspec of change"
44         required: true
45         type: string
46         default: master
47
48 env:
49   OS_CLOUD: "vex"
50   PACKER_VERSION: "1.9.1"
51
52 concurrency:
53   group: ${{ github.event.inputs.GERRIT_CHANGE_ID || github.run_id }}
54   cancel-in-progress: true
55
56 jobs:
57   prepare:
58     runs-on: ubuntu-latest
59     steps:
60       - name: Clear votes
61         uses: lfit/gerrit-review-action@v0.4
62         with:
63           host: ${{ vars.GERRIT_SERVER }}
64           username: ${{ vars.GERRIT_SSH_USER }}
65           key: ${{ secrets.GERRIT_SSH_PRIVKEY }}
66           known_hosts: ${{ vars.GERRIT_KNOWN_HOSTS }}
67           gerrit-change-number: ${{ inputs.GERRIT_CHANGE_NUMBER }}
68           gerrit-patchset-number: ${{ inputs.GERRIT_PATCHSET_NUMBER }}
69           vote-type: clear
70           comment-only: true
71       - name: Allow replication
72         run: sleep 10s
73
74   packer-validator:
75     needs: prepare
76     runs-on: ubuntu-latest
77     steps:
78       - uses: lfit/checkout-gerrit-change-action@v0.4
79         with:
80           gerrit-refspec: ${{ inputs.GERRIT_REFSPEC }}
81           delay: "0s"
82       - name: Clone git submodules
83         run: git submodule update --init
84       - name: Setup packer
85         uses: hashicorp/setup-packer@main
86         id: setup
87         with:
88           version: ${{ env.PACKER_VERSION }}
89       - name: Create cloud-env file required for packer
90         id: create-cloud-env-file
91         shell: bash
92         run: |
93           echo "${{ secrets.CLOUDS_ENV_B64 }}" | base64 --decode > "${GITHUB_WORKSPACE}/cloud-env.pkrvars.hcl"
94       - name: Create cloud.yaml file for openstack client
95         id: create-cloud-yaml-file
96         shell: bash
97         run: |
98           mkdir -p "$HOME/.config/openstack"
99           echo "${{ secrets.CLOUDS_YAML_B64 }}" | base64 --decode > "$HOME/.config/openstack/clouds.yaml"
100       - uses: actions/setup-python@v4
101         id: setup-python
102         with:
103           python-version: "3.11"
104       - name: Install openstack deps
105         id: install-openstack-deps
106         run: |
107           python -m pip install --upgrade pip
108           pip install python-openstackclient
109           pip freeze
110       - uses: dorny/paths-filter@v2
111         id: changes
112         with:
113           base: ${{ inputs.GERRIT_BRANCH }}
114           ref: ${{ inputs.GERRIT_REFSPEC }}
115           filters: |
116             src:
117               - 'packer/**'
118       - if: steps.changes.outputs.src == 'true'
119         run: |
120           set -x
121           cd packer
122
123           varfiles=(common-packer/vars/*.pkrvars.hcl)
124           templates=(templates/*.pkr.hcl)
125
126           mkdir -p "${GITHUB_WORKSPACE}/logs"
127           PACKER_LOGS_DIR="${GITHUB_WORKSPACE}/logs"
128
129           for varfile in "${varfiles[@]}"; do
130               if [[ "$varfile" == *"cloud-env.json"* ]] || \
131                  [[ "$varfile" == "vars/*.json" ]] || \
132                  [[ "$varfile" == *"cloud-env.pkrvars.hcl"* ]] || \
133                  [[ "$varfile" == *"cloud-env-aws.pkrvars.hcl"* ]] || \
134                  [[ "$varfile" == "vars/*.pkrvars.hcl" ]]; then
135                   continue
136               fi
137
138               echo "-----> Test var: $varfile"
139               for template in "${templates[@]}"; do
140                   if [[ "$template" == *"variables.pkr.hcl"* ]] || \
141                      [[ "$template" == *"variables.auto.pkr.hcl"* ]]; then
142                       continue
143                   fi
144
145                   if [[ "${template#*.}" == "pkr.hcl" ]]; then
146                       echo "packer init $template ..."
147                       packer init "$template"
148                   fi
149
150                   export PACKER_LOG="yes"
151                   export PACKER_LOG_PATH="$PACKER_LOGS_DIR/packer-validate-${varfile##*/}-${template##*/}.log"
152                   if output=$(OS_CLOUD=${{ env.OS_CLOUD }} packer validate \
153                                   -var-file="${GITHUB_WORKSPACE}/cloud-env.pkrvars.hcl" \
154                                   -var-file="$varfile" "$template"); then
155                       echo "$template: $output"
156                   else
157                       echo "$template: $output"
158                       exit 1
159                   fi
160               done
161           done
162
163   vote:
164     if: ${{ always() }}
165     needs: [prepare, packer-validator]
166     runs-on: ubuntu-latest
167     steps:
168       - uses: technote-space/workflow-conclusion-action@v3
169       - name: Set vote
170         uses: lfit/gerrit-review-action@v0.4
171         with:
172           host: ${{ vars.GERRIT_SERVER }}
173           username: ${{ vars.GERRIT_SSH_USER }}
174           key: ${{ secrets.GERRIT_SSH_PRIVKEY }}
175           known_hosts: ${{ vars.GERRIT_KNOWN_HOSTS }}
176           gerrit-change-number: ${{ inputs.GERRIT_CHANGE_NUMBER }}
177           gerrit-patchset-number: ${{ inputs.GERRIT_PATCHSET_NUMBER }}
178           vote-type: ${{ env.WORKFLOW_CONCLUSION }}
179           comment-only: true