-
Notifications
You must be signed in to change notification settings - Fork 45
167 lines (158 loc) · 6.26 KB
/
guide-notebooks-ec2.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
name: Guide Notebooks Regression - EC2
run-name: Guide Notebooks Regression - EC2
on: [push]
jobs:
start_ec2_runner:
runs-on: ubuntu-latest
env:
AWS_KEY_ID: ${{ secrets.AWS_KEY_ID }}
AWS_KEY_SECRET: ${{ secrets.AWS_KEY_SECRET }}
AWS_GH_RUNNER_KEY: ${{ secrets.AWS_GH_RUNNER_KEY }}
GH_TOKEN: ${{ secrets.GH_TOKEN }}
AWS_INSTANCE_TYPE: ${{ vars.AWS_INSTANCE_TYPE }}
AWS_IMAGE_ID: ${{ vars.AWS_IMAGE_ID }}
AWS_SECURITY_GROUP: ${{ vars.AWS_SECURITY_GROUP }}
outputs:
EC2_INSTANCE_ID: ${{ steps.create_instance.outputs.EC2_INSTANCE_ID }}
EC2_PUBLIC_DNS: ${{ steps.create_instance.outputs.EC2_PUBLIC_DNS }}
RUNS_ON: ${{ steps.create_instance.outputs.RUNS_ON }}
steps:
- name: configure_aws
run: |
set -xe
sudo apt update
sudo apt install -y awscli jq
mkdir ${HOME}/.aws
echo "[default]" > ${HOME}/.aws/credentials
echo "aws_access_key_id = ${AWS_KEY_ID}" >> ${HOME}/.aws/credentials
echo "aws_secret_access_key = ${AWS_KEY_SECRET}" >> ${HOME}/.aws/credentials
echo "region = us-east-1" >> ${HOME}/.aws/credentials
cat ${HOME}/.aws/credentials
- name: create_instance
id: create_instance
run: |
set -xe
output=$(aws ec2 run-instances \
--instance-type ${AWS_INSTANCE_TYPE} \
--image-id ${AWS_IMAGE_ID} \
--security-group-ids ${AWS_SECURITY_GROUP} \
--key-name github_runner \
--count 1)
EC2_INSTANCE_ID=$(echo ${output} | jq -r ".Instances[0].InstanceId")
EC2_INSTANCE_STATE=''
while [ "${EC2_INSTANCE_STATE}" != "running" ]; do
EC2_INSTANCE_STATE=$(aws ec2 describe-instances --instance-ids ${EC2_INSTANCE_ID} | jq -r ".Reservations[0].Instances[0].State.Name")
sleep 1
done
# hack to make sure sshd comes up
sleep 120
EC2_PUBLIC_DNS=$(aws ec2 describe-instances \
--instance-ids ${EC2_INSTANCE_ID} \
| jq -r ".Reservations[0].Instances[0].PublicDnsName")
echo "EC2_INSTANCE_ID=${EC2_INSTANCE_ID}" >> "$GITHUB_OUTPUT"
echo "RUNS_ON=[self-hosted, ${EC2_INSTANCE_ID}]" >> "$GITHUB_OUTPUT"
echo "EC2_PUBLIC_DNS=${EC2_PUBLIC_DNS}" >> "$GITHUB_OUTPUT"
echo "EC2_PUBLIC_DNS=${EC2_PUBLIC_DNS}" >> "$GITHUB_ENV"
- name: install ssh key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.AWS_GH_RUNNER_KEY }}
known_hosts: 'none'
- name: clone
uses: actions/checkout@v3
- name: register_runner
id: register_runner
run: |
set -xe
echo ${GH_TOKEN} > gh_token
scp -o StrictHostKeyChecking=accept-new gh_token ubuntu@${EC2_PUBLIC_DNS}:gh_token
scp .github/workflows/provision_runner.sh ubuntu@${EC2_PUBLIC_DNS}:provision_runner.sh
ssh ubuntu@${EC2_PUBLIC_DNS} bash provision_runner.sh
guide_notebooks_regression_ec2:
runs-on: ${{ needs.start_ec2_runner.outputs.RUNS_ON }}
needs: start_ec2_runner
steps:
- name: verify-instance
env:
EC2_INSTANCE_ID: ${{ needs.start_ec2_runner.outputs.EC2_INSTANCE_ID }}
run: |
set -xe
if [[ $(ec2metadata --instance-id) != ${EC2_INSTANCE_ID} ]]; then
echo "Running on the wrong instance! Bailing, but try rerunning"
exit 1
fi
- name: clone
uses: actions/checkout@v3
- name: install
run: |
set -xe
cd ${{ github.workspace }}
python -m venv env
source env/bin/activate
pip install --upgrade pip
pip install matplotlib nilearn
pip install -e .
nobrainer info
- name: run
run: |
set -xe
cd ${{ github.workspace }}
git clone https://github.com/neuronets/nobrainer-book.git
cd nobrainer-book
# if there is a matching book branch, switch to it
if [ $(git ls-remote --heads https://github.com/neuronets/nobrainer-book.git ${{ github.ref_name }} | wc -l) -ne 0 ]; then
echo "Checking out branch ${{ github.ref_name }}"
git checkout ${{ github.ref_name }};
else
echo "No matching branch found, sticking with the default"
fi
cd ${{ github.workspace }}
source env/bin/activate
for notebook_script in $(ls nobrainer-book/docs/nobrainer-guides/scripts/*.py); do
echo "running ${notebook_script}"
python ${notebook_script}
done
stop_ec2_runner:
if: always()
runs-on: ubuntu-latest
needs: [start_ec2_runner, guide_notebooks_regression_ec2]
env:
AWS_KEY_ID: ${{ secrets.AWS_KEY_ID }}
AWS_KEY_SECRET: ${{ secrets.AWS_KEY_SECRET }}
GH_TOKEN: ${{ secrets.GH_TOKEN }}
steps:
- name: configure_aws
run: |
set -xe
sudo apt update
sudo apt install -y awscli
mkdir ${HOME}/.aws
echo "[default]" > ${HOME}/.aws/credentials
echo "aws_access_key_id = ${AWS_KEY_ID}" >> ${HOME}/.aws/credentials
echo "aws_secret_access_key = ${AWS_KEY_SECRET}" >> ${HOME}/.aws/credentials
echo "region = us-east-1" >> ${HOME}/.aws/credentials
cat ${HOME}/.aws/credentials
- name: install ssh key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.AWS_GH_RUNNER_KEY }}
known_hosts: 'none'
- name: clone
uses: actions/checkout@v3
- name: deregister_runner
id: deregister_runner
env:
EC2_PUBLIC_DNS: ${{ needs.start_ec2_runner.outputs.EC2_PUBLIC_DNS }}
run: |
set -xe
echo ${GH_TOKEN} > gh_token
scp -o StrictHostKeyChecking=accept-new gh_token ubuntu@${EC2_PUBLIC_DNS}:gh_token
scp .github/workflows/destroy_runner.sh ubuntu@${EC2_PUBLIC_DNS}:destroy_runner.sh
ssh ubuntu@${EC2_PUBLIC_DNS} bash destroy_runner.sh
- name: stop_runner
if: always()
env:
EC2_INSTANCE_ID: ${{ needs.start_ec2_runner.outputs.EC2_INSTANCE_ID }}
run: |
set -xe
aws ec2 terminate-instances --instance-ids ${EC2_INSTANCE_ID}