Skip to content

Commit ede1aa4

Browse files
committed
New runner
1 parent 243d062 commit ede1aa4

File tree

5 files changed

+176
-49
lines changed

5 files changed

+176
-49
lines changed

.github/workflows/create-vm.yml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,23 @@ jobs:
2525
label: ${{ steps.create-runner.outputs.label }}
2626
machine-zone: ${{ steps.create-runner.outputs.machine-zone }}
2727
steps:
28-
- uses: actions/checkout@v3
29-
- id: auth
30-
name: Authenticate to Google Cloud
28+
- name: Checkout repository
29+
uses: actions/checkout@v3
30+
- name: Authenticate to Google Cloud
31+
id: auth
32+
if: github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
3133
uses: google-github-actions/auth@v1
3234
with:
3335
credentials_json: ${{ secrets.gcp_credentials }}
34-
- id: gcloud-auth
35-
name: gcloud auth activate-service-account
36+
- name: Activate GCP service account
37+
id: gcloud-auth
38+
if: github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
3639
shell: bash
3740
run: |
3841
gcloud auth activate-service-account --key-file ${{ steps.auth.outputs.credentials_file_path }}
3942
- id: create-runner
40-
uses: gitpod-io/gce-github-runner@secrets
43+
uses: gitpod-io/gce-github-runner@delete-secrets
4144
with:
4245
runner_token: ${{ secrets.runner_token }}
43-
task: ${{ inputs.task }}
44-
46+
task: ${{ inputs.task }}
47+
gcp_credentials: ${{ secrets.gcp_credentials }}

.github/workflows/delete-vm.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ on:
1717
jobs:
1818
run:
1919
runs-on: ubuntu-latest
20+
name: Cleanup
2021
steps:
21-
- uses: actions/checkout@v3
2222
- id: auth
2323
name: Authenticate to Google Cloud
2424
uses: google-github-actions/auth@v1
2525
with:
2626
credentials_json: ${{ secrets.gcp_credentials }}
2727
- id: gcloud-auth
28-
name: gcloud auth activate-service-account
28+
name: Destroy GCE VM
2929
shell: bash
3030
run: |
31-
if [ -z "${{ inputs.runner-label }}" ];then
32-
exit 0
33-
fi
31+
set -x
32+
33+
echo "Removing GCE VM..."
3434
3535
gcloud auth activate-service-account --key-file ${{ steps.auth.outputs.credentials_file_path }}
3636
if [ -z "$(gcloud compute instances list | grep "${{ inputs.runner-label }}")" ]; then

.github/workflows/new-runner.yml

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
name: "Ephemeral GCE GitHub self-hosted runner"
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
runner_token:
7+
description: >-
8+
GitHub auth token, needs `repo`/`public_repo` scope: https://docs.github.com/en/rest/reference/actions#self-hosted-runners.
9+
required: true
10+
type: string
11+
project_id:
12+
description: >-
13+
ID of the Google Cloud Platform project. If provided, this will configure gcloud to use this project ID.
14+
required: false
15+
default: public-github-runners
16+
type: string
17+
machine_zone:
18+
description: GCE zone
19+
default: "europe-west1-b"
20+
required: false
21+
type: string
22+
machine_type:
23+
description: GCE machine type; https://cloud.google.com/compute/docs/machine-types
24+
default: "n2d-standard-8"
25+
required: true
26+
type: string
27+
disk_size:
28+
description: VM disk size.
29+
required: false
30+
default: 250GB
31+
type: string
32+
image_project:
33+
description: >
34+
The Google Cloud project against which all image and image family references will be resolved.
35+
required: false
36+
default: public-github-runners
37+
type: string
38+
image:
39+
description: Specifies the name of the image that the disk will be initialized with.
40+
required: false
41+
default: gh-runner-202307281245
42+
type: string
43+
image_family:
44+
description: The image family for the operating system that the boot disk will be initialized with.
45+
required: false
46+
type: string
47+
scopes:
48+
description: Scopes granted to the VM, defaults to full access (cloud-platform).
49+
default: cloud-platform
50+
required: true
51+
type: string
52+
shutdown_timeout:
53+
description: "Shutdown grace period (in seconds)."
54+
default: 30
55+
required: true
56+
type: string
57+
task:
58+
description: Additional context about the workflow
59+
default: default
60+
required: true
61+
type: string
62+
gcp_credentials:
63+
description: GCP JSON credentials
64+
required: true
65+
type: string
66+
67+
outputs:
68+
label:
69+
description: >-
70+
Unique runner label. This label can be used to request a specific
71+
runner for the workflow job.
72+
value: ${{ steps.gce-github-runner-script.outputs.label }}
73+
machine-zone:
74+
description: >-
75+
VM availability zone
76+
value: ${{ steps.gce-github-runner-script.outputs.machine-zone }}
77+
78+
jobs:
79+
run:
80+
runs-on: ubuntu-latest
81+
steps:
82+
- id: check-repository
83+
if: github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
84+
shell: bash
85+
run: |
86+
echo "❌ Job actions are not allowed to run in forks" >> $GITHUB_STEP_SUMMARY
87+
exit 1
88+
- uses: actions/checkout@v3
89+
- id: auth
90+
name: Authenticate to Google Cloud
91+
uses: google-github-actions/auth@v1
92+
with:
93+
credentials_json: ${{ inputs.gcp_credentials }}
94+
- id: gcloud-auth
95+
name: gcloud auth activate-service-account
96+
shell: bash
97+
run: |
98+
gcloud auth activate-service-account --key-file ${{ steps.auth.outputs.credentials_file_path }}
99+
- id: gce-github-runner-script
100+
if: github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
101+
uses: pyTooling/Actions/with-post-step@r0
102+
with:
103+
main: |
104+
${{ github.action_path }}/action.sh
105+
--command=start
106+
--runner_token=${{ inputs.runner_token }}
107+
--project_id=${{ inputs.project_id }}
108+
--machine_zone=${{ inputs.machine_zone }}
109+
--machine_type=${{ inputs.machine_type }}
110+
--disk_size=${{ inputs.disk_size }}
111+
--scopes=${{ inputs.scopes }}
112+
--shutdown_timeout=${{ inputs.shutdown_timeout }}
113+
--image_project=${{ inputs.image_project }}
114+
--image=${{ inputs.image }}
115+
--image_family=${{ inputs.image_family }}
116+
--boot_disk_type=pd-ssd
117+
--task=${{ inputs.task }}
118+
post: |
119+
echo "Removing GCE VM..."
120+
121+
gcloud auth activate-service-account --key-file ${{ steps.auth.outputs.credentials_file_path }}
122+
if [ -z "$(gcloud compute instances list | grep "${{ inputs.runner-label }}")" ]; then
123+
# vm is gone
124+
exit 0
125+
fi
126+
127+
gcloud compute instances delete ${{ inputs.runner-label }} --quiet --zone ${{ inputs.machine-zone }}

action.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,12 @@ function start_vm {
139139

140140
echo "The new GCE VM will be ${VM_ID}"
141141

142+
RUNNER_ID="${VM_ID}-$(date +%s)"
143+
142144
cat <<FILE_EOF >/tmp/startup-script.sh
143145
#!/bin/bash
144146
145147
set -e
146-
set -x
147148
148149
# leeway temporal directories
149150
chmod 777 /var/tmp
@@ -172,7 +173,6 @@ EOF
172173
173174
chmod +x /etc/systemd/system/shutdown.sh
174175
175-
RUNNER_ID=${VM_ID}-$(date +%s)
176176
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
177177
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
178178
@@ -187,7 +187,6 @@ FILE_EOF
187187
#!/bin/bash
188188
189189
set -e
190-
set -x
191190
192191
pushd /actions-runner || exit 0
193192

action.yml

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,49 +68,47 @@ outputs:
6868
runs:
6969
using: "composite"
7070
steps:
71-
- id: check-repository
71+
- name: Check if we are allowed to run
72+
id: check-repository
7273
if: github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
7374
shell: bash
7475
run: |
7576
echo "❌ Job actions are not allowed to run in forks" >> $GITHUB_STEP_SUMMARY
7677
exit 1
77-
- uses: actions/checkout@v3
78-
- id: auth
79-
name: Authenticate to Google Cloud
78+
- name: Checkout repository
79+
uses: actions/checkout@v3
80+
- name: Authenticate to Google Cloud
81+
id: auth
82+
if: github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
8083
uses: google-github-actions/auth@v1
8184
with:
8285
credentials_json: ${{ inputs.gcp_credentials }}
83-
- id: gcloud-auth
84-
name: gcloud auth activate-service-account
86+
export_environment_variables: true
87+
cleanup_credentials: false
88+
- name: Activate GCP service account
89+
id: gcloud-auth
90+
if: github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
8591
shell: bash
8692
run: |
8793
gcloud auth activate-service-account --key-file ${{ steps.auth.outputs.credentials_file_path }}
88-
- id: gce-github-runner-script
94+
- name: Create GCE VM
95+
id: gce-github-runner-script
8996
if: github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name
90-
uses: pyTooling/Actions/with-post-step@r0
91-
with:
92-
main: |
93-
${{ github.action_path }}/action.sh
94-
--command=start
95-
--runner_token=${{ inputs.runner_token }}
96-
--project_id=${{ inputs.project_id }}
97-
--machine_zone=${{ inputs.machine_zone }}
98-
--machine_type=${{ inputs.machine_type }}
99-
--disk_size=${{ inputs.disk_size }}
100-
--scopes=${{ inputs.scopes }}
101-
--shutdown_timeout=${{ inputs.shutdown_timeout }}
102-
--image_project=${{ inputs.image_project }}
103-
--image=${{ inputs.image }}
104-
--image_family=${{ inputs.image_family }}
105-
--boot_disk_type=pd-ssd
106-
--task=${{ inputs.task }}
107-
post: |
108-
echo "Removing GCE VM..."
109-
110-
gcloud auth activate-service-account --key-file ${{ steps.auth.outputs.credentials_file_path }}
111-
if [ -z "$(gcloud compute instances list | grep "${{ inputs.runner-label }}")" ]; then
112-
# vm is gone
113-
exit 0
114-
fi
97+
shell: bash
98+
run: |
99+
set -x
115100
116-
gcloud compute instances delete ${{ inputs.runner-label }} --quiet --zone ${{ inputs.machine-zone }}
101+
${{ github.action_path }}/action.sh \
102+
--command=start \
103+
--runner_token=${{ inputs.runner_token }} \
104+
--project_id=${{ inputs.project_id }} \
105+
--machine_zone=${{ inputs.machine_zone }} \
106+
--machine_type=${{ inputs.machine_type }} \
107+
--disk_size=${{ inputs.disk_size }} \
108+
--scopes=${{ inputs.scopes }} \
109+
--shutdown_timeout=${{ inputs.shutdown_timeout }} \
110+
--image_project=${{ inputs.image_project }} \
111+
--image=${{ inputs.image }} \
112+
--image_family=${{ inputs.image_family }} \
113+
--boot_disk_type=pd-ssd \
114+
--task=${{ inputs.task }}

0 commit comments

Comments
 (0)