mirror of
https://github.com/ghndrx/shellmate.git
synced 2026-02-10 06:45:02 +00:00
Add deploy workflow for dev/staging/production environments
This commit is contained in:
101
.github/workflows/deploy.yml
vendored
101
.github/workflows/deploy.yml
vendored
@@ -2,65 +2,66 @@ name: Deploy
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [master]
|
branches: [main, develop, staging]
|
||||||
|
workflow_dispatch:
|
||||||
env:
|
inputs:
|
||||||
REGISTRY: ghcr.io
|
environment:
|
||||||
IMAGE_NAME: ${{ github.repository }}
|
description: 'Environment to deploy to'
|
||||||
|
required: true
|
||||||
|
default: 'dev'
|
||||||
|
type: choice
|
||||||
|
options:
|
||||||
|
- dev
|
||||||
|
- staging
|
||||||
|
- production
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-and-push:
|
deploy:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
permissions:
|
needs: []
|
||||||
contents: read
|
environment:
|
||||||
packages: write
|
name: ${{ github.ref == 'refs/heads/main' && 'production' || github.ref == 'refs/heads/staging' && 'staging' || 'dev' }}
|
||||||
|
url: ${{ github.ref == 'refs/heads/main' && 'https://shellmate.sh' || github.ref == 'refs/heads/staging' && 'https://staging.shellmate.sh' || 'https://dev.shellmate.sh' }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set environment variables
|
||||||
uses: docker/setup-buildx-action@v3
|
id: env
|
||||||
|
run: |
|
||||||
|
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
|
||||||
|
echo "env_name=production" >> $GITHUB_OUTPUT
|
||||||
|
echo "ssh_host=shellmate.sh" >> $GITHUB_OUTPUT
|
||||||
|
echo "ssh_port=22" >> $GITHUB_OUTPUT
|
||||||
|
elif [[ "${{ github.ref }}" == "refs/heads/staging" ]]; then
|
||||||
|
echo "env_name=staging" >> $GITHUB_OUTPUT
|
||||||
|
echo "ssh_host=shellmate.sh" >> $GITHUB_OUTPUT
|
||||||
|
echo "ssh_port=2223" >> $GITHUB_OUTPUT
|
||||||
|
else
|
||||||
|
echo "env_name=dev" >> $GITHUB_OUTPUT
|
||||||
|
echo "ssh_host=shellmate.sh" >> $GITHUB_OUTPUT
|
||||||
|
echo "ssh_port=2222" >> $GITHUB_OUTPUT
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Log in to Container Registry
|
- name: Deploy to ${{ steps.env.outputs.env_name }}
|
||||||
uses: docker/login-action@v3
|
|
||||||
with:
|
|
||||||
registry: ${{ env.REGISTRY }}
|
|
||||||
username: ${{ github.actor }}
|
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Extract metadata
|
|
||||||
id: meta
|
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
|
||||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
|
||||||
tags: |
|
|
||||||
type=sha,prefix=
|
|
||||||
type=raw,value=latest
|
|
||||||
|
|
||||||
- name: Build and push
|
|
||||||
uses: docker/build-push-action@v5
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
push: true
|
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
|
||||||
cache-from: type=gha
|
|
||||||
cache-to: type=gha,mode=max
|
|
||||||
|
|
||||||
deploy:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
needs: build-and-push
|
|
||||||
environment: production
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Deploy to Hetzner
|
|
||||||
uses: appleboy/ssh-action@v1.0.3
|
uses: appleboy/ssh-action@v1.0.3
|
||||||
with:
|
with:
|
||||||
host: ${{ secrets.HETZNER_HOST }}
|
host: ${{ steps.env.outputs.ssh_host }}
|
||||||
username: ${{ secrets.HETZNER_USER }}
|
username: root
|
||||||
key: ${{ secrets.HETZNER_SSH_KEY }}
|
key: ${{ secrets.DEPLOY_SSH_KEY }}
|
||||||
|
port: ${{ steps.env.outputs.ssh_port }}
|
||||||
script: |
|
script: |
|
||||||
cd /opt/shellmate
|
cd /opt/shellmate
|
||||||
docker compose pull
|
git fetch origin
|
||||||
docker compose up -d --remove-orphans
|
git checkout ${{ github.ref_name }}
|
||||||
docker system prune -f
|
git pull origin ${{ github.ref_name }}
|
||||||
|
docker compose up -d --build
|
||||||
|
echo "Deployed ${{ github.ref_name }} to ${{ steps.env.outputs.env_name }}"
|
||||||
|
|
||||||
|
- name: Deployment summary
|
||||||
|
run: |
|
||||||
|
echo "## Deployment Complete 🚀" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "- **Environment:** ${{ steps.env.outputs.env_name }}" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "- **Branch:** ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
|
||||||
|
|||||||
Reference in New Issue
Block a user