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:
- Node App
- GitHub-actions
- Remote machine
There is a few ways to run app locally:
- by node
yarn launch
- by pm2
yarn pm2:start
- by docker
docker compose up
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.
After push cahnges to github on main branch go to Actions tab of your repository:
Two tasks will appear here which will:
- check your code
- build a docker image
- upload it to GitHub Container Registry (GitHub alternative to Docker)
- connect to your remote server
- deploy the built image on it
*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
See .env.sample and enter your data.
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
Bash script for calling docker compose:
docker compose -f docker-compose.run.yml --env-file .env up -d
After launching project locally by pm2 or deploying it on remote server you will able to monitor your server in PM2 dashboard: