Skip to content

Commit

Permalink
✨ add github workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
UrWrstNightmare committed Dec 18, 2023
1 parent b88e8b8 commit 32fef6a
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 4,833 deletions.
13 changes: 13 additions & 0 deletions .docker/Back.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:20-alpine AS builder
ENV NODE_ENV production

WORKDIR /app

COPY ./back/package.json .
COPY ./back/package-lock.json .

RUN npm install --omit=dev

COPY ./back .

CMD ["npm", "start"]
23 changes: 23 additions & 0 deletions .docker/Front.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM node:20-alpine AS builder
ENV NODE_ENV production

WORKDIR /app

COPY ./front/package.json .
COPY ./front/package-lock.json .

RUN npm install --omit=dev

COPY ./front .

RUN npm run build

# Bundle static assets with nginx
FROM nginx:1.25.3-alpine as production
ENV NODE_ENV production
# Copy built assets from builder
COPY --from=builder /app/build /usr/share/nginx/html
# Add your nginx.conf
COPY ./.docker/nginx/nginx.conf /etc/nginx/conf.d/default.conf
# Start nginx
CMD ["nginx", "-g", "daemon off;"]
20 changes: 20 additions & 0 deletions .docker/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
server {
listen 80;
root /usr/share/nginx/html;

location / {
include /etc/nginx/mime.types;
try_files $uri $uri/ /index.html;
}

# Cache static assets
location ~* \.(?:jpg|jpeg|gif|png|ico|svg)$ {
expires 7d;
add_header Cache-Control "public";
}

# Cache css and js bundle
location ~* \.(?:css|js)$ {
add_header Cache-Control "no-cache, public, must-revalidate, proxy-revalidate";
}
}
82 changes: 82 additions & 0 deletions .github/workflows/build-and-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: "Build and Push Production Build"

on:
workflow_call:
secrets:
ENV_VARS_FRONT:
description: "Environment Variables in Client (@/front/.env)"
required: true
ENV_VARS_BACK:
description: "Environment Variables in Server (@/back/.env)"
required: true
HOST:
description: "Server Host"
required: true
USERNAME:
description: "Server Username"
required: true
PASSWORD:
description: "Server Password"
required: true

jobs:
publish-docker-image:
name: "Build and Publish Latest Docker Image"
runs-on: ubuntu-22.04

strategy:
fail-fast: false
matrix:
include:
- dockerfile: ./.docker/Front.Dockerfile
image: ghcr.io/sparcs-kaist/sparcs-clubs-front
- dockerfile: ./.docker/Back.Dockerfile
image: ghcr.io/sparcs-kaist/sparcs-clubs-back

steps:
- name: "Checkout Repository"
uses: actions/checkout@v2

- name: "Create .env File"
run: |
echo "Creating .env file"
echo "${{ secrets.ENV_VARS_FRONT }}" >> front/.env
echo "${{ secrets.ENV_VARS_BACK }}" >> back/.env
echo "done"
- name: "Log in to Github Container Registry"
uses: docker/login-action@v2
with:
registry: "ghcr.io"
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: "Extract Metadata"
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ matrix.image }}
labels: latest

- name: Build and push Docker image Drill4Net.Agent.Service
uses: docker/build-push-action@v3
with:
context: .
file: ${{ matrix.dockerfile }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

deploy-production-image:
name: "Deploy Production Image in Server"
needs: [publish-docker-image]
runs-on: ubuntu-22.04
steps:
- name: "Run Remote Pull & Build Script"
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
sudo /home/${{ secrets.USERNAME }}/redeploy.sh ${{ github.actor }} ${{ secrets.GITHUB_TOKEN }}
24 changes: 24 additions & 0 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Continuous Deployment to Production Server

on:
push:
tags:
- "v*"
- "!*-rc*"
- "!*-alpha*"
- "!*-beta*"

concurrency:
group: cd-${{ github.ref }}
cancel-in-progress: true

jobs:
deploy-production:
name: "Deploy Production Build (CD)"
uses: "./.github/workflows/build-and-push.yml"
secrets:
ENV_VARS_FRONT: ${{ secrets.ENV_VARS_FRONT }}
ENV_VARS_BACK: ${{ secrets.ENV_VARS_BACK }}
HOST: ${{ secrets.HOST }}
USERNAME: ${{ secrets.USERNAME }}
PASSWORD: ${{ secrets.PASSWORD }}
17 changes: 17 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: "3.8"

services:
client:
container_name: sparcs-clubs-front
image: sparcs-clubs-front
build:
context: .
dockerfile: .docker/Front.Dockerfile
target: production

back:
container_name: sparcs-clubs-back
image: sparcs-clubs-back
build:
context: .
dockerfile: .docker/Back.Dockerfile
Loading

0 comments on commit 32fef6a

Please sign in to comment.