Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Improve Dockerfile and enable pipeline to automate docker image generation #388

Merged
merged 18 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# this build is triggered when a new pre-release has been created
# it creates a new docker build image based on the tag associated

name: build

on:
release:
types:
- 'prereleased'
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

jobs:
build:
name: build
runs-on: ubuntu-latest

steps:
# download the source code into the runner
- name: checkout
uses: actions/checkout@v2

# gather metadata from git & github actions to reference in docker
- name: git & github metadata
id: metadata
uses: docker/metadata-action@v3
with:
images: ${{ env.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/openverse/frontend

# setup docker buildx
- name: setup docker buildx
uses: docker/setup-buildx-action@v1
with:
install: true

# login in docker repository
- name: docker login
uses: aws-actions/amazon-ecr-login@v1

# build a docker image
- name: build docker image
uses: docker/build-push-action@v2
with:
context: .
tags: ${{ steps.metadata.outputs.tags }}
labels: ${{ steps.metadata.outputs.labels }}
push: true


48 changes: 48 additions & 0 deletions .github/workflows/pre-build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# this workflow will try to lint and build a node.js application
#
# this is useful for stages that you require to make sure everything is working
# properly before creating a container image to be pushed on the cloud
#
name: pre_build

on:
pull_request:
push:
branches:
- 'main'
- 'ci/*' # branches that follows the pattern ci/* can access this workflow too
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really solid idea! I always hate troubleshooting issues with commits to main, this is a great workaround.


jobs:
pre_build:
name: pre_build
runs-on: ubuntu-latest

steps:
# download the source code into the runner
- name: checkout
uses: actions/checkout@v2

# setup node.js environment and npm
- name: setup node environment
uses: actions/setup-node@v2
with:
node-version: '16.x'

# lookup cache dependencies already defined, based on the hash generated from the file
# yarn.lock
- name: cache dependencies
id: cache-node-modules
uses: actions/cache@v2
with:
path: node_modules
key: node-modules-${{ hashFiles('package-lock.json') }}

# install dependencies only if the cache is not present
- name: install dependencies
if: steps.cache-node-modules.outputs.cache-hit != 'true'
run: npm install

# run lint and syntax
- name: lint & syntax check
run: npm run lint

44 changes: 33 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
FROM node:14-alpine
# application builder
FROM node:16-alpine AS builder

# directory for the app in the container
WORKDIR /usr/app

# copies all the app's files from host into the container folder which
# might include the node_modules dir if npm install executed in the host
# copy package.json and package-lock.json files
COPY package*.json .

# install dependencies including local development tools
RUN npm install

# copy the rest of the content
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This copy command does not copy the node_modules folder because it's in the dockerignore file, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, and as per the official docs, if the .dockerignore file is not present, docker ignores the files present in the .gitignore

COPY . /usr/app

# removes any existing node_modules folder
# this prevents the host's node_modules from being used in the container
# which could cause issues with native binaries such as node_sass.
RUN rm -rf /usr/app/node_modules/
# build the application and generate a distribution package
RUN npm run build

# application package
FROM node:16-alpine AS app

WORKDIR /usr/app

ENV NODE_ENV=production
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ENV NODE_ENV=production

We'll actually want to remove this, related to #450. We want to be able to use NODE_ENV as staging or as production. Can this be moved to the actions, maybe @rbadillap ? By the way, Nuxt builds the app the same in staging or prod mode, so you don't have to worry about differences in the built files based on this key.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on internal conversations, this should be kept as it

ENV CYPRESS_INSTALL_BINARY=0
rbadillap marked this conversation as resolved.
Show resolved Hide resolved

RUN npm install -g npm@7.21.0 && \
npm install && \
npm run i18n:get-translations
zackkrida marked this conversation as resolved.
Show resolved Hide resolved
# copy the package.json and package-lock.json files
COPY package*.json .

RUN npm install
rbadillap marked this conversation as resolved.
Show resolved Hide resolved

# copy distribution directory with the static content
COPY --from=builder /usr/app/dist /usr/app/dist


# expose port 3000 by default
# https://nuxtjs.org/docs/features/configuration/#edit-host-and-port
EXPOSE 3000

# run the application in static mode
CMD ["npm", "start"]