forked from stgraber/lxd-github-actions
-
Notifications
You must be signed in to change notification settings - Fork 2
/
prepare-instance
executable file
·65 lines (50 loc) · 2.35 KB
/
prepare-instance
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
#!/bin/sh
LXD_CONTAINER=$1
GH_RUNNER_VERSION="2.301.1"
CONTAINER_DEPENDENCIES="ca-certificates curl gnupg lsb-release \
python3.8-venv python3.8-dev build-essential gcc jq"
#Exit early if the required base container already exists
BASE_CONTAINER_EXISTS=$(lxc ls |grep "$LXD_CONTAINER " |awk '{print $2}' |wc -l)
if [ $BASE_CONTAINER_EXISTS -ge 1 ]
then
echo "You already have a base-container $LXD_CONTAINER \
\n If you want to Create a new base-container for a different repo, please choose a different name \
\n If you want to modify the existing base container, remove the existing container first by running: 'lxc stop $LXD_CONTAINER && lxc rm $LXD_CONTAINER'"
exit 0
fi
#Create an new ubuntu20 container
lxc launch images:ubuntu/20.04 $LXD_CONTAINER
#Install all required dependencies for your github runner
(
cat << EOF
#!/bin/sh
set -eu
sudo apt-get update
#prepare most basic dependencies
sudo apt-get install $CONTAINER_DEPENDENCIES --yes
#prepare docker and docker-compose
sudo apt-get install apt-transport-https ca-certificates gnupg-agent software-properties-common --yes
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get install docker-ce=5:20.10.22~3-0~ubuntu-focal docker-ce-cli=5:20.10.22~3-0~ubuntu-focal docker-compose-plugin containerd.io --yes
#workaround to escape $@ to prevent script parameters to be used. We want to use legacy "docker-compose" beside the new "docker compose"
echo 'docker compose "$' > /bin/docker-compose
echo '@"' > /bin/docker-compose && sed -i 's/^/docker compose "$/' /bin/docker-compose
chmod +x /bin/docker-compose
ARCH=x64
[ "\$(uname -m)" = "aarch64" ] && ARCH=arm64
curl -o /tmp/runner.tar.gz -L https://github.com/actions/runner/releases/download/v$GH_RUNNER_VERSION/actions-runner-linux-\${ARCH}-$GH_RUNNER_VERSION.tar.gz
# Create directory for the runner
rm -rf /srv/github-actions/
mkdir /srv/github-actions/
cd /srv/github-actions/
chown ubuntu:ubuntu /srv/github-actions/
# Unpack the code
sudo -u ubuntu -i tar zxf /tmp/runner.tar.gz -C /srv/github-actions/
# Add ubuntu user to docker group
sudo -su ubuntu sudo usermod -aG docker ubuntu
echo "ALL SETUP"
# Cleanup
rm /tmp/runner.tar.gz
EOF
) | lxc exec $LXD_CONTAINER /bin/sh