Skip to content

Commit

Permalink
Merge pull request #42 from wearefuturegov/feature/platforms-for-build
Browse files Browse the repository at this point in the history
Update github actions to use new syntax
  • Loading branch information
apricot13 authored Jun 11, 2024
2 parents 4cd0a1b + 285260b commit ec28e21
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# This action pre-builds the dev-base image and pushes it to the GitHub Container Registry (GHCR)

name: Deploy outpost api image to github container registry
name: Publish outpost-api-service to github container registry

on:
push:
branches: [develop, staging, production]

jobs:
push-outpost-api-image:
publish-outpost-api-image:
runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -28,18 +28,26 @@ jobs:
run: |
BRANCH_NAME=${GITHUB_REF#refs/heads/}
if [[ "$BRANCH_NAME" == "develop" ]]; then
echo "::set-output name=tag::development"
echo "tag=development" >> $GITHUB_ENV
elif [[ "$BRANCH_NAME" == "staging" ]]; then
echo "::set-output name=tag::staging"
echo "tag=staging" >> $GITHUB_ENV
elif [[ "$BRANCH_NAME" == "production" ]]; then
echo "::set-output name=tag::latest"
echo "tag=latest" >> $GITHUB_ENV
else
echo "tag=default" >> $GITHUB_ENV
fi
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push outpost api docker image
uses: docker/build-push-action@v5
with:
context: .
tags: ghcr.io/wearefuturegov/outpost-api-service:${{ steps.vars.outputs.tag }}
tags: ghcr.io/wearefuturegov/outpost-api-service:${{ env.tag }}
file: Dockerfile.production
platforms: ${{ matrix.platforms }}
push: true
Expand Down
44 changes: 44 additions & 0 deletions .github/workflows/test-docker-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This action tests to make sure that the docker image can be built successfully on the specified platforms

name: Test outpost-api-image docker build

on: push

jobs:
test-docker-build:
runs-on: ubuntu-latest
strategy:
matrix:
platforms: ["linux/amd64", "linux/arm64", "linux/arm64/v8"]
steps:
- name: "Checkout GitHub Action"
uses: actions/checkout@main

- name: Set Docker Image Tag
id: vars
run: |
BRANCH_NAME=${GITHUB_HEAD_REF#refs/heads/}
if [[ "$BRANCH_NAME" == "develop" ]]; then
echo "tag=development" >> $GITHUB_ENV
elif [[ "$BRANCH_NAME" == "staging" ]]; then
echo "tag=staging" >> $GITHUB_ENV
elif [[ "$BRANCH_NAME" == "production" ]]; then
echo "tag=latest" >> $GITHUB_ENV
else
echo "tag=default" >> $GITHUB_ENV
fi
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build Docker image
uses: docker/build-push-action@v5
with:
context: .
tags: outpost-api-service:${{ env.tag }}
file: Dockerfile.production
platforms: ${{ matrix.platforms }}
push: false
75 changes: 75 additions & 0 deletions .github/workflows/test-in-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# This action tests to make sure that the docker image can be built successfully on the specified platforms

name: Run tests in docker container

on: push

jobs:
test-in-docker:
runs-on: ubuntu-latest

steps:
- name: "Checkout GitHub Action"
uses: actions/checkout@main

- name: "Create custom network"
run: docker network create outpost-api-test-network

- name: "Setup mongo"
run: |
docker run --rm -d -p 27017:27017 \
--name mongo \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
-e MONGO_INITDB_USERNAME=outpost \
-e MONGO_INITDB_PASSWORD=password \
-e MONGO_INITDB_DATABASE=outpost_api_development \
-v $PWD/.docker/services/mongo/setup-mongodb.js:/docker-entrypoint-initdb.d/mongo-init.js:ro \
--network=outpost-api-test-network \
mongo:latest
- name: Set Docker Image Tag
id: vars
run: |
BRANCH_NAME=${GITHUB_HEAD_REF#refs/heads/}
if [[ "$BRANCH_NAME" == "develop" ]]; then
echo "tag=development" >> $GITHUB_ENV
elif [[ "$BRANCH_NAME" == "staging" ]]; then
echo "tag=staging" >> $GITHUB_ENV
elif [[ "$BRANCH_NAME" == "production" ]]; then
echo "tag=latest" >> $GITHUB_ENV
else
echo "tag=default" >> $GITHUB_ENV
fi
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build Docker image
uses: docker/build-push-action@v5
with:
context: .
tags: outpost-api-service:${{ env.tag }}
file: Dockerfile.production
push: false
load: true
build-args: |
NODE_ENV=development
FORCE_SSL=false
- name: Run the image
env:
DB_URI: mongodb://outpost:password@mongo:27017/outpost_api_development
run: |
docker run --rm -d \
-e DEBUG_LEVEL=debug \
-e DB_URI=$DB_URI \
--name temp_container \
--network=outpost-api-test-network \
outpost-api-service:${{ env.tag }}
docker exec temp_container npm run dummy-data
docker exec temp_container npm run test
docker stop temp_container
3 changes: 2 additions & 1 deletion Dockerfile.production
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ RUN if [ "${NODE_ENV}" = "development" ] || [ -z "${NODE_ENV}" ]; then \
RUN if [ "${NODE_ENV}" = "production" ]; then \
npm ci --omit=dev && npm cache clean --force; fi

USER outpost-user
WORKDIR /app

# ----------------------------------------------------------------
Expand All @@ -31,7 +32,7 @@ ENV NODE_ENV $NODE_ENV
ENV FORCE_SSL $FORCE_SSL

COPY --from=build_frontend --chown=outpost-user:outpost-user /tmp/node_modules /app/node_modules
COPY . /app
COPY --chown=outpost-user:outpost-user . /app

USER outpost-user
EXPOSE 3000
Expand Down
5 changes: 5 additions & 0 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ let db

module.exports = {
connect: async cb => {
if (!uri) {
logger.error(`DB_URI not set`)
return false
}

MongoClient.connect(uri)
.then(async client => {
// const ping = await client.db().command({ ping: 1 })
Expand Down

0 comments on commit ec28e21

Please sign in to comment.