Skip to content

Commit 14f4199

Browse files
authored
Merge pull request #3 from gitpod-io/delete-secrets
Refactoring
2 parents cc85ef4 + 630c148 commit 14f4199

File tree

13 files changed

+178
-76
lines changed

13 files changed

+178
-76
lines changed

.github/workflows/create-vm.yml

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,48 @@
1-
name: Create ephemeral GCE VM
1+
name: Create GCE VM
22

33
on:
44
workflow_call:
5+
inputs:
6+
task:
7+
type: string
8+
required: false
9+
default: "default"
510
secrets:
6-
GCP_CREDENTIALS:
11+
gcp_credentials:
712
required: true
8-
GH_SELF_HOSTED_RUNNER:
13+
runner_token:
914
required: true
1015
outputs:
1116
label:
1217
value: ${{ jobs.run.outputs.label }}
18+
machine-zone:
19+
value: ${{ jobs.run.outputs.machine-zone }}
1320

1421
jobs:
1522
run:
23+
name: Create GCE VM
1624
runs-on: ubuntu-latest
1725
outputs:
1826
label: ${{ steps.create-runner.outputs.label }}
27+
machine-zone: ${{ steps.create-runner.outputs.machine-zone }}
1928
steps:
20-
- uses: actions/checkout@v3
21-
- id: auth
22-
name: Authenticate to Google Cloud
29+
- name: Checkout repository
30+
uses: actions/checkout@v3
31+
- name: Authenticate to Google Cloud
32+
id: auth
33+
if: github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
2334
uses: google-github-actions/auth@v1
2435
with:
25-
credentials_json: ${{ secrets.GCP_CREDENTIALS }}
26-
- id: gcloud-auth
27-
name: gcloud auth activate-service-account
36+
credentials_json: ${{ secrets.gcp_credentials }}
37+
- name: Activate GCP service account
38+
id: gcloud-auth
39+
if: github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
2840
shell: bash
2941
run: |
3042
gcloud auth activate-service-account --key-file ${{ steps.auth.outputs.credentials_file_path }}
3143
- id: create-runner
32-
uses: gitpod-io/gce-github-runner@main
44+
uses: gitpod-io/gce-github-runner@delete-secrets
3345
with:
34-
token: ${{ secrets.GH_SELF_HOSTED_RUNNER }}
46+
runner_token: ${{ secrets.runner_token }}
47+
task: ${{ inputs.task }}
48+
gcp_credentials: ${{ secrets.gcp_credentials }}

.github/workflows/delete-vm.yml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,36 @@ on:
66
runner-label:
77
type: string
88
required: true
9+
machine-zone:
10+
type: string
11+
required: true
912

1013
secrets:
11-
GCP_CREDENTIALS:
14+
gcp_credentials:
1215
required: true
1316

1417
jobs:
1518
run:
1619
runs-on: ubuntu-latest
20+
name: Delete ephemeral GCE VM
1721
steps:
18-
- uses: actions/checkout@v3
1922
- id: auth
2023
name: Authenticate to Google Cloud
2124
uses: google-github-actions/auth@v1
2225
with:
23-
credentials_json: ${{ secrets.GCP_CREDENTIALS }}
26+
credentials_json: ${{ secrets.gcp_credentials }}
2427
- id: gcloud-auth
25-
name: gcloud auth activate-service-account
28+
name: Destroy GCE VM
2629
shell: bash
2730
run: |
28-
if [ -z "${{ inputs.runner-label }}" ];then
29-
exit 0
30-
fi
31+
set -x
32+
33+
echo "Removing GCE VM..."
3134
3235
gcloud auth activate-service-account --key-file ${{ steps.auth.outputs.credentials_file_path }}
3336
if [ -z "$(gcloud compute instances list | grep "${{ inputs.runner-label }}")" ]; then
3437
# vm is gone
3538
exit 0
3639
fi
3740
38-
gcloud compute instances delete ${{ inputs.runner-label }} --quiet --zone ${{ inputs.runner-az }}
41+
gcloud compute instances delete ${{ inputs.runner-label }} --quiet --zone ${{ inputs.machine-zone }}

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
11
# gce-github-runner
22

33
Ephemeral GCE GitHub self-hosted runner based on https://github.com/related-sciences/gce-github-runner
4+
5+
## Usage
6+
7+
```yaml
8+
jobs:
9+
create-runner:
10+
uses: gitpod-io/gce-github-runner/.github/workflows/create-vm.yml@secrets
11+
secrets:
12+
runner_token: ${{ secrets.GH_SA_TOKEN }}
13+
gcp_credentials: ${{ secrets.GCP_SA_KEY }}
14+
15+
test:
16+
needs: [create-runner]
17+
runs-on: ${{ needs.create-runner.outputs.label }}
18+
steps:
19+
- run: echo "This runs on the GCE VM"
20+
21+
delete-runner:
22+
if: always()
23+
needs:
24+
- create-runner
25+
- test
26+
uses: gitpod-io/gce-github-runner/.github/workflows/delete-vm.yml@secrets
27+
secrets:
28+
gcp_credentials: ${{ secrets.GCP_SA_KEY }}
29+
with:
30+
runner-label: ${{ needs.create-runner.outputs.label }}
31+
machine-zone: ${{ needs.create-runner.outputs.machine-zone }}
32+
```
33+
34+
* `create-runner` creates the GCE VM and registers the runner with unique label
35+
* `test` uses the runner
36+
* `delete-runner` waits for the end of the steps execution and then shutdowns the GCE VM, removing the runner from the GitHub runner
37+
38+
39+
## Inputs
40+
41+
See inputs and descriptions [here](./action.yml).

action.sh

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,50 +19,45 @@ function safety_off {
1919
source "${ACTION_DIR}/vendor/getopts_long.sh"
2020

2121
command=
22-
token=
22+
runner_token=
2323
project_id=
24-
runner_ver=
2524
machine_zone=
2625
machine_type=
2726
boot_disk_type=
2827
disk_size=
29-
runner_service_account=
3028
image_project=
3129
image=
3230
image_family=
3331
scopes=
3432
shutdown_timeout=
33+
task=
3534

3635
OPTLIND=1
3736
while getopts_long :h opt \
3837
command required_argument \
39-
token required_argument \
38+
runner_token required_argument \
4039
project_id required_argument \
41-
runner_ver required_argument \
4240
machine_zone required_argument \
4341
machine_type required_argument \
4442
boot_disk_type optional_argument \
4543
disk_size optional_argument \
46-
runner_service_account optional_argument \
4744
image_project optional_argument \
4845
image optional_argument \
4946
image_family optional_argument \
5047
scopes required_argument \
5148
shutdown_timeout required_argument \
49+
task required_argument \
5250
help no_argument "" "$@"; do
5351
case "$opt" in
5452
command)
5553
command=$OPTLARG
5654
;;
57-
token)
58-
token=$OPTLARG
55+
runner_token)
56+
runner_token=$OPTLARG
5957
;;
6058
project_id)
6159
project_id=$OPTLARG
6260
;;
63-
runner_ver)
64-
runner_ver=$OPTLARG
65-
;;
6661
machine_zone)
6762
machine_zone=$OPTLARG
6863
;;
@@ -75,9 +70,6 @@ while getopts_long :h opt \
7570
disk_size)
7671
disk_size=${OPTLARG-$disk_size}
7772
;;
78-
runner_service_account)
79-
runner_service_account=${OPTLARG-$runner_service_account}
80-
;;
8173
image_project)
8274
image_project=${OPTLARG-$image_project}
8375
;;
@@ -93,6 +85,9 @@ while getopts_long :h opt \
9385
shutdown_timeout)
9486
shutdown_timeout=$OPTLARG
9587
;;
88+
task)
89+
task=$OPTLARG
90+
;;
9691
h | help)
9792
usage
9893
exit 0
@@ -106,21 +101,33 @@ while getopts_long :h opt \
106101
done
107102

108103
function start_vm {
109-
VM_ID="gce-gh-runner-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}"
104+
VM_ID="runner-$(echo ${GITHUB_RUN_ID}-${GITHUB_RUN_NUMBER}-${task} | sha1sum | cut -f 1 -d " ")"
110105

111106
if [ ! -z "$(gcloud compute instances list | grep "${VM_ID}")" ]; then
112107
# the VM already exists.
113108
# this can happen when we call the action from a reusable workflow.
114109
# in these scenarios we don't want a new VM ;)
115-
echo "Skipping creation of new VM. Using the existing one."
110+
echo "Skipping creation of new VM. Using the existing one (${VM_ID})"
111+
echo "label=${VM_ID}" >>"${GITHUB_OUTPUT}"
112+
echo "machine-zone=${machine_zone}" >>"${GITHUB_OUTPUT}"
116113
exit 0
117114
fi
118115

119116
echo "Starting GCE VM ..."
117+
if [ -z "$runner_token" ]; then
118+
echo "❌ runner_token parameter is required"
119+
exit 1
120+
fi
121+
120122
RUNNER_TOKEN=$(curl -S -s -XPOST \
121-
-H "authorization: Bearer ${token}" \
122-
"https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runners/registration-token" |
123+
-H "Authorization: Bearer $runner_token" \
124+
"https://api.github.com/repos/$GITHUB_REPOSITORY/actions/runners/registration-token" |
123125
jq -r .token)
126+
if [ -z "$RUNNER_TOKEN" ]; then
127+
echo "❌ Failed to get a registration token"
128+
exit 1
129+
fi
130+
124131
echo "✅ Successfully got the GitHub Runner registration token"
125132

126133
image_project_flag=$([[ -z "${image_project}" ]] || echo "--image-project=${image_project}")
@@ -132,11 +139,12 @@ function start_vm {
132139

133140
echo "The new GCE VM will be ${VM_ID}"
134141

142+
RUNNER_ID="${VM_ID}-$(date +%s)"
143+
135144
cat <<FILE_EOF >/tmp/startup-script.sh
136145
#!/bin/bash
137146
138147
set -e
139-
set -x
140148
141149
# leeway temporal directories
142150
chmod 777 /var/tmp
@@ -156,6 +164,12 @@ cleanup() {
156164
trap 'cleanup; exit 130' INT
157165
trap 'cleanup; exit 143' TERM
158166
167+
cat <<-EOF >/etc/environment
168+
PATH="/home/runner/go-packages/bin:/home/runner/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
169+
GOPATH="/home/runner/go-packages"
170+
GOROOT="/home/runner/go"
171+
EOF
172+
159173
# Create a systemd service in charge of shutting down the machine once the workflow has finished
160174
cat <<-EOF >/etc/systemd/system/shutdown.sh
161175
#!/bin/sh
@@ -165,7 +179,6 @@ EOF
165179
166180
chmod +x /etc/systemd/system/shutdown.sh
167181
168-
RUNNER_ID=${VM_ID}-$(date +%s)
169182
su -s /bin/bash -c "cd /actions-runner-1/;/actions-runner-1/config.sh --url https://github.com/${GITHUB_REPOSITORY} --token ${RUNNER_TOKEN} --name ${RUNNER_ID}-1 --labels ${VM_ID} --unattended --disableupdate" runner
170183
su -s /bin/bash -c "cd /actions-runner-2/;/actions-runner-2/config.sh --url https://github.com/${GITHUB_REPOSITORY} --token ${RUNNER_TOKEN} --name ${RUNNER_ID}-2 --labels ${VM_ID} --unattended --disableupdate" runner
171184
@@ -180,7 +193,6 @@ FILE_EOF
180193
#!/bin/bash
181194
182195
set -e
183-
set -x
184196
185197
pushd /actions-runner || exit 0
186198
@@ -191,7 +203,7 @@ REMOVE_TOKEN=\$(curl \
191203
-H "Authorization: Bearer ${RUNNER_TOKEN}" \
192204
https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runners/remove-token | jq .token --raw-output)
193205
if [ -z "\$REMOVE_TOKEN" ]; then
194-
echo "Failed to get a removal token"
206+
echo "Failed to get a removal token"
195207
exit 0
196208
fi
197209
@@ -217,8 +229,10 @@ FILE_EOF
217229
--maintenance-policy="TERMINATE" \
218230
--metadata-from-file="startup-script=/tmp/startup-script.sh,shutdown-script=/tmp/shutdown-script.sh" &&
219231
echo "label=${VM_ID}" >>"${GITHUB_OUTPUT}"
232+
echo "machine-zone=${machine_zone}" >>"${GITHUB_OUTPUT}"
220233

221234
safety_off
235+
set +x
222236
while ((i++ < 60)); do
223237
GH_READY=$(gcloud compute instances describe "${VM_ID}" --zone="${machine_zone}" --format='json(labels)' | jq -r .labels.gh_ready)
224238
if [[ $GH_READY == 1 ]]; then
@@ -230,7 +244,7 @@ FILE_EOF
230244
if [[ $GH_READY == 1 ]]; then
231245
echo "${VM_ID} ready ..."
232246
else
233-
echo "Waited 5 minutes for ${VM_ID}, without luck, deleting ${VM_ID} ..."
247+
echo "Waited 5 minutes for ${VM_ID}, without luck, deleting ${VM_ID} ..."
234248
gcloud --quiet compute instances delete "${VM_ID}" --zone="${machine_zone}"
235249
exit 1
236250
fi

0 commit comments

Comments
 (0)