Skip to content

Instantly share code, notes, and snippets.

@jeserodz
Last active December 28, 2020 21:40
Show Gist options
  • Save jeserodz/fa56f64196d6a822fb8d1e83b4575906 to your computer and use it in GitHub Desktop.
Save jeserodz/fa56f64196d6a822fb8d1e83b4575906 to your computer and use it in GitHub Desktop.
Node.js DevOps
# Location: <project_root>/.dockerignore
#
# Description: Ignore these files when building the Docker image.
node_modules
npm-debug.log
# Location: <user>@<remote_server>:~/docker-compose.yml
#
# Description: Docker Compose file to run Traefik and the
# application services.
version: "3"
services:
reverse-proxy:
image: traefik:v2.2
command: --api.insecure=true --providers.docker
ports:
- '80:80'
- '8080:8080'
volumes:
- /var/run/docker.sock:/var/run/docker.sock
# Define a service for each application that needs to be routed by Traefik.
<application_name>:
# Build Docker image in the application directory. Files copied over SSH.
build:
context: ./<application_dir>
# Read env file from remote server ~/env directory.
env_file:
- ./env/<application_name>.env
# Specify (or overwrite) the PORT env variable for the application.
environment:
- PORT=3000
# Expose the ports used by the application in the Docker container.
expose:
- 3000
# These labels are used to specify Traefik settings for this application service.
labels:
- 'traefik.http.routers.distrologiq-dev.rule=Host(`api.dev.distrologiq.com`)'
# Location: <project_root>/Dockerfile
#
# Description: Docker image used to run the apllication
# in the remote server. Usually ran with Traefik.
FROM node:alpine
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . .
CMD [ "node", "dist/main" ]
# Location: <project_root>/.github/workflows/main.yml
#
# Description: Build and deploy a Node.js API when a commit is pushed
# into master branch. Uses SSH to copy build artifacts
# and run the Docker container in the remote server.
#
# Example: Express, NestJS, etc.
name: Development
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2-beta
with:
node-version: '12'
- name: Install Dependencies
run: npm install
- name: Build Project
run: npm run build
- name: Deploy to Server
uses: easingthemes/ssh-deploy@v2.1.1
env:
REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
REMOTE_USER: ${{ secrets.REMOTE_USER }}
SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }}
TARGET: './<application_dir>'
ARGS: '-rltgoDzvO --delete'
SOURCE: '.'
- name: Restart Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
cd ~
docker-compose up --build -d <application_name>

Node.js DevOps

These notes provides examples on how to deploy a Node.js applications.

Tooling

  • GitHub Actions
    • Used for CI/CD.
  • Traefik
    • Used for reverse proxy and load balancing.
  • Docker
    • Used to setup Traefik and the applications using docker-compose.
  • SSH
    • Used to transfer the build artifacs and run the application in the remote server.

Setup for Node.js API

  1. Setup DNS A records to point to the remote server IP address.
  2. Configure Traefik on the remote server using ~/docker-compose.yml.
  3. Create environment file on the remote server (~/env/<application_name>.env).
  4. Create a Dockerfile and .dockerignore file in the project.
  5. Create a GitHub Workflow file for CI/CD.
  6. Setup the GitHub Secrets in the project repository.
  7. Push repository changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment