Skip to content

Commit 015a480

Browse files
authored
Merge pull request #10 from gitpod-io/wv/add-label-input
Add label input to allow recreating the VM
2 parents a36405b + 1a67573 commit 015a480

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

.github/workflows/create-vm.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ on:
77
type: string
88
required: false
99
default: "default"
10+
# If set, use this label instead of generating a random one (and 'task' will be ignored).
11+
# Useful to recreate a runner VM that got shutdown.
12+
label:
13+
type: string
14+
required: false
15+
default: ""
1016
secrets:
1117
gcp_credentials:
1218
required: true
@@ -30,19 +36,20 @@ jobs:
3036
uses: actions/checkout@v4
3137
- name: Authenticate to Google Cloud
3238
id: auth
33-
if: github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
39+
if: github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
3440
uses: google-github-actions/auth@v1
3541
with:
3642
credentials_json: ${{ secrets.gcp_credentials }}
3743
- name: Activate GCP service account
3844
id: gcloud-auth
39-
if: github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
45+
if: github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
4046
shell: bash
4147
run: |
4248
gcloud auth activate-service-account --key-file ${{ steps.auth.outputs.credentials_file_path }}
4349
- id: create-runner
4450
uses: gitpod-io/gce-github-runner@main
4551
with:
4652
runner_token: ${{ secrets.runner_token }}
47-
task: ${{ inputs.task }}
53+
task: ${{ inputs.task }}
54+
label: ${{ inputs.label }}
4855
gcp_credentials: ${{ secrets.gcp_credentials }}

action.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ disk_size=
2828
image_project=
2929
image=
3030
image_family=
31+
label=
3132
scopes=
3233
shutdown_timeout=
3334
task=
@@ -44,6 +45,7 @@ while getopts_long :h opt \
4445
image_project optional_argument \
4546
image optional_argument \
4647
image_family optional_argument \
48+
label optional_argument \
4749
scopes required_argument \
4850
shutdown_timeout required_argument \
4951
task required_argument \
@@ -79,6 +81,9 @@ while getopts_long :h opt \
7981
image_family)
8082
image_family=${OPTLARG-$image_family}
8183
;;
84+
label)
85+
label=${OPTLARG-$label}
86+
;;
8287
scopes)
8388
scopes=$OPTLARG
8489
;;
@@ -102,6 +107,10 @@ done
102107

103108
function start_vm {
104109
VM_ID="runner-$(echo ${GITHUB_RUN_ID}-${GITHUB_RUN_NUMBER}-${task} | sha1sum | cut -f 1 -d " ")"
110+
if [ ! -z "${label}" ]; then
111+
echo "Label provided, using it as VM ID (${label})"
112+
VM_ID="${label}"
113+
fi
105114

106115
if [ ! -z "$(gcloud compute instances list | grep "${VM_ID}")" ]; then
107116
# the VM already exists.
@@ -114,7 +123,7 @@ function start_vm {
114123
fi
115124

116125
echo "Starting GCE VM ..."
117-
if [ -z "$runner_token" ]; then
126+
if [ -z "$runner_token" ]; then
118127
echo "❌ runner_token parameter is required"
119128
exit 1
120129
fi
@@ -123,7 +132,7 @@ function start_vm {
123132
-H "Authorization: Bearer $runner_token" \
124133
"https://api.github.com/repos/$GITHUB_REPOSITORY/actions/runners/registration-token" |
125134
jq -r .token)
126-
if [ -z "$RUNNER_TOKEN" ]; then
135+
if [ -z "$RUNNER_TOKEN" ]; then
127136
echo "❌ Failed to get a registration token"
128137
exit 1
129138
fi
@@ -202,10 +211,10 @@ REMOVE_TOKEN=\$(curl \
202211
-H "Accept: application/vnd.github+json" \
203212
-H "Authorization: Bearer ${RUNNER_TOKEN}" \
204213
https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runners/remove-token | jq .token --raw-output)
205-
if [ -z "\$REMOVE_TOKEN" ]; then
214+
if [ -z "\$REMOVE_TOKEN" ]; then
206215
echo "❌ Failed to get a removal token"
207216
exit 0
208-
fi
217+
fi
209218
210219
su -s /bin/bash -c "cd /actions-runner/;/actions-runner-1/config.sh remove --token \${REMOVE_TOKEN}" runner
211220
su -s /bin/bash -c "cd /actions-runner/;/actions-runner-2/config.sh remove --token \${REMOVE_TOKEN}" runner

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ inputs:
3939
image_family:
4040
description: The image family for the operating system that the boot disk will be initialized with.
4141
required: false
42+
label:
43+
description: >-
44+
Runner label. Can be set to override the VM name to recreate a runner with a specific label,
45+
e.g. to replace a runner that was shutdown. If set, 'task' will be ignored.
46+
required: false
4247
scopes:
4348
description: Scopes granted to the VM, defaults to full access (cloud-platform).
4449
default: cloud-platform
@@ -108,5 +113,6 @@ runs:
108113
--image_project=${{ inputs.image_project }} \
109114
--image=${{ inputs.image }} \
110115
--image_family=${{ inputs.image_family }} \
116+
--label=${{ inputs.label }} \
111117
--boot_disk_type=pd-ssd \
112118
--task=${{ inputs.task }}

0 commit comments

Comments
 (0)