Skip to content

hirurg-lybitel/docker-server-with-github-actions

Repository files navigation

Deploy server with github actions

This example illustrates how to set up automatic build and deployment of a docker image on your remote server by GitHub Acions.

View the result on the demo server.

There're three main points here:

  1. Node App
  2. GitHub-actions
  3. Remote machine

Node App

There is a few ways to run app locally:

  • by node
yarn launch
  • by pm2
yarn pm2:start
  • by docker
docker compose up

GitHub Actions

Here you need to set several mandatory secrets for your repository (Settings -> Secrets and variables -> Actions):

  • DOCKER_* - your Docker Hub credentials;
  • DEPLOY_* - SSH data for connection to your remote machine.

image

After push cahnges to github on main branch go to Actions tab of your repository:

image

Two tasks will appear here which will:

  1. check your code
  2. build a docker image
  3. upload it to GitHub Container Registry (GitHub alternative to Docker)
  4. connect to your remote server
  5. deploy the built image on it

Remote machine*

*change path workspace/Yuri/test in docker-deploy.yml to your path.

In addition to setting up ssh and port forwarding, several files are needed here:

  • .env
  • docker-compose.run.yml
  • run_container.sh

image

.env

See .env.sample and enter your data.

docker-compose.run.yml

Docker compose file for creating and running container:

version: "3.8"
services:
  server:
    image: ghcr.io/hirurg-lybitel/docker-server-with-github-actions:main    
    build:
      context: .
      dockerfile: Dockerfile
      target: server
      args:
        - PM2_PUBLIC_KEY=${PM2_PUBLIC_KEY}
        - PM2_SECRET_KEY=${PM2_SECRET_KEY}
    container_name: simple_server
    ports:
      - "${PORT}:${PORT}"
    env_file:
      - ./.env

run_container.sh

Bash script for calling docker compose:

docker compose -f docker-compose.run.yml --env-file .env up -d

PM2 monitoring

After launching project locally by pm2 or deploying it on remote server you will able to monitor your server in PM2 dashboard:

image