Skip to content

Adds cleanup job

Adds cleanup job #24

Workflow file for this run

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:
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. Exiting."
exit 1
fi
- name: Start Fargate runner
run: |
# 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}"
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: 'yarn'
- 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: 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
steps:
- name: Stop running tasks
run: |
# List running tasks for the specific Task Definition and Cluster
TASKS=$(aws ecs list-tasks --region $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 $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"