-
Notifications
You must be signed in to change notification settings - Fork 1
132 lines (111 loc) · 4.43 KB
/
repo.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
name: Repo
on: push
env:
NODE_VERSION: 18.x
AWS_DEFAULT_REGION: us-east-1
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
CLUSTER_NAME: formio-gh-runner
TASK_DEF_NAME: task-defintion-gh-actions
## Jobs
jobs:
runner:
if: true
runs-on: ubuntu-latest
steps:
- name: Install awscli
run: |
ls ${{ github.workspace }}
sudo apt-get update
sudo apt install -y awscli
- name: Check for existing runner
run: |
# Check if a task for the given Task Definition is already running in the specified Cluster
EXISTING_TASKS=$(aws ecs list-tasks --region $AWS_DEFAULT_REGION --cluster $CLUSTER_NAME --family $TASK_DEF_NAME --query 'taskArns' --output text)
if [ ! -z "$EXISTING_TASKS" ]; then
echo "A task for the Task Definition: $TASK_DEF_NAME is already running in the Cluster: $CLUSTER_NAME."
else
# Fetch the first available subnet in the region
SUBNETS=$(aws ec2 describe-subnets --region $AWS_DEFAULT_REGION --query 'Subnets[0].SubnetId' --output text)
# Optionally, fetch the first available security group in the region
SEC_GROUPS=$(aws ec2 describe-security-groups --region $AWS_DEFAULT_REGION --query 'SecurityGroups[0].GroupId' --output text)
echo "Using Subnet: $SUBNETS"
echo "Using Security Group: $SEC_GROUPS"
aws ecs run-task \
--region $AWS_DEFAULT_REGION \
--cluster $CLUSTER_NAME \
--task-definition $TASK_DEF_NAME \
--launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[$SUBNETS],securityGroups=[$SEC_GROUPS],assignPublicIp=ENABLED}"
sleep 40
fi
setup:
needs: runner
runs-on: self-hosted
steps:
- run: echo "Triggered by ${{ github.event_name }} event."
- name: Check out repository code ${{ github.repository }} on ${{ github.ref }}
uses: actions/checkout@v3
- name: Set up SSH key
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_KEY }}
- name: Set up Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Cache node modules
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node-
- name: Installing dependencies
if: steps.cache.outputs.cache-hit != 'true'
uses: borales/actions-yarn@v4
with:
cmd: install --frozen-lockfile
build:
needs: setup
runs-on: self-hosted
steps:
- name: Check out repository code ${{ github.repository }} on ${{ github.ref }}
uses: actions/checkout@v3
- name: Set up Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Restore node modules from cache
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node-
- name: Build
uses: borales/actions-yarn@v4
with:
cmd: build
stop:
needs: [runner, setup, build]
runs-on: ubuntu-latest
if: always()
# if: false
steps:
- name: Stop running tasks
run: |
# List running tasks for the specific Task Definition and Cluster
TASKS=$(aws ecs list-tasks --region $AWS_DEFAULT_REGION --cluster $CLUSTER_NAME --family $TASK_DEF_NAME --query 'taskArns' --output text)
if [ -z "$TASKS" ]; then
echo "No tasks found to stop for the Task Definition: $TASK_DEF_NAME in the Cluster: $CLUSTER_NAME"
exit 1
fi
# Stop each task found for the provided Task Definition and Cluster
for TASK_ARN in $TASKS; do
echo "Stopping task: $TASK_ARN"
aws ecs stop-task --region $AWS_DEFAULT_REGION --cluster $CLUSTER_NAME --task $TASK_ARN
done
echo "All tasks have been stopped for the Task Definition: $TASK_DEF_NAME in the Cluster: $CLUSTER_NAME"