-
Notifications
You must be signed in to change notification settings - Fork 4
165 lines (158 loc) · 5.93 KB
/
build.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
---
name: Build images that changed
on:
- pull_request
jobs:
image-list:
name: build images
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.change-list.outputs.all_changed_files }}
any_changed: ${{ steps.change-list.outputs.any_changed }}
steps:
- name: checkout code
uses: actions/checkout@v4
with:
# full git history needed to get proper list of changed files
fetch-depth: 0
- name: Get list of changes
id: change-list
uses: tj-actions/changed-files@v45
with:
matrix: true
files: |
**/*.hcl
build-images:
name: Image builder
needs: image-list
if: ${{ needs.image-list.outputs.any_changed }}
runs-on: ubuntu-latest
strategy:
# just run one build at a time
max-parallel: 1
matrix:
images: ${{ fromJson(needs.image-list.outputs.matrix) }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup python
uses: actions/setup-python@v5
with:
python-version: 3.x
- name: Install environment
run: |
curl -L https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 > jq
chmod +x jq
pip install yq simplejson fedcloudclient
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.2.9
- name: Terraform Format
id: fmt
run: |
cd builder
terraform fmt -check
- name: Terraform init
id: init
run: |
cd builder
terraform init
- name: Deploy
env:
REFRESH_TOKEN: ${{ secrets.REFRESH_TOKEN }}
run: |
cd builder
mkdir -p ~/.config/openstack
touch ~/.config/openstack/secure.yaml
./refresh.sh vo.access.egi.eu "$REFRESH_TOKEN" deploy images
./refresh.sh cloud.egi.eu "$REFRESH_TOKEN" backend
FEDCLOUD_LOCKER_TOKEN="$(fedcloud secret locker create \
--oidc-access-token "$(cat .oidc_token)" \
--ttl 1h --num-uses 2)"
echo "::add-mask::$FEDCLOUD_LOCKER_TOKEN"
fedcloud secret put --locker-token "$FEDCLOUD_LOCKER_TOKEN" deploy "data=$REFRESH_TOKEN"
DEPLOY_SITE="$(yq -r .clouds.deploy.site clouds.yaml)"
echo "DEPLOY_SITE=$DEPLOY_SITE" >> "$GITHUB_ENV"
sed -i -e "s#%IMAGE%#${{ matrix.images }}#" cloud-init.yaml
sed -i -e "s/%TOKEN%/${{ secrets.GITHUB_TOKEN }}/" cloud-init.yaml
sed -i -e "s/%REF%/${{ github.sha }}/" cloud-init.yaml
sed -i -e "s/%SHORT_REF%/$(git rev-parse --short HEAD)/" cloud-init.yaml
sed -i -e "s/%FEDCLOUD_LOCKER_TOKEN%/$FEDCLOUD_LOCKER_TOKEN/" cloud-init.yaml
# terraform!
terraform apply -auto-approve -var-file="$DEPLOY_SITE.tfvars"
- name: Get VM ID
id: terraform-vm-id
run: |
cd builder
terraform output -raw instance-id
- name: Refresh backend token
env:
REFRESH_TOKEN: ${{ secrets.REFRESH_TOKEN }}
run: |
cd builder
./refresh.sh cloud.egi.eu "$REFRESH_TOKEN" backend
- name: Get the status file from swift
uses: nick-fields/retry@v3
with:
# this is about 40 minutes
max_attempts: 60
retry_wait_seconds: 40
timeout_minutes: 5
command: >
pushd builder &&
openstack --os-cloud backend --os-token "$(yq -r .clouds.backend.auth.token clouds.yaml)" object save fedcloud-vmi "${{ steps.terraform-vm-id.outputs.stdout }}" &&
openstack --os-cloud backend --os-token "$(yq -r .clouds.backend.auth.token clouds.yaml)" object delete fedcloud-vmi "${{ steps.terraform-vm-id.outputs.stdout }}"
- name: Process output
id: process-output
run: |
cd builder
cat "${{ steps.terraform-vm-id.outputs.stdout }}"
# show the status in the build log
{
echo 'build-log<<BUILDEOF'
echo "VM is ${{ steps.terraform-vm-id.outputs.stdout }}"
cat "${{ steps.terraform-vm-id.outputs.stdout }}"
echo "BUILDEOF"
} >> "$GITHUB_OUTPUT"
outcome=$( (grep "^### BUILD-IMAGE: " \
"${{ steps.terraform-vm-id.outputs.stdout }}" \
|| echo "ERROR") \
| cut -f2 -d":" | cut -f1 -d"-" | tr -d " ")
echo "outcome=$outcome" >> "$GITHUB_OUTPUT"
- name: Update PR with build status
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Build status: \`${{ steps.process-output.outputs.outcome }}\`
<details><summary>Build log</summary>
\`\`\`
${{ steps.process-output.outputs.build-log }}
\`\`\`
</details>
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
if (context.issue.number) {
issue = context.issue.number;
} else {
issue = (await github.rest.repos.listPullRequestsAssociatedWithCommit({
commit_sha: context.sha,
owner: context.repo.owner,
repo: context.repo.repo,
})).data[0].number;
}
github.rest.issues.createComment({
issue_number: issue,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
# Now create the new files and commit them? or PR them?
- name: Delete VM
if: always()
env:
REFRESH_TOKEN: ${{ secrets.REFRESH_TOKEN }}
run: |
cd builder
./refresh.sh vo.access.egi.eu "$REFRESH_TOKEN" deploy
terraform destroy -auto-approve -var-file="$DEPLOY_SITE.tfvars"