Test & Publish #88
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Test & Publish | |
on: | |
push: | |
branches: [main] | |
workflow_dispatch: | |
inputs: | |
environment: | |
type: environment | |
description: The environment to deploy to. | |
jobs: | |
detect-environments: | |
runs-on: ubuntu-latest | |
outputs: | |
environments: ${{ steps.environments.outputs.result }} | |
steps: | |
- uses: actions/github-script@v6 | |
id: environments | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
result-encoding: json | |
script: | | |
if (context.payload?.inputs?.environment) return [context.payload?.inputs?.environment]; | |
const {data: {environments}} = | |
await github.request(`GET /repos/${process.env.GITHUB_REPOSITORY}/environments`); | |
return environments.map(e => e.name) | |
test-request-api: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Install test dependencies with pip | |
working-directory: ./request-api | |
run: | | |
pip install -r test_requirements.txt | |
- name: Run Lint and Tests | |
working-directory: ./request-api | |
run: make test | |
publish-request-api: | |
runs-on: ubuntu-latest | |
needs: [detect-environments, test-request-api] | |
strategy: | |
matrix: | |
environment: ${{ fromJSON(needs.detect-environments.outputs.environments) }} | |
environment: ${{ matrix.environment }} | |
env: | |
DOCKER_REPO: ${{ secrets.DEPLOY_DOCKER_REPOSITORY_REQUEST_API }} | |
steps: | |
- uses: actions/checkout@v4 | |
- id: vars | |
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | |
- run: | | |
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" | |
unzip -q awscliv2.zip | |
sudo ./aws/install --update | |
sudo apt-get update | |
sudo apt-get install -y rsync | |
- uses: aws-actions/configure-aws-credentials@v1-node16 | |
with: | |
aws-access-key-id: ${{ secrets.DEPLOY_AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.DEPLOY_AWS_SECRET_ACCESS_KEY }} | |
aws-region: us-east-1 | |
- uses: aws-actions/amazon-ecr-login@v2 | |
with: | |
registry-type: public | |
- run: docker pull $DOCKER_REPO:${GITHUB_REF_NAME} || echo "no current latest image" | |
- run: docker build --build-arg DEPLOY_TIME="$(date +%Y-%m-%dT%H:%M:%S)" -t $DOCKER_REPO:${{ steps.vars.outputs.sha_short }} -f request-api/Dockerfile . | |
- run: docker tag $DOCKER_REPO:${{ steps.vars.outputs.sha_short }} $DOCKER_REPO:${GITHUB_REF_NAME} | |
- run: docker push $DOCKER_REPO:${{ steps.vars.outputs.sha_short }} | |
- run: docker push $DOCKER_REPO:${GITHUB_REF_NAME} | |
- uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.DEPLOY_AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.DEPLOY_AWS_SECRET_ACCESS_KEY }} | |
aws-region: eu-west-2 | |
- name: Update ECS Service | |
run: | | |
aws ecs update-service --cluster ${{ matrix.environment }}-cluster --service ${{ matrix.environment }}-pub-async-request-api --task-definition ${{ matrix.environment }}-pub-async-request-api-task --force-new-deployment | |
test-request-processor: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Install GDAL binary package | |
run: | | |
sudo apt-get update | |
sudo apt-get install gdal-bin | |
- uses: actions/checkout@v4 | |
- name: Install test dependencies with pip | |
working-directory: ./request-processor-celery | |
run: | | |
pip install -r test_requirements.txt | |
- name: Download specification files | |
working-directory: ./request-processor-celery | |
run: make specification | |
- name: Lint, Test, Coverage | |
working-directory: ./request-processor-celery | |
run: make all | |
publish-request-processor: | |
runs-on: ubuntu-latest | |
needs: [detect-environments, test-request-processor] | |
strategy: | |
matrix: | |
environment: ${{ fromJSON(needs.detect-environments.outputs.environments) }} | |
environment: ${{ matrix.environment }} | |
env: | |
DOCKER_REPO: ${{ secrets.DEPLOY_DOCKER_REPOSITORY_REQUEST_PROCESSOR }} | |
steps: | |
- uses: actions/checkout@v4 | |
- id: vars | |
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | |
- run: | | |
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" | |
unzip -q awscliv2.zip | |
sudo ./aws/install --update | |
sudo apt-get update | |
sudo apt-get install -y rsync | |
- uses: aws-actions/configure-aws-credentials@v1-node16 | |
with: | |
aws-access-key-id: ${{ secrets.DEPLOY_AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.DEPLOY_AWS_SECRET_ACCESS_KEY }} | |
aws-region: us-east-1 | |
- uses: aws-actions/amazon-ecr-login@v2 | |
with: | |
registry-type: public | |
- run: docker pull $DOCKER_REPO:${GITHUB_REF_NAME} || echo "no current latest image" | |
- run: docker build --build-arg DEPLOY_TIME="$(date +%Y-%m-%dT%H:%M:%S)" -t $DOCKER_REPO:${{ steps.vars.outputs.sha_short }} -f request-processor-celery/Dockerfile . | |
- run: docker tag $DOCKER_REPO:${{ steps.vars.outputs.sha_short }} $DOCKER_REPO:${GITHUB_REF_NAME} | |
- run: docker push $DOCKER_REPO:${{ steps.vars.outputs.sha_short }} | |
- run: docker push $DOCKER_REPO:${GITHUB_REF_NAME} | |
- uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.DEPLOY_AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.DEPLOY_AWS_SECRET_ACCESS_KEY }} | |
aws-region: eu-west-2 | |
- name: List ECS Tasks | |
run: | | |
TASK_ARNS=$(aws ecs list-tasks --cluster ${{ matrix.environment }}-cluster --family ${{ matrix.environment }}-pub-async-request-processor-task --output text --query 'taskArns[]') | |
echo "TASK_ARNS=$TASK_ARNS" >> $GITHUB_ENV | |
- name: Stop ECS Tasks | |
if: env.TASK_ARNS | |
run: | | |
for TASK_ARN in $TASK_ARNS | |
do | |
aws ecs stop-task --cluster ${{ matrix.environment }}-cluster --task $TASK_ARN | |
done | |
- name: Update ECS Task | |
run: | | |
aws ecs run-task --cluster ${{ matrix.environment }}-cluster --task-definition ${{ matrix.environment }}-pub-async-request-processor-task --count 2 --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets=[$SUBNET_PRIVATE_IDS],securityGroups=[$SECURITY_GROUP_PUBLIC_ID],assignPublicIp=ENABLED}" | |