feat: add dockerized Node.js service with GitHub Actions deployment #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |