58 lines
1.5 KiB
YAML
58 lines
1.5 KiB
YAML
name: Deploy API
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- chore/setup-workflow
|
|
paths:
|
|
- api/**
|
|
- .github/workflows/deploy_api.yml
|
|
|
|
env:
|
|
REGISTRY: ghcr.io
|
|
|
|
jobs:
|
|
build-and-push:
|
|
name: Build Docker image and push to GitHub Container Registry
|
|
runs-on: ubuntu-latest
|
|
|
|
permissions:
|
|
contents: read
|
|
packages: write
|
|
|
|
steps:
|
|
- uses: actions/checkout@v3
|
|
- uses: docker/login-action@v2
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
- uses: docker/metadata-action@v4
|
|
id: meta
|
|
with:
|
|
images: ${{ env.REGISTRY }}/${{ github.repository }}/api
|
|
- uses: docker/build-push-action@v4
|
|
with:
|
|
context: ./api
|
|
push: true
|
|
tags: ${{ steps.meta.outputs.tags }}
|
|
labels: ${{ steps.meta.outputs.labels }}
|
|
|
|
deploy:
|
|
needs: [build-and-push]
|
|
name: Deploy to EC2
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- uses: appleboy/ssh-action@master
|
|
with:
|
|
host: ${{ secrets.EC2_HOST }}
|
|
username: ${{ secrets.EC2_USERNAME }}
|
|
key: ${{ secrets.EC2_SSH_KEY }}
|
|
script: |
|
|
docker login ${{ env.REGISTRY }} -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }}
|
|
docker pull ${{ env.REGISTRY }}/${{ github.repository }}/api:main
|
|
docker stop crabfit-api
|
|
docker rm crabfit-api
|
|
docker run -d -p 3000:3000 --name crabfit-api --env-file ./.env ${{ env.REGISTRY }}/${{ github.repository }}/api:main
|