diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 62ac086..0ee8548 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -2,65 +2,66 @@ name: Deploy on: push: - branches: [master] - -env: - REGISTRY: ghcr.io - IMAGE_NAME: ${{ github.repository }} + branches: [main, develop, staging] + workflow_dispatch: + inputs: + environment: + description: 'Environment to deploy to' + required: true + default: 'dev' + type: choice + options: + - dev + - staging + - production jobs: - build-and-push: + deploy: runs-on: ubuntu-latest - permissions: - contents: read - packages: write + needs: [] + environment: + 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: - uses: actions/checkout@v4 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 + - name: Set environment variables + 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 - 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 + - name: Deploy to ${{ steps.env.outputs.env_name }} uses: appleboy/ssh-action@v1.0.3 with: - host: ${{ secrets.HETZNER_HOST }} - username: ${{ secrets.HETZNER_USER }} - key: ${{ secrets.HETZNER_SSH_KEY }} + host: ${{ steps.env.outputs.ssh_host }} + username: root + key: ${{ secrets.DEPLOY_SSH_KEY }} + port: ${{ steps.env.outputs.ssh_port }} script: | cd /opt/shellmate - docker compose pull - docker compose up -d --remove-orphans - docker system prune -f + git fetch origin + git checkout ${{ github.ref_name }} + 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