1#!/bin/bash
2
3#
4# Ephemeral runner startup script.
5#
6# Expects the following environment variables:
7#
8# - repo=<owner>/<name>
9# - access_token=<ghp_***>
10#
11
12set -e -u
13
14# Check the cached registration token.
15token_file=registration-token.json
16set +e
17expires_at=$(jq --raw-output .expires_at "$token_file" 2>/dev/null)
18status=$?
19set -e
20if [[ $status -ne 0 || $(date +%s) -ge $(date -d "$expires_at" +%s) ]]; then
21    # Refresh the cached registration token.
22    curl \
23        -X POST \
24        -H "Accept: application/vnd.github.v3+json" \
25        -H "Authorization: token $access_token" \
26        "https://api.github.com/repos/$repo/actions/runners/registration-token" \
27        -o "$token_file"
28fi
29
30# (Re-)register the runner.
31registration_token=$(jq --raw-output .token "$token_file")
32./config.sh remove --token "$registration_token" || true
33./config.sh \
34    --url "https://github.com/$repo" \
35    --token "$registration_token" \
36    --labels z15 \
37    --ephemeral
38
39# Run one job.
40./run.sh
41