Update main.yml #3
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: Containerize and Deploy | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Docker Login | |
uses: docker/login-action@v3.1.0 | |
with: | |
registry: ghcr.io | |
username: ${{github.actor}} | |
password: ${{secrets.GITHUB_TOKEN}} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v5.3.0 | |
with: | |
context: ./web | |
file: ./web/Dockerfile | |
push: true | |
tags: ghcr.io/${{github.repository_owner}}/my-app:${{github.sha}} | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build-and-push | |
steps: | |
- name: Deploy | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{secrets.SERVER_IP}} | |
username: ${{secrets.SERVER_USER}} | |
password: ${{secrets.SERVER_PASSWORD}} | |
script: | | |
docker pull ghcr.io/${{github.repository_owner}}/my-app:${{github.sha}} | |
docker stop my-app || true | |
docker rm my-app || true | |
docker run -d --name my-app -p 3000:3000 ghcr.io/${{github.repository_owner}}/my-app:${{github.sha}} |