-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaction.yml
113 lines (106 loc) · 4.3 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
name: metal-github-runner
branding:
icon: 'rotate-cw'
color: 'green'
description: "Provision an Equinix Metal Server and set up a github self-hosted runner on it"
inputs:
github_token:
description: "Github Token with repo scope"
required: true
metal_auth_token:
description: "Equinix Metal API Token"
required: true
metal_project_id:
description: "Equinix Metal Project ID"
required: true
metro:
description: "Equinix Metal Metro for Github self-hosted runner"
required: true
plan:
description: "Equinix Metal Plan for Github self-hosted runner"
required: true
os:
description: "Equinix Metal OS for Github self-hosted runner"
default: "ubuntu_22_04"
provisioning_timeout:
description: "Equinix Metal Provisioning Timeout for Github self-hosted runner"
default: "30"
repository_level:
description: "Set to true to enable repository level registration token"
default: "false"
custom_script:
description: "Custom user script to run (as root) before starting the runner"
default: ""
runs:
using: "composite"
steps:
- run: |
# Determine the level and runner scope based on inputs
if [[ "${{ inputs.repository_level }}" == "true" ]] || [[ -z "${{ github.organization }}" ]]; then
level="repos"
runner_scope="${{ github.repository }}"
else
level="orgs"
runner_scope="${{ github.organization }}"
fi
# Get the registration token
registration_token=$(curl -fsSL \
-X POST \
-H 'accept: application/vnd.github+json' \
-H 'authorization: Bearer ${{ inputs.github_token }}' \
-H 'X-GitHub-Api-Version: 2022-11-28' \
--url "https://api.github.com/$level/$runner_scope/actions/runners/registration-token" | jq .token)
# Output the registration token and runner scope
echo "registration_token=$registration_token" >> $GITHUB_OUTPUT
echo "runner_scope=$runner_scope" >> $GITHUB_OUTPUT
shell: bash
id: get-registration
- name: Create Device
uses: equinix-labs/metal-device-action@main
with:
metal_auth_token: ${{ inputs.metal_auth_token }}
metal_project_id: ${{ inputs.metal_project_id }}
metro: ${{ inputs.metro }}
plan: ${{ inputs.plan }}
os: ${{ inputs.os }}
provisioning_timeout: ${{ inputs.provisioning_timeout }}
user_data: |
#!/bin/bash
set -e # Exit immediately if any command exits with a non-zero status
# Install necessary packages based on OS
if command -v yum &> /dev/null; then
yum install -y curl jq libicu gcc
else
export DEBIAN_FRONTEND=noninteractive
apt-get -qy update
apt-get -qy install build-essential gcc jq
fi
# Create a user for runner
useradd -m ghrunner -s /bin/bash
cd /home/ghrunner
mkdir runner
# Download and install GitHub actions runner
VERSION=$(curl -fsSL https://api.github.com/repos/actions/runner/releases/latest | jq -r .tag_name | sed 's/^v//')
curl -o actions-runner-linux-x64-${VERSION}.tar.gz -fsSL https://github.com/actions/runner/releases/download/v${VERSION}/actions-runner-linux-x64-${VERSION}.tar.gz
tar xzf ./actions-runner-linux-x64-${VERSION}.tar.gz -C runner
chown -R ghrunner ./runner
cd runner
# Configure the runner
sudo -E -u ghrunner ./config.sh --unattended --url https://github.com/${{ steps.get-registration.outputs.runner_scope }} \
--token ${{ steps.get-registration.outputs.registration_token }} --name $(hostname) --work /home/ghrunner \
--labels ${{ steps.get-registration.outputs.runner_scope }} --ephemeral
# Running custom script if provided
CUSTOM_SCRIPT=$(mktemp $HOME/custom_script.XXXXXX.sh)
cat > $CUSTOM_SCRIPT <<'CUSTOM_SCRIPT_EOF'
${{ inputs.custom_script }}
CUSTOM_SCRIPT_EOF
if [[ -s $CUSTOM_SCRIPT ]]; then
chmod 0700 $CUSTOM_SCRIPT
echo "Running custom user script"
$CUSTOM_SCRIPT
else
rm $CUSTOM_SCRIPT
fi
# Install and start the runner service
./svc.sh install
./svc.sh start