Skip to content

Deploy

Deploy #189

Workflow file for this run

name: Deploy
env:
registry: "ghcr.io"
on:
workflow_run:
workflows: ["Version Bump"]
types:
- completed
workflow_dispatch:
concurrency:
group: deploy-${{ github.ref }}
cancel-in-progress: false
permissions:
actions: read
contents: read
packages: write
jobs:
verify-version-job:
if: ${{ github.event.repository.fork == false && (github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success') }}
runs-on: ubuntu-latest
outputs:
should_deploy: ${{ steps.check-version.outputs.should_deploy }}
steps:
- name: verify version job succeeded
id: check-version
uses: actions/github-script@v7
with:
script: |
if (context.eventName === "workflow_dispatch") {
core.setOutput("should_deploy", "true");
return;
}
const runId = context.payload.workflow_run?.id;
if (!runId) {
core.info("No upstream workflow run id found. Skipping deploy.");
core.setOutput("should_deploy", "false");
return;
}
const jobs = await github.paginate(github.rest.actions.listJobsForWorkflowRun, {
owner: context.repo.owner,
repo: context.repo.repo,
run_id: runId,
per_page: 100,
});
const versionJob = jobs.find((job) => job.name === "version");
const shouldDeploy = Boolean(versionJob && versionJob.conclusion === "success");
if (!shouldDeploy) {
core.info("Version Bump 'version' job did not succeed; skipping deploy.");
}
core.setOutput("should_deploy", shouldDeploy ? "true" : "false");
docker:
needs: verify-version-job
if: ${{ needs.verify-version-job.outputs.should_deploy == 'true' }}
runs-on: ubuntu-latest
steps:
# checkout the repository at the latest commit
- name: checkout repository
uses: actions/checkout@v6
- name: set start timestamp
id: start-time
run: echo "value=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
- name: Send slack start message
uses: slackapi/slack-github-action@v2.0.0
with:
method: chat.postMessage
token: ${{ secrets.SLACK_BOT_TOKEN }}
payload: |
channel: ${{ secrets.SLACK_CHANNEL_ID }}
text: "Starting <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|deployment> to GHCR at ${{ steps.start-time.outputs.value }}"
# setup node
- name: setup node
uses: actions/setup-node@v3
with:
node-version: 20
cache: npm
# install dependencies
- name: install dependencies
run: npm ci
# set build timestamp
- name: set build timestamp
id: build-time
run: echo "value=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
# get backend version
- name: get backend version
id: backend-version
uses: martinbeentjes/npm-get-version-action@v1.1.0
with:
path: ./
# print version
- name: print backend version
run: echo "building server image with version ${{ steps.backend-version.outputs.current-version }}"
# setup qemu for multi-platform builds
- name: set up qemu
uses: docker/setup-qemu-action@v3
# setup docker buildx
- name: set up docker buildx
id: buildx
uses: docker/setup-buildx-action@v3
with:
buildkitd-flags: --debug
# login to GHCR
- name: login to ghcr
uses: docker/login-action@v3
with:
registry: ${{ env.registry }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
logout: false
# build and push server image
- name: build and push server image
uses: docker/build-push-action@v6
with:
context: "."
file: ./src/server/Dockerfile
platforms: linux/amd64
push: true
tags: |
${{ env.registry }}/${{ github.repository_owner }}/bug:${{ steps.backend-version.outputs.current-version }}
${{ env.registry }}/${{ github.repository_owner }}/bug:latest
labels: |
author=${{ github.actor }}
version=${{ steps.backend-version.outputs.current-version }}
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
uk.co.bbc.bug.build.timestamp=${{ steps.build-time.outputs.value }}
uk.co.bbc.bug.build.number=${{ github.run_number }}
uk.co.bbc.bug.build.commit=${{ github.sha }}
- name: set end timestamp
id: end-time
if: ${{ always() }}
run: echo "value=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
- name: Send slack end message
if: ${{ always() }}
uses: slackapi/slack-github-action@v2.0.0
with:
method: chat.postMessage
token: ${{ secrets.SLACK_BOT_TOKEN }}
payload: |
channel: ${{ secrets.SLACK_CHANNEL_ID }}
text: "Deployment to GHCR finished with status ${{ job.status }} at ${{ steps.end-time.outputs.value }}"