Skip to content

Commit

Permalink
Merge pull request #41 from wearefuturegov/develop
Browse files Browse the repository at this point in the history
staging deployment
  • Loading branch information
apricot13 authored Jun 11, 2024
2 parents 44cd636 + ec28e21 commit 8778b94
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .docker/services/mongo/mongo.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
version: "3.7"
services:
mongo:
image: mongo:6
image: mongo:latest
platform: linux/arm64
ports:
- 27018:27017
volumes:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# 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:
platforms: ["linux/amd64", "linux/arm64", "linux/arm64/v8"]
steps:
- name: "Checkout GitHub Action"
uses: actions/checkout@main
Expand All @@ -25,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: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ FROM node:iron-alpine as build_frontend
ARG NODE_ENV
ENV NODE_ENV $NODE_ENV

RUN apk update
RUN apk add curl

COPY ./package.json ./tmp/package.json
COPY ./package-lock.json ./tmp/package-lock.json

Expand Down
6 changes: 5 additions & 1 deletion Dockerfile.production
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ ARG FORCE_SSL
ENV NODE_ENV $NODE_ENV
RUN adduser -D outpost-user

RUN apk update
RUN apk add curl

COPY ./package.json ./tmp/package.json
COPY ./package-lock.json ./tmp/package-lock.json

Expand All @@ -18,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 @@ -28,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
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ services:
# command: ["-f", "/dev/null"]
environment:
DB_URI: mongodb://${MONGO_INITDB_USERNAME:-outpost}:${MONGO_INITDB_PASSWORD:-password}@mongo/${MONGO_INITDB_DATABASE:-outpost_api_development}
platform: linux/amd64
platform: linux/arm64
volumes:
- ./:/app:cached
- /app/node_modules
ports:
- 3001:3000
- "${HOST_PORT:-3000}:3000"
networks:
- internal_network
- external_network
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3001/health"]
test: ["CMD-SHELL", "curl -f http://localhost:3000/health"]
interval: 1m30s
timeout: 10s
retries: 3
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const v1 = require("./src/controllers/v1")
const router = express.Router()
const server = express()
const port = process.env.PORT || 3000
const host_port = process.env.HOST_PORT || process.env.PORT || 3000
const environment = process.env.NODE_ENV || "production"
const isDevelopment = environment === "development"

Expand All @@ -21,7 +22,7 @@ connect(() =>
logger.info(
`📡 Database connection established http${
!isDevelopment ? "s" : ""
}://localhost:${port}/api/v1/services`
}://localhost:${host_port}/api/v1/services`
)
)

Expand Down Expand Up @@ -92,6 +93,6 @@ server.use((err, req, res, next) => {
* Start the server
*/
server.listen(port, () => {
logger.info(`Server is running on port ${port}`)
logger.info(`Server is running on port ${host_port}`)
logger.info(`Logging level is set to ${logger.level}`)
})
68 changes: 23 additions & 45 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,62 +34,37 @@ To run it on your machine you need Node.js, npm, nvm (https://github.com/nvm-sh/

## 🧬 Configure Outpost API

It expects a few environment variables.

`DB_URI`

- MongoDB connection URI

`GOOGLE_API_KEY`

- Used for geocoding `location=` parameters.
- **Needs the geocoding API enabled.**

Other environmental variables:

`COMPOSE_PROJECT_NAME`

- This is used to name the docker images and containers

---
See [environmental variables](#environmental-variables) below.

## 💻 Running it locally

It is recommended to use docker but you can choose to run the project locally as well.

```sh
# get the code
git clone [email protected]:wearefuturegov/outpost-api-service.git && cd outpost-api-service

# make sure your using the correct node version
nvm use

# setup env and database variables
# see setting up database below
cp sample.env .env

# Setup your database locally (see below) or start up a database using docker
# NB this runs mongo on a non-standard port `27018` in case you have mongo already running, you can change it to `27017` if you would like to
# connect with compass or mongosh with: mongodb://outpost:password@localhost:27018
docker compose up -d mongo

# once its setup you can just use this to restart it
docker start outpost-api-db

# install dependencies
npm install
# setup
docker compose up -d

# run development mode
npm run dev
# if you need dummy data
docker compose exec app npm run dummy-data

# access the api
open http://localhost:3001/api/v1/services

# add some dummy data (if required)
npm run dummy-data
# access the mongodb
# NB this runs mongo on a non-standard port `27018` in case you have mongo already running, you can change it to `27017` if you would like to
mongo "mongodb://outpost:password@localhost:27018/outpost_api_development"

# stop your database
# stop the application at any time by running
docker compose stop

# delete your database image
# remove the running containers by running
docker compose down

```

### Setting up database locally
Expand All @@ -102,8 +77,6 @@ mongosh .docker/services/mongo/setup-mongodb.js
npm run prepare-indices
```

or you can use the following commands to create your indices

# 🧬 Configuration

## Environmental Variables
Expand All @@ -112,10 +85,13 @@ You can provide config with a `.env` file. Run `cp sample.env .env` to create a

The following environmental variables are required.

| Variable | Description | Example | Required? |
| ---------------- | ------------------ | -------------------------------------------------------------------- | --------- |
| `DB_URI` | Mongo database url | `mongodb://outpost:password@localhost:27018/outpost_api_development` | Yes |
| `GOOGLE_API_KEY` | Google API Key | `1234` | Yes |
| Variable | Description | Example | Required? |
| ---------------- | ------------------------------------------------------------------------- | -------------------------------------------------------------------- | --------- |
| `DB_URI` | Mongo database url | `mongodb://outpost:password@localhost:27018/outpost_api_development` | Yes |
| `GOOGLE_API_KEY` | Google API Key | `1234` | Yes |
| `DEBUG_LEVEL` | Debug logging level options are: error, warn, info, http, debug | `debug` | No |
| `FORCE_SSL` | Force SSL defaults to false unless this is set to true | true | No |
| `HOST_PORT` | If running in docker set this to change the exposed port, default is 3000 | 3001 | No |

# ✨ Features

Expand All @@ -133,6 +109,8 @@ You will need to enable the `Geocode API`, no restrictions are needed.

It's suitable for 12-factor hosting like Heroku. It has a [Procfile](https://devcenter.heroku.com/articles/procfile) that will make sure the proper MongoDB indices are set up.

Ensure you set `FORCE_SSL` to true.

### Deploying using docker

We also provide a Docker image if you would like to host the application in a container
Expand Down
5 changes: 4 additions & 1 deletion sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
# doesn't apply in development env
# FORCE_SSL=true

# if you need to run the api on a different port on the host
# HOST_PORT=3000

# ----- MONGO CONFIG

DB_URI=mongodb://localhost:27018/outpost_api_development
# DB_URI=mongodb://localhost:27018/outpost_api_development

# ----- GEOCODING
GOOGLE_API_KEY=
Expand Down
Loading

0 comments on commit 8778b94

Please sign in to comment.