Skip to content

Deploy

Deploy #142

Workflow file for this run

name: Deploy
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@v9
id: environments
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
result-encoding: json
script: |
const allowed = ['development', 'staging', 'production'];
if (context.payload?.inputs?.environment) {
return allowed.includes(context.payload.inputs.environment)
? [context.payload.inputs.environment]
: [];
}
const { data: { environments } } = await github.rest.repos.getAllEnvironments({
owner: context.repo.owner,
repo: context.repo.repo
});
return environments.map(e => e.name).filter(name => allowed.includes(name));
deploy:
runs-on: ubuntu-latest
needs: [detect-environments]
strategy:
matrix:
environment: ${{ fromJSON(needs.detect-environments.outputs.environments) }}
environment: ${{ matrix.environment }}
env:
DOCKER_REPO: ${{ secrets.DEPLOY_DOCKER_REPOSITORY }}
steps:
- uses: actions/checkout@v6
- 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@v6
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
- uses: aws-actions/amazon-ecr-login@v2
- run: docker pull $DOCKER_REPO:main || 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 }} .
- run: docker tag $DOCKER_REPO:${{ steps.vars.outputs.sha_short }} $DOCKER_REPO:main
- run: docker push $DOCKER_REPO:${{ steps.vars.outputs.sha_short }}
- run: docker push $DOCKER_REPO:main