-
Notifications
You must be signed in to change notification settings - Fork 15
177 lines (156 loc) · 6.16 KB
/
setup-ceremony.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
168
169
170
171
172
173
174
175
176
177
name: Setup ceremony
# this will only run on pushes to main/staing/dev
on:
push:
branches: [ main, staging, dev ]
jobs:
# first we start a custom runner
start-runner:
if: github.ref != 'refs/heads/dev'
name: Start self-hosted EC2 runner
runs-on: ubuntu-latest
environment:
${{ (github.ref == 'refs/heads/main' && 'p0tion-production') ||
(github.ref == 'refs/heads/staging' && 'p0tion-staging') }}
outputs:
label: ${{ steps.start-ec2-runner.outputs.label }}
ec2-instance-id: ${{ steps.start-ec2-runner.outputs.ec2-instance-id }}
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Start EC2 runner
id: start-ec2-runner
uses: machulav/ec2-github-runner@v2
with:
mode: start
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
ec2-image-id: ami-0d65ed0872506990c
ec2-instance-type: t3.2xlarge
subnet-id: subnet-0817be1b2160793b5
security-group-id: sg-0aea3cbb15e30a921
aws-resource-tags: >
[
{ "Key": "Name", "Value": "p0tion-github-runner" },
{ "Key": "GitHubRepository", "Value": "${{ github.repository }}" }
]
# then we setup the ceremony
setup-ceremonies:
name: Setup ceremony using vm-runner
needs: start-runner
if: github.ref != 'refs/heads/dev'
runs-on: ${{ needs.start-runner.outputs.label }} # run the job on the newly created runner
environment:
${{ (github.ref == 'refs/heads/main' && 'p0tion-production') ||
(github.ref == 'refs/heads/staging' && 'p0tion-staging') }}
steps:
- uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
# install p0tion
- name: Install p0tion globally
run: npm install -g @p0tion/phase2cli
# write env to file
- name: Write env locally
run: |
echo "${{ secrets.PHASE2CLI_ENV_FILE }}" > ./.env
# List ceremonies that have already been created
- name: List existing ceremonies
id: list_ceremonies
run: |
echo "$(phase2cli list)" > existing_ceremonies.txt
cat existing_ceremonies.txt
# List all the ceremonies in ./ceremonies
- name: List all ceremonies
id: list_all_ceremonies
run: |
echo "$(ls -d ceremonies/* | grep -v 'README.md' | cut -d'/' -f2)" > dir_output.txt
cat dir_output.txt
# want to setup only ceremonies that have not been setup already
- name: Run p0tion and setup ceremony
run: |
IFS=',' read -ra EXISTING_CEREMONIES <<< $(cat existing_ceremonies.txt)
ALL_CEREMONIES=()
while IFS= read -r line; do
ALL_CEREMONIES+=("$line")
done < dir_output.txt
for ceremony in "${ALL_CEREMONIES[@]}"; do
if [[ ! " ${EXISTING_CEREMONIES[@]} " =~ " ${ceremony} " ]]; then
echo 'Setting up ceremony: ./ceremonies/$ceremony/p0tionConfig.json'
NODE_OPTIONS=--max-old-space-size=12288 phase2cli coordinate setup --template "./ceremonies/$ceremony/p0tionConfig.json" --auth "${{ secrets.ACCESS_TOKEN }}"
fi
done
# on dev do not use runner
setup-ceremonies-dev:
name: Setup ceremony without vm-runner
if: github.ref == 'refs/heads/dev'
runs-on: ubuntu-latest
environment: 'p0tion-development'
steps:
- uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
# install p0tion
- name: Install p0tion globally
run: npm install -g @p0tion/phase2cli
# write env to file
- name: Write env locally
run: |
echo "${{ secrets.PHASE2CLI_ENV_FILE }}" > ./.env
# List ceremonies that have already been created
- name: List existing ceremonies
id: list_ceremonies
run: |
echo "$(phase2cli list)" > existing_ceremonies.txt
cat existing_ceremonies.txt
# List all the ceremonies in ./ceremonies
- name: List all ceremonies
id: list_all_ceremonies
run: |
echo "$(ls -d ceremonies/* | grep -v 'README.md' | cut -d'/' -f2)" > dir_output.txt
cat dir_output.txt
# want to setup only ceremonies that have not been setup already
- name: Run p0tion and setup ceremony
run: |
IFS=',' read -ra EXISTING_CEREMONIES <<< $(cat existing_ceremonies.txt)
ALL_CEREMONIES=()
while IFS= read -r line; do
ALL_CEREMONIES+=("$line")
done < dir_output.txt
for ceremony in "${ALL_CEREMONIES[@]}"; do
if [[ ! " ${EXISTING_CEREMONIES[@]} " =~ " ${ceremony} " ]]; then
echo 'Setting up ceremony: ./ceremonies/$ceremony/p0tionConfig.json'
phase2cli coordinate setup --template "./ceremonies/$ceremony/p0tionConfig.json" --auth "${{ secrets.ACCESS_TOKEN }}"
fi
done
# after everything, make sure we stop the runner
# do not set environment so there is no need to approve to stop the runner
stop-runner:
name: Stop self-hosted EC2 runner
# run if previous failed and if not on dev branch
if: ${{ always() && github.ref != 'refs/heads/dev' }}
needs:
- start-runner # required to get output from the start-runner job
- setup-ceremonies # required to wait when the main job is done
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Stop EC2 runner
uses: machulav/ec2-github-runner@v2
with:
mode: stop
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
label: ${{ needs.start-runner.outputs.label }}
ec2-instance-id: ${{ needs.start-runner.outputs.ec2-instance-id }}