Skip to content

Deploy to Digital Ocean #37

Deploy to Digital Ocean

Deploy to Digital Ocean #37

Workflow file for this run

name: Deploy
on:
workflow_run:
workflows: ["CI"]
types:
- completed
branches:
- main
workflow_dispatch:
inputs:
branch:
description: 'Branch to deploy'
required: true
default: 'main'
jobs:
deploy:
runs-on: ubuntu-latest
if: |
github.event_name == 'workflow_dispatch' ||
(github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.head_branch == 'main')
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.branch || 'main' }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GH_PAT }}
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ghcr.io/loftwah/linkarooie:${{ github.event.inputs.branch || 'latest' }}
- name: Deploy to droplet
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.DROPLET_IP }}
username: root
key: ${{ secrets.DROPLET_SSH_PRIVATE_KEY }}
script: |
echo "${{ secrets.GH_PAT }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
# Stop and remove all running containers
docker stop $(docker ps -aq) || true
docker rm $(docker ps -aq) || true
# Pull the latest image for the specified branch
docker pull ghcr.io/loftwah/linkarooie:${{ github.event.inputs.branch || 'latest' }}
# Run the new container
docker run -d --name linkarooie-app -p 80:3000 -e RAILS_ENV=production -e SECRET_KEY_BASE=${{ secrets.SECRET_KEY_BASE }} ghcr.io/loftwah/linkarooie:${{ github.event.inputs.branch || 'latest' }}