Skip to content

feat: add dockerized Node.js service with GitHub Actions deployment #1

feat: add dockerized Node.js service with GitHub Actions deployment

feat: add dockerized Node.js service with GitHub Actions deployment #1

Workflow file for this run

name: Deploy Dockerized Service
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: ./app
push: true
tags: |
ghcr.io/${{ github.repository }}:latest
ghcr.io/${{ github.repository }}:${{ github.sha }}
- name: Deploy to Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.TARGET_HOST }}
username: ${{ secrets.TARGET_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
# Pull the latest image
docker pull ghcr.io/${{ github.repository }}:latest
# Stop and remove existing container if it exists
docker stop dockerized-service || true
docker rm dockerized-service || true
# Run the new container
docker run -d \
--name dockerized-service \
--restart always \
-p 3000:3000 \
--env-file /opt/dockerized-service/.env \
ghcr.io/${{ github.repository }}:latest