Deploy Next.js app #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: Deploy Next.js app | |
on: | |
push: | |
branches: | |
- master | |
paths: | |
- 'src/**' | |
workflow_dispatch: | |
env: | |
REGISTRY: registry.digitalocean.com/vixero-projects | |
PROJECT_NAME: vixero-portfolio-website | |
jobs: | |
build_and_push: | |
name: Build and push docker image | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Build container | |
run: |- | |
docker build \ | |
--build-arg NEXT_PUBLIC_TO_EMAIL_ADDRESS="${{ vars.NEXT_PUBLIC_TO_EMAIL_ADDRESS }}" \ | |
--build-arg NEXT_PUBLIC_RECAPTCHA_SITE_KEY="${{ vars.NEXT_PUBLIC_RECAPTCHA_SITE_KEY }}" \ | |
--build-arg TO_EMAIL_ADDRESS="${{ secrets.TO_EMAIL_ADDRESS }}" \ | |
--build-arg SENDGRID_API_KEY="${{ secrets.SENDGRID_API_KEY }}" \ | |
--build-arg RECAPTCHA_SECRET_KEY="${{ secrets.RECAPTCHA_SECRET_KEY }}" \ | |
--build-arg GOOGLE_DRIVE_RESUME_FILE_ID="${{ secrets.GOOGLE_DRIVE_RESUME_FILE_ID }}" \ | |
-t "${{ env.REGISTRY }}/${{ env.PROJECT_NAME }}:${{ github.sha }}" ./ | |
- name: Install doctl | |
uses: digitalocean/action-doctl@v2 | |
with: | |
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} | |
- name: Log in to DigitalOcean Container Registry with short-lived credentials | |
run: doctl registry login --expiry-seconds 600 | |
- name: Remove all old images | |
run: | | |
if [ ! -z "$(doctl registry repository list | grep "${{ env.PROJECT_NAME }}")" ]; | |
then doctl registry repository delete-manifest "${{ env.PROJECT_NAME }}" $(doctl registry repository list-tags "${{ env.PROJECT_NAME }}" | grep -o "sha.*") --force; | |
else echo "No repository"; | |
fi | |
- name: Push image to DigitalOcean Container Registry | |
run: docker push ${{ env.REGISTRY }}/${{ env.PROJECT_NAME }}:${{ github.sha }} | |
deploy: | |
name: Deploy to Digital Ocean droplet | |
runs-on: ubuntu-latest | |
needs: build_and_push | |
steps: | |
- name: Deploy to Digital Ocean droplet via SSH action | |
uses: appleboy/ssh-action@v0.1.3 | |
with: | |
host: ${{ secrets.DIGITALOCEAN_HOST }} | |
username: ${{ secrets.DIGITALOCEAN_USERNAME }} | |
password: ${{ secrets.DIGITALOCEAN_PASSWORD }} | |
script: | | |
# Login to registry | |
docker login -u ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} -p ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} registry.digitalocean.com | |
# Check if the container exists before stopping and removing | |
if docker ps -a --format '{{.Names}}' | grep -q "${{ env.PROJECT_NAME }}"; then | |
# Stop running container | |
docker stop ${{ env.PROJECT_NAME }} | |
# Remove old container | |
docker rm ${{ env.PROJECT_NAME }} | |
fi | |
# Run a new container from a new image | |
docker run -d \ | |
--restart always \ | |
--name ${{ env.PROJECT_NAME }} \ | |
--network bridge -p 3000:3000 \ | |
${{ env.REGISTRY }}/${{ env.PROJECT_NAME }}:${{ github.sha }} |