Skip to content

Commit

Permalink
ci: docker build workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
apotdevin committed Jun 17, 2024
1 parent fa5b66d commit 0878bdf
Show file tree
Hide file tree
Showing 9 changed files with 672 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.git
.next
.env
.github
74 changes: 74 additions & 0 deletions .github/workflows/main-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Docker build and push to repository

on:
push:
branches:
- main

jobs:
main_build_image:
runs-on: ubuntu-latest
outputs:
shortref: ${{ steps.prep.outputs.shortref }}
tags: ${{ steps.prep.outputs.tags }}
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.ECR_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.ECR_SECRET_KEY }}
aws-region: us-west-2

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
with:
registry-type: public

- name: Prepare
id: prep
env:
ECR_REGISTRY: ${{ steps.login-ecr-public.outputs.registry }}
ECR_REGISTRY_ALIAS: d6h3d6x3
ECR_REPOSITORY: amboss-banco
run: |
DOCKER_IMAGE=$ECR_REGISTRY/$ECR_REGISTRY_ALIAS/$ECR_REPOSITORY
SHORTREF=${GITHUB_SHA::8}
TAGS="${DOCKER_IMAGE}:${SHORTREF}"
# Set output parameters.
echo "shortref=${SHORTREF}" >> $GITHUB_OUTPUT
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
- name: Check Image
id: image_check
env:
ECR_TAG: ${{ steps.prep.outputs.tags }}
run: docker manifest inspect $ECR_TAG && echo "imageExists=true" >> $GITHUB_OUTPUT || echo "imageExists=false" >> $GITHUB_OUTPUT

- name: Setup Docker Buildx Driver
id: docker_driver_setup
if: ${{ steps.image_check.outputs.imageExists == 'false' }}
uses: docker/setup-buildx-action@v3

- name: Build, tag, and push image to Amazon ECR
id: docker_build
if: ${{ steps.image_check.outputs.imageExists == 'false' }}
uses: docker/build-push-action@v5
with:
context: ./
file: ./Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.prep.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Build Outputs
if: ${{ steps.image_check.outputs.imageExists == 'false' }}
run: |
echo "Image ID: ${{ steps.docker_build.outputs.imageid }}"
echo "Image Digest: ${{ steps.docker_build.outputs.digest }}"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

.env
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20.11.1
57 changes: 57 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
FROM node:20.11.1-alpine as base

# ---------------
# Install Dependencies
# ---------------
FROM base AS deps

RUN apk add --no-cache libc6-compat

WORKDIR /app

COPY package.json package-lock.json ./
RUN npm ci

# ---------------
# Build App
# ---------------
FROM base AS builder

WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules
COPY . .

ENV NEXT_TELEMETRY_DISABLED 1

RUN npm run build

# ---------------
# Final App
# ---------------
FROM base
WORKDIR /app

ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1

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

COPY --from=builder /app/public ./public

RUN mkdir .next
RUN chown nextjs:nodejs .next

COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000
ENV HOSTNAME 0.0.0.0

COPY ./scripts/startup.sh /startup.sh
ENTRYPOINT ["sh", "/startup.sh" ]
1 change: 1 addition & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
async rewrites() {
return [
{
Expand Down
Loading

0 comments on commit 0878bdf

Please sign in to comment.