Skip to content

Commit

Permalink
Merge pull request #17 from Vizzuality/infrastructure
Browse files Browse the repository at this point in the history
Infrastructure
  • Loading branch information
martintomas authored Sep 21, 2023
2 parents d34ec74 + 04f5504 commit 92eea3b
Show file tree
Hide file tree
Showing 38 changed files with 1,153 additions and 34 deletions.
137 changes: 137 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
name: Run deploy

on:
workflow_dispatch:
push:
branches:
- develop
paths:
- 'client/**'
- 'cms/**'
- '.github/workflows/*'

env:
PROJECT_NAME: "esa-gda-comms"
IMAGE_REGISTRY: "registry.digitalocean.com/esa-gda-comms"

jobs:
build_client_image:
name: Build Client image and push to DO Container Registry
runs-on: ubuntu-22.04

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Extract branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
id: extract_branch

- name: Copy env variables to docker
env:
ENVIRONMENT: ${{ steps.extract_branch.outputs.branch == 'main' && 'PROD' || 'STAGING' }}
run: |
echo "${{ secrets[format('{0}_CLIENT_ENV_FILE', env.ENVIRONMENT)] }}" > client/.env.local
- name: Build and tag Client image
env:
IMAGE_NAME: ${{ env.PROJECT_NAME }}-${{ steps.extract_branch.outputs.branch == 'main' && 'production' || 'staging' }}-client
run: |
docker build -f client/Dockerfile.prod -t $IMAGE_REGISTRY/$IMAGE_NAME:$GITHUB_SHA .
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}

- name: Log in to DO Container Registry
run: doctl registry login --expiry-seconds 600

- name: Push image to DigitalOcean Container Registry
env:
IMAGE_NAME: ${{ env.PROJECT_NAME }}-${{ steps.extract_branch.outputs.branch == 'main' && 'production' || 'staging' }}-client
run: docker push $IMAGE_REGISTRY/$IMAGE_NAME:$GITHUB_SHA

- name: Remove old images from Container Registry
uses: ripplr-io/docr-image-remove@v1
with:
image_repository: ${{ env.PROJECT_NAME }}-${{ steps.extract_branch.outputs.branch == 'main' && 'production' || 'staging' }}-client
buffer_size: 5

build_cms_image:
name: Build CMS image and push to DO Container Registry
runs-on: ubuntu-22.04

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Extract branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
id: extract_branch

- name: Copy env variables to docker
env:
ENVIRONMENT: ${{ steps.extract_branch.outputs.branch == 'main' && 'PROD' || 'STAGING' }}
run: |
echo "${{ secrets[format('{0}_CMS_ENV_FILE', env.ENVIRONMENT)] }}" > cms/.env
- name: Build and tag CMS image
env:
IMAGE_NAME: ${{ env.PROJECT_NAME }}-${{ steps.extract_branch.outputs.branch == 'main' && 'production' || 'staging' }}-cms
run: |
docker build -f cms/Dockerfile.prod -t $IMAGE_REGISTRY/$IMAGE_NAME:$GITHUB_SHA .
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}

- name: Log in to DO Container Registry
run: doctl registry login --expiry-seconds 600

- name: Push image to DigitalOcean Container Registry
env:
IMAGE_NAME: ${{ env.PROJECT_NAME }}-${{ steps.extract_branch.outputs.branch == 'main' && 'production' || 'staging' }}-cms
run: docker push $IMAGE_REGISTRY/$IMAGE_NAME:$GITHUB_SHA

- name: Remove old images from Container Registry
uses: ripplr-io/docr-image-remove@v1
with:
image_repository: ${{ env.PROJECT_NAME }}-${{ steps.extract_branch.outputs.branch == 'main' && 'production' || 'staging' }}-cms
buffer_size: 5

deploy:
name: Deploy Client and CMS to DO APP Platform
needs: [build_client_image, build_cms_image]
runs-on: ubuntu-22.04

steps:
- name: DigitalOcean App Platform deployment
uses: digitalocean/[email protected]
env:
IMAGE_NAME_CLIENT: ${{ env.PROJECT_NAME }}-${{ steps.extract_branch.outputs.branch == 'main' && 'production' || 'staging' }}-client
IMAGE_NAME_CMS: ${{ env.PROJECT_NAME }}-${{ steps.extract_branch.outputs.branch == 'main' && 'production' || 'staging' }}-cms
with:
app_name: ${{ env.PROJECT_NAME }}-${{ steps.extract_branch.outputs.branch == 'main' && 'production' || 'staging' }}
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
images: '[
{
"name": "${{ env.IMAGE_NAME_CLIENT }}",
"image":{
"registry_type": "DOCR",
"repository": "${{ env.IMAGE_NAME_CLIENT }}",
"tag": "${{ github.sha }}"
}
},
{
"name": "${{ env.IMAGE_NAME_CMS }}",
"image":{
"registry_type": "DOCR",
"repository": "${{ env.IMAGE_NAME_CMS }}",
"tag": "${{ github.sha }}"
}
}
]'
21 changes: 0 additions & 21 deletions Dockerfile.cms

This file was deleted.

12 changes: 6 additions & 6 deletions Dockerfile.client → client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
FROM node:18-alpine
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
ENV NODE_ENV development
FROM node:18.15.0-bullseye-slim
RUN apt-get update -y && \
apt-get upgrade -y && \
apt-get install -y libc6 && \
apt-get clean

WORKDIR /app
COPY .yarn ./.yarn
Expand All @@ -19,5 +20,4 @@ USER node

EXPOSE 3000
ENV PORT 3000

CMD ["yarn", "dev"]
ENTRYPOINT ["/app/client/entrypoint.sh"]
42 changes: 42 additions & 0 deletions client/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Build all the things
FROM node:18.15.0-bullseye-slim AS build
RUN apt-get update -y && \
apt-get upgrade -y && \
apt-get install -y libc6 && \
apt-get clean
ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1

WORKDIR /app
COPY .yarn ./.yarn
COPY package.json .yarnrc.yml yarn.lock .prettierrc.js ./

WORKDIR /app/client
COPY ./client/package.json ./
RUN yarn install

COPY ./client .

RUN yarn build

# Copy only the built files into the final image
FROM node:18.15.0-bullseye-slim AS runner
ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1

WORKDIR /app

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=build --chown=nextjs:nodejs /app/client/.next ./.next
COPY --from=build --chown=nextjs:nodejs /app/client/entrypoint.sh ./entrypoint.sh
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/client/package.json ./package.json

USER nextjs

EXPOSE 3000
ENV PORT 3000

ENTRYPOINT ["/app/entrypoint.sh"]
19 changes: 19 additions & 0 deletions client/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh
set -e

case "${NODE_ENV}" in
development)
echo "Running Development Server"
exec yarn dev
;;
test)
echo "Running Test"
exec yarn test
;;
production)
echo "Running Production Server"
exec yarn start
;;
*)
echo "Unknown NODE environment: \"${NODE_ENV}\""
esac
31 changes: 31 additions & 0 deletions cms/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM node:18.15.0-bullseye-slim
# Install dependencies
RUN apt-get update -y && \
apt-get upgrade -y && \
apt-get install -y \
build-essential \
gcc autoconf \
automake \
zlib1g-dev \
libpng-dev \
nasm bash \
libvips-dev \
&& apt-get clean

WORKDIR /app/
COPY .yarn ./.yarn
COPY package.json .yarnrc.yml yarn.lock ./

WORKDIR /app/cms
COPY ./cms/package.json ./
RUN yarn install

ENV PATH /app/node_modules/.bin:$PATH

COPY ./cms .
RUN chown -R node:node /app/cms
USER node
RUN ["yarn", "prebuild"]
RUN ["yarn", "build"]
EXPOSE 1337
ENTRYPOINT ["/app/cms/entrypoint.sh"]
49 changes: 49 additions & 0 deletions cms/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Build all the things
FROM node:18.15.0-bullseye-slim as build
RUN apt-get update -y && \
apt-get upgrade -y && \
apt-get install -y \
build-essential \
gcc autoconf \
automake \
zlib1g-dev \
libpng-dev \
nasm bash \
libvips-dev \
&& apt-get clean

ENV NODE_ENV production

WORKDIR /app
COPY .yarn ./.yarn
COPY package.json .yarnrc.yml yarn.lock ./

WORKDIR /app/cms
COPY ./cms/package.json ./
RUN yarn install

COPY ./cms .
RUN yarn prebuild
RUN yarn build

# Copy only the built files into the final image
FROM node:18.15.0-bullseye-slim AS runner
RUN apt-get update -y && \
apt-get upgrade -y && \
apt-get install -y libvips-dev && \
apt-get clean

ENV NODE_ENV production

WORKDIR /app

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 strapi

COPY --from=build --chown=strapi:nodejs /app/cms ./
COPY --from=build /app/node_modules ./node_modules

USER strapi

EXPOSE 1337
ENTRYPOINT ["/app/entrypoint.sh"]
1 change: 1 addition & 0 deletions cms/config/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
url: env('CMS_URL', null),
app: {
keys: env.array('APP_KEYS'),
},
Expand Down
19 changes: 19 additions & 0 deletions cms/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
set -e

case "${NODE_ENV}" in
development)
echo "Running Development Server"
exec yarn dev
;;
test)
echo "Running Test"
exec yarn test
;;
production)
echo "Running Production Server"
exec yarn start
;;
*)
echo "Unknown NODE environment: \"${NODE_ENV}\""
esac
1 change: 1 addition & 0 deletions cms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"dev": "strapi develop",
"start": "strapi start",
"prebuild": "cd src/plugins/map-field && yarn install && yarn build",
"build": "strapi build",
"strapi": "strapi",
"seed": "strapi import -f ./seed.tar.gz"
Expand Down
Loading

0 comments on commit 92eea3b

Please sign in to comment.