1+ #! /bin/bash
2+
3+ # Copyright 2022 The JAX Authors.
4+ #
5+ # Licensed under the Apache License, Version 2.0 (the "License");
6+ # you may not use this file except in compliance with the License.
7+ # You may obtain a copy of the License at
8+ #
9+ # https://www.apache.org/licenses/LICENSE-2.0
10+ #
11+ # Unless required by applicable law or agreed to in writing, software
12+ # distributed under the License is distributed on an "AS IS" BASIS,
13+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ # See the License for the specific language governing permissions and
15+ # limitations under the License.
16+
17+ # Heavily influenced by
18+ # https://github.com/openxla/iree/tree/main/build_tools/github_actions/runner/config
19+
20+ # This file sets up a tpu vm to be used as a github runner for testing.
21+ # It creates a user runner without sudo permissions to
22+ # run the config file and authenticate to github
23+
24+
25+ set -eux
26+
27+ if [ " $# " -ne 4 ]; then
28+ echo " Usage: setup_runner.sh <runner name> <tags> <github token> <repo_url>"
29+ fi
30+
31+ runner_name=" $1 "
32+ runner_tags=" $2 "
33+ runner_token=" $3 "
34+ repo_url=" $4 "
35+
36+
37+ # Create `runner` user. This user won't have sudo access unless you ssh into the
38+ # GCP VM as `runner` using gcloud. Don't do that!
39+ sudo useradd runner -m
40+ ORIGIN_USER=${USER}
41+ # Find the latest actions-runner download. The runner will automatically update
42+ # itself when new versions are released. Github requires that all self-hosted
43+ # runners are updated to the latest version within 30 days of release
44+ # (https://docs.github.com/en/actions/hosting-your-own-runners/autoscaling-with-self-hosted-runners#controlling-runner-software-updates-on-self-hosted-runners).
45+ # Example URL:
46+ # https://github.com/actions/runner/releases/download/v2.298.2/actions-runner-linux-x64-2.298.2.tar.gz
47+ actions_runner_download_regexp=' https://github.com/actions/runner/releases/' \
48+ ' download/v[0-9.]\+/actions-runner-linux-x64-[0-9.]\+\.tar\.gz'
49+ # Use `head -n 1` because there are multiple instances of the same URL
50+ actions_runner_download=$(
51+ curl -s -X GET ' https://api.github.com/repos/actions/runner/releases/latest' |
52+ grep -o " ${actions_runner_download_regexp} " |
53+ head -n 1)
54+ echo " actions_runner_download: ${actions_runner_download} "
55+
56+ # Run the rest of the setup as `runner`.
57+ #
58+ # Note that env vars in the heredoc will be expanded according to the _calling_
59+ # environment, not the `runner` environment we're creating -- it acts like a
60+ # double-quoted string. This is how variables like $runner_name are inserted
61+ # without using sudo -E (which would cause the current environment to be
62+ # inherited). This also means we must be careful to escape any variables that
63+ # we'd like to evaluate in the `runner` environment, e.g. $HOME.
64+ sudo -i -u runner bash -ex << EOF
65+ cd ~/
66+
67+ mkdir actions-runner && cd actions-runner
68+ curl -o actions-runner-linux-x64.tar.gz -L ${actions_runner_download}
69+ tar xzf ./actions-runner-linux-x64.tar.gz
70+ # Register the runner with Github
71+ ./config.sh --unattended \
72+ --url ${repo_url} \
73+ --labels ${runner_tags} \
74+ --token ${runner_token} \
75+ --name ${runner_name}
76+
77+ EOF
0 commit comments