forked from newrelic/newrelic-php-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
213 lines (193 loc) · 7.75 KB
/
Copy pathcreate-runner.yml
File metadata and controls
213 lines (193 loc) · 7.75 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# This is a basic workflow to help you get started with Actions
name: CI Create Runner
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
# push:
# branches: [ "main" ]
# pull_request:
# branches: [ "main" ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
inputs:
IMAGE_ID:
description: "The AWS AMI ID"
required: false
default: false
INSTANCE_TYPE:
description: "The type of EC2 instance desired"
required: false
default: false
KEY_NAME:
description: "The PEM file name"
required: false
default: ""
USER_DATA_FILE:
description: "Filename to pass to --user-data"
required: false
default: 'user_data.sh'
REPO_ORG:
description: "GitHUb Organization"
required: false
default: 'My-Cirrus-CI'
REPO_NAME:
description: "Repository Name"
required: false
default: 'aws'
workflow_call:
inputs:
IMAGE_ID:
description: "The AWS AMI ID"
required: false
default: false
type: string
INSTANCE_TYPE:
description: "The type of EC2 instance desired"
required: false
default: false
type: string
KEY_NAME:
description: "The PEM file name"
required: false
default: ""
type: string
USER_DATA_FILE:
description: "Filename to pass to --user-data"
required: false
default: 'user_data.sh'
type: string
REPO_ORG:
description: "GitHUb Organization"
required: false
default: 'My-Cirrus-CI'
type: string
REPO_NAME:
description: "Repository Name"
required: false
default: 'aws'
type: string
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
create-runner:
env:
PEM_FILE: ${{ secrets.MYAWSCITESTING }}
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3
# Authenticate with AWS
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_DEFAULT_REGION }}
# Version check
- name: Version check
run: |
aws --version
- name: API test with token
env:
GH_TOKEN: ${{ secrets.GH_TOKEN}}
OWNER: ${{ inputs.REPO_ORG }}
REPO: ${{ inputs.REPO_NAME }}
run: |
echo "OWNER is equal to ${OWNER}"
echo "REPO is equal to ${REPO}"
# GitHub CLI api
# https://cli.github.com/manual/gh_api
gh api \
-H "Accept: application/vnd.github+json" \
/repos/"${OWNER}"/"${REPO}"/actions/runners
# Add required variables to user_data
- name: Add required variables to user_data
run: |
sed -i "/^# Secrets.*/a export RUNNER_NAME=${{ secrets.RUNNER_NAME }}" ec2/run-instances/${{ inputs.USER_DATA_FILE }}
sed -i "/^# Secrets.*/a export GH_TOKEN=${{ secrets.GH_TOKEN }}" ec2/run-instances/${{ inputs.USER_DATA_FILE }}
sed -i "/^# Secrets.*/a export OWNER=${{ inputs.REPO_ORG }}" ec2/run-instances/${{ inputs.USER_DATA_FILE }}
sed -i "/^# Secrets.*/a export REPO=${{ inputs.REPO_NAME }}" ec2/run-instances/${{ inputs.USER_DATA_FILE }}
cat ec2/run-instances/${{ inputs.USER_DATA_FILE }}
# Create the EC2 instance from the appropriate AMI
# GHA: This is my personal AMI, this will be possibly shared back to NR
- name: Create the EC2 instance from the appropriate AMI
if: ${{ success() }}
run: |
aws ec2 run-instances \
--image-id ${{ inputs.IMAGE_ID || secrets.AWS_AMI_ID }} \
--count 1 --instance-type ${{ inputs.INSTANCE_TYPE || secrets.AWS_INSTANCE_TYPE }} \
--security-group-ids ${{ secrets.AWS_SEC_GROUP }} \
--subnet-id ${{ secrets.AWS_SUBNET }} \
--tag-specifications 'ResourceType=instance,Tags=[{Key=runner,Value=true}]' \
--user-data file://ec2/run-instances/${{ inputs.USER_DATA_FILE }}
# Get the instance ID once the instance is created
- name: Get the instance ID once the instance is created
if: ${{ success() }}
run: |
# Sleep for now for testing
sleep 10
RUNNER_ID=$(aws ec2 describe-instances --filter Name=tag:runner,Values=true Name=instance-state-code,Values=16 --query Reservations[*].Instances[*].[InstanceId] --output text)
while [[ $RUNNER_ID != *"i-"* ]]; do
RUNNER_ID=$(aws ec2 describe-instances --filter Name=tag:runner,Values=true Name=instance-state-code,Values=16 --query Reservations[*].Instances[*].[InstanceId] --output text)
sleep 2
done
echo "${RUNNER_ID}"
echo "RUNNER_ID="${RUNNER_ID}"" >> $GITHUB_ENV
# Check ENV variables
- name: Check environmental variables
if: ${{ always() }}
run: printenv | sort -f
- name: API test with token (curl method)
env:
GH_TOKEN: ${{ secrets.GH_TOKEN}}
OWNER: ${{ inputs.REPO_ORG }}
REPO: ${{ inputs.REPO_NAME }}
run: |
echo "OWNER is equal to ${OWNER}"
echo "REPO is equal to ${REPO}"
curl \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GH_TOKEN}" \
https://api.github.com/repos/"${OWNER}"/"${REPO}"/actions/runners
- name: Check to see what runners are available
env:
GH_TOKEN: ${{ secrets.GH_TOKEN}}
OWNER: ${{ inputs.REPO_ORG }}
REPO: ${{ inputs.REPO_NAME }}
run: |
# GitHub CLI api
# https://cli.github.com/manual/gh_api
gh auth status
RUNNER_INFO=$(curl \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN" \
https://api.github.com/repos/"$OWNER"/"$REPO"/actions/runners)
# Return number of runners configured for the repo
RUNNERS=$(echo "${RUNNER_INFO}" | jq -r '.total_count')
echo "Runners = ${RUNNERS}"
while [[ "$RUNNERS" = "0" ]]; do
echo "No runners configured (yet)"
RUNNER_INFO=$(curl \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN" \
https://api.github.com/repos/"$OWNER"/"$REPO"/actions/runners)
RUNNERS=$(echo "${RUNNER_INFO}" | jq -r '.total_count')
sleep 15
done
RUNNER_STATUS=$(echo "${RUNNER_INFO}" | jq 'select(objects)|=[.]' | grep 'status')
while [[ $RUNNER_STATUS != *"online"* ]]; do
RUNNER_INFO=$(curl \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN" \
https://api.github.com/repos/"$OWNER"/"$REPO"/actions/runners)
RUNNER_STATUS=$(echo "${RUNNER_INFO}" | jq 'select(objects)|=[.]' | grep 'status')
if [[ -z "$RUNNER_STATUS" ]]; then
RUNNER_STATUS="BUIDING"
echo "Runner Status is - ${RUNNER_STATUS}"
fi
echo "Runner is still offline"
sleep 30
done
echo "Runner is ready!"