2024-05-03 19:40:44 -07:00
#!/usr/bin/env bash
# @file GitHub Runner Registration
# @brief Registers a GitHub action runner with GitHub
# @description
# This script registers the host as a self-hosted GitHub runner with scope set
# in the `.user.github.runnerOrg` input in the `.chezmoi.yaml.tmpl` file. If your organization is `megabyte-labs`, then
# the value of `.user.github.runnerOrg` should be `megabyte-labs`. A self-hosted runner is an application
# that that allows you to run tasks from GitHub CI.
#
# This script adds 3 labels to the runner: self-hosted, _hostname_, and _operating-system family_.
#
# The script automatically acquires the GitHub Action runner token (as long as you specify your `.user.github.runnerOrg` value in `.chezmoi.yaml.tmpl`).
# In order to authenticate with GitHub, you should have the `GITHUB_TOKEN` environment variable in place with the appropriate permissions
# specified when you generate the token.
#
# ## Links
#
# * [Secrets / Environment variables documentation](https://install.doctor/docs/customization/secrets)
2024-05-27 20:45:14 -07:00
set -Eeuo pipefail
2024-05-27 20:50:11 -07:00
trap "gum log -sl error 'Script encountered an error!'" ERR
2024-05-27 04:15:03 -07:00
2024-05-03 19:40:44 -07:00
### Check if GitHub runner is installed
2024-05-27 20:45:14 -07:00
if [ -f " ${ XDG_DATA_HOME :- $HOME /.local/share } /github-runner/config.sh " ] ; then
2024-05-03 19:40:44 -07:00
if [ -f " ${ XDG_DATA_HOME :- $HOME /.local/share } /github-runner/.runner " ] ; then
2024-05-27 20:50:11 -07:00
gum log -sl info " GitHub Actions runner is already configured ( ${ XDG_DATA_HOME :- $HOME /.local/share } /github-runner/.runner file is present) "
2024-05-03 19:40:44 -07:00
else
2024-05-27 20:50:11 -07:00
gum log -sl info 'Creating runner configuration'
2024-05-27 20:45:14 -07:00
### Configure host labels
2024-05-03 19:40:44 -07:00
HOST_DISTRO_FAMILY = " $( yq '.data.host.distro.family' " ${ XDG_CONFIG_HOME :- $HOME /.config } /chezmoi/chezmoi.yaml " ) "
HOST_DISTRO_ID = " $( yq '.data.host.distro.id' " ${ XDG_CONFIG_HOME :- $HOME /.config } /chezmoi/chezmoi.yaml " ) "
LABELS = " self-hosted, $( yq '.data.host.hostname' " ${ XDG_CONFIG_HOME :- $HOME /.config } /chezmoi/chezmoi.yaml " ) , $HOST_DISTRO_FAMILY "
if [ " $HOST_DISTRO_FAMILY " != " $HOST_DISTRO_ID " ] ; then
LABELS = " ${ LABELS } , $HOST_DISTRO_ID "
fi
2024-05-27 20:45:14 -07:00
### Add VirtualBox label (if installed)
2024-05-03 19:40:44 -07:00
if command -v VirtualBox > /dev/null; then
LABELS = " ${ LABELS } ,virtualbox "
fi
2024-05-27 20:45:14 -07:00
### Add Docker label (if installed)
2024-05-03 19:40:44 -07:00
if command -v docker > /dev/null; then
LABELS = " ${ LABELS } ,docker "
fi
2024-05-27 20:45:14 -07:00
if get-secret --exists GITHUB_TOKEN; then
2024-05-03 19:40:44 -07:00
if command -v jq > /dev/null; then
### Acquire token
2024-05-27 20:50:11 -07:00
gum log -sl info 'Acquiring runner token'
2024-05-27 04:15:03 -07:00
RUNNER_ORG = " $( yq '.data.user.github.runnerOrg' " ${ XDG_CONFIG_HOME :- $HOME /.config } /chezmoi/chezmoi.yaml " ) "
2024-05-27 20:45:14 -07:00
RUNNER_TOKEN = " $( curl -sSL -X POST -H "Accept: application/vnd.github+json" -H " Authorization: Bearer $( get-secret GITHUB_TOKEN) " -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/orgs/${ RUNNER_ORG } /actions/runners/registration-token | jq -r '.token' ) "
2024-05-03 19:40:44 -07:00
### Generate the configuration
2024-05-27 20:50:11 -07:00
gum log -sl info " Joining GitHub runner to https://github.com/ ${ RUNNER_ORG } "
2024-05-27 04:15:03 -07:00
" ${ XDG_DATA_HOME :- $HOME /.local/share } /github-runner/config.sh " --unattended --url https://github.com/${ RUNNER_ORG } --token " $RUNNER_TOKEN " --labels " $LABELS " || EXIT_CODE = $?
2024-05-27 20:45:14 -07:00
if [ -n " ${ EXIT_CODE :- } " ] ; then
2024-05-27 20:50:11 -07:00
gum log -sl error 'GitHub runner configuration failed' && exit 1
2024-05-03 19:40:44 -07:00
fi
2024-05-27 20:45:14 -07:00
2024-05-03 19:40:44 -07:00
### Install / start the service
2024-05-27 20:50:11 -07:00
gum log -sl info 'Configuring runner service'
2024-05-27 20:45:14 -07:00
" ${ XDG_DATA_HOME :- $HOME /.local/share } /github-runner/svc.sh " install
2024-05-27 23:55:42 -07:00
gum log -sl info 'Successfully installed the GitHub Actions runner service'
2024-05-27 20:50:11 -07:00
gum log -sl info 'Starting runner service'
2024-05-27 20:45:14 -07:00
" ${ XDG_DATA_HOME :- $HOME /.local/share } /github-runner/svc.sh " start
2024-05-27 23:55:42 -07:00
gum log -sl info 'Started the GitHub Actions runner service'
2024-05-03 19:40:44 -07:00
else
2024-05-27 20:50:11 -07:00
gum log -sl warn 'jq is required by the GitHub runner configuration script'
2024-05-03 19:40:44 -07:00
fi
else
2024-05-27 20:50:11 -07:00
gum log -sl warn 'The GITHUB_TOKEN environment variable is not present'
2024-05-03 19:40:44 -07:00
fi
fi
else
2024-05-27 20:50:11 -07:00
gum log -sl info " The GitHub Actions runner installation is not present at ${ XDG_DATA_HOME :- $HOME /.local/share } /github-runner "
2024-05-03 19:40:44 -07:00
fi