forked from sclorg/testing-farm-as-github-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
217 lines (205 loc) · 7.69 KB
/
action.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
name: 'Schedule tests on Testing Farm'
description: 'A GitHub action will schedule a user defined tests to the Testing Farm to run tmt tests.'
author: 'RHSCL team'
branding:
icon: circle
color: red
inputs:
api_key:
description: 'A testing farm API key'
required: true
tmt_repository:
description: 'An url to tmt repository'
required: true
test_fmf_plan:
required: false
description: 'A fmf plan which will be selected. By default all plans are selected.'
tests_tmt_ref:
description: 'A tmt tests branch which will be used for tests'
required: false
default: 'master'
compose:
description: 'A compose for tests'
default: 'Fedora'
required: true
create_issue_comment:
description: 'It creates a github issue Comment'
required: false
default: 'false'
pull_request_status_name:
description: 'GitHub pull request status name'
required: false
default: 'Fedora'
env_vars:
description: 'Environment variables for test env, separated by ;'
required: false
default: ''
env_secrets:
description: 'Environment secrets for test env, separated by ;'
required: false
default: ''
debug:
description: 'Print debug logs when working with testing farm'
required: false
default: 'true'
update_pull_request_status:
description: 'Action will update pull request status. Default: true'
required: false
default: 'true'
outputs:
request_id:
description: 'An ID of a scheduled testing farm request'
value: ${{ env.REQ_ID }}
request_url:
description: 'An url of a scheduled testing farm request'
value: ${{ env.TF_URL }}
runs:
using: "composite"
steps:
- name: Install mandatory packages
run: |
sudo apt update && sudo apt -y install curl jq
- name: Get pull request number
id: pr_nr
run: |
PR_URL="${{ github.event.comment.issue_url }}"
echo "::set-output name=PR_NR::${PR_URL##*/}"
- name: Get commit SHA value
id: sha_value
run: |
echo "::set-output name=SHA::$(git re-parse HEAD)"
- name: Generate tmt variables
id: generate_tmt_vars
run: |
python -c 'import json; print({} if not "${{ inpust.env_vars }}".strip() else json.dumps({key: value for key, value in [s.split("=", 1) for s in "${{ inpust.env_vars }}".split(";")]}))' > env_vars
echo "::set-output name=TMT_ENV_VARS::$(cat env_vars)"
- name: Generate tmt secrets
id: generate_tmt_secrets
run: |
python -c 'import json; print({} if not "${{ inpust.env_secrets }}".strip() else json.dumps({key: value for key, value in [s.split("=", 1) for s in "${{ inpust.env_secrets }}".split(";")]}))' > env_secrets
echo "::set-output name=TMT_ENV_SECRETS::$(cat env_secrets)"
- name: Schedule a test on Testing Farm
id: sched_test
run: |
cat << EOF > request.json
{
"api_key": "${{ inputs.api_key }}",
"test": { "fmf": {
"url": "${{ inputs.tmt_repository }}",
"ref": "${{ inputs.tmt_ref }}",
"name": "{{ inputs.tests_tmt_ref }}",
}
},
"environments": [{
"arch": "x86_64",
"os": {
"compose": "${{ inputs.compose }}"
},
"variables": "${{ steps.generate_tmt_vars.outputs.TMT_ENV_VARS }}"
"secrets": "${{ steps.generate_tmt_secrets.outputs.TMT_ENV_SECRETS }}"
}]
}
EOF
if [ "${{ inputs.debug }}" == "true" ]; then
echo "Let's print request.json"
cat request.json
fi
curl https://api.testing-farm.io/requests \
--data @request.json \
--header "Content-Type: application/json" \
--output response.json
if [ "${{ inputs.debug }}" == "true" ]; then
echo "Let's print testing farm response"
cat response.json
jq < response.json
fi
# Store REQ_ID into GITHUB_ENV variables
req_id=$(jq -r .id response.json)
tf_url=$(jq -r .run.artifacts response.json)
echo "REQ_ID=$req_id" >> $GITHUB_ENV
echo "TF_URL=$tf_url" >> $GITHUB_ENV
- name: Switch pull request state to running
id: running
run: |
if [ "${{ inputs.update_pull_request_status }}" == "true" ]; then
# Create running.json file for query, whether job is finished or not.
cat << EOF > running.json
{
"sha": "${{ steps.sha_value.outputs.SHA }}",
"state": "pending",
"context": "Testing Farm - ${{ inputs.pull_request_status_name }}",
"description": "Build started",
"target_url": "${{ env.TF_URL }}/${{ env.REQ_ID }}"
}
EOF
# Update GitHub status description to 'Build started'
curl -X POST -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/$GITHUB_REPOSITORY/statuses/${{ steps.sha_value.outputs.SHA }} \
--data @running.json
fi
- name: Check if scheduled test is still running
id: still_running
run: |
CMD=https://api.testing-farm.io/requests/${{ env.REQ_ID }}
curl $CMD > job.json
if [ "${{ inputs.debug }}" == "true" ]; then
cat job.json
jq < job.json
fi
state=$(jq -r .state job.json)
while [ "$state" == "running" ] || [ "$state" == "new" ] || [ "$state" == "pending" ] || [ "$state" == "queued" ]; do
sleep 30
curl $CMD > job.json
if [ "${{ inputs.debug }}" == "true" ]; then
cat job.json
jq < job.json
fi
state=$(jq -r .state job.json)
done
- name: Get final state of Testing Farm scheduled request
id: finale_state
run: |
curl ${{ secrets.TF_ENDPOINT }}/requests/${{ env.REQ_ID }} > job.json
if [ "${{ inputs.debug }}" == "true" ]; then
cat job.json
jq < job.json
fi
state=$(jq -r .state job.json)
result=$(jq -r .result.overall job.json)
new_state="success"
infra_error=" "
echo "State is $state and result is: $result"
if [ "$state" == "complete" ]; then
if [ "$result" != "passed" ]; then
new_state="failure"
fi
else
# Mark job in case of infrastructure issues. Report to Testing Farm team
infra_error=" - Infra problems"
new_state="failure"
fi
echo "New State is: $new_state"
echo "Infra state is: $infra_error"
echo "::set-output name=FINAL_STATE::$new_state"
echo "::set-output name=INFRA_STATE::$infra_error"
- name: Switch pull request GitHub status to final state
run: |
if [ "${{ inputs.update_pull_request_status }}" == "true" ]; then
cat << EOF > final.json
{
"sha": "${{ steps.sha_value.outputs.SHA }}",
"state": "${{ steps.final_state.outputs.FINAL_STATE }}",
"context": "Testing Farm - ${{ inputs.pull_request_status_name }}",
"description": "Build finished${{ steps.final_state.outputs.INFRA_STATE }}",
"target_url": "${{ env.TF_URL }}/${{ env.REQ_ID }}"
}
EOF
if [ "${{ inputs.debug }}" == "true" ]; then
cat final.json
fi
# Switch Github status to proper state
curl -X POST -H "Authorization:
Bearer ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/$GITHUB_REPOSITORY/statuses/${{ steps.sha_value.outputs.SHA }} \
--data @final.json
fi