Skip to content

Commit f530484

Browse files
committed
Add deploy workflow for dev/staging/production environments
1 parent f4663fa commit f530484

1 file changed

Lines changed: 51 additions & 50 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,66 @@ name: Deploy
22

33
on:
44
push:
5-
branches: [master]
6-
7-
env:
8-
REGISTRY: ghcr.io
9-
IMAGE_NAME: ${{ github.repository }}
5+
branches: [main, develop, staging]
6+
workflow_dispatch:
7+
inputs:
8+
environment:
9+
description: 'Environment to deploy to'
10+
required: true
11+
default: 'dev'
12+
type: choice
13+
options:
14+
- dev
15+
- staging
16+
- production
1017

1118
jobs:
12-
build-and-push:
19+
deploy:
1320
runs-on: ubuntu-latest
14-
permissions:
15-
contents: read
16-
packages: write
21+
needs: []
22+
environment:
23+
name: ${{ github.ref == 'refs/heads/main' && 'production' || github.ref == 'refs/heads/staging' && 'staging' || 'dev' }}
24+
url: ${{ github.ref == 'refs/heads/main' && 'https://shellmate.sh' || github.ref == 'refs/heads/staging' && 'https://staging.shellmate.sh' || 'https://dev.shellmate.sh' }}
1725

1826
steps:
1927
- uses: actions/checkout@v4
2028

21-
- name: Set up Docker Buildx
22-
uses: docker/setup-buildx-action@v3
23-
24-
- name: Log in to Container Registry
25-
uses: docker/login-action@v3
26-
with:
27-
registry: ${{ env.REGISTRY }}
28-
username: ${{ github.actor }}
29-
password: ${{ secrets.GITHUB_TOKEN }}
29+
- name: Set environment variables
30+
id: env
31+
run: |
32+
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
33+
echo "env_name=production" >> $GITHUB_OUTPUT
34+
echo "ssh_host=shellmate.sh" >> $GITHUB_OUTPUT
35+
echo "ssh_port=22" >> $GITHUB_OUTPUT
36+
elif [[ "${{ github.ref }}" == "refs/heads/staging" ]]; then
37+
echo "env_name=staging" >> $GITHUB_OUTPUT
38+
echo "ssh_host=shellmate.sh" >> $GITHUB_OUTPUT
39+
echo "ssh_port=2223" >> $GITHUB_OUTPUT
40+
else
41+
echo "env_name=dev" >> $GITHUB_OUTPUT
42+
echo "ssh_host=shellmate.sh" >> $GITHUB_OUTPUT
43+
echo "ssh_port=2222" >> $GITHUB_OUTPUT
44+
fi
3045
31-
- name: Extract metadata
32-
id: meta
33-
uses: docker/metadata-action@v5
34-
with:
35-
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
36-
tags: |
37-
type=sha,prefix=
38-
type=raw,value=latest
39-
40-
- name: Build and push
41-
uses: docker/build-push-action@v5
42-
with:
43-
context: .
44-
push: true
45-
tags: ${{ steps.meta.outputs.tags }}
46-
labels: ${{ steps.meta.outputs.labels }}
47-
cache-from: type=gha
48-
cache-to: type=gha,mode=max
49-
50-
deploy:
51-
runs-on: ubuntu-latest
52-
needs: build-and-push
53-
environment: production
54-
55-
steps:
56-
- name: Deploy to Hetzner
46+
- name: Deploy to ${{ steps.env.outputs.env_name }}
5747
uses: appleboy/ssh-action@v1.0.3
5848
with:
59-
host: ${{ secrets.HETZNER_HOST }}
60-
username: ${{ secrets.HETZNER_USER }}
61-
key: ${{ secrets.HETZNER_SSH_KEY }}
49+
host: ${{ steps.env.outputs.ssh_host }}
50+
username: root
51+
key: ${{ secrets.DEPLOY_SSH_KEY }}
52+
port: ${{ steps.env.outputs.ssh_port }}
6253
script: |
6354
cd /opt/shellmate
64-
docker compose pull
65-
docker compose up -d --remove-orphans
66-
docker system prune -f
55+
git fetch origin
56+
git checkout ${{ github.ref_name }}
57+
git pull origin ${{ github.ref_name }}
58+
docker compose up -d --build
59+
echo "Deployed ${{ github.ref_name }} to ${{ steps.env.outputs.env_name }}"
60+
61+
- name: Deployment summary
62+
run: |
63+
echo "## Deployment Complete 🚀" >> $GITHUB_STEP_SUMMARY
64+
echo "" >> $GITHUB_STEP_SUMMARY
65+
echo "- **Environment:** ${{ steps.env.outputs.env_name }}" >> $GITHUB_STEP_SUMMARY
66+
echo "- **Branch:** ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY
67+
echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)