From 96dacd3ce7c7b21e74f29226afbd8e721ac9fed4 Mon Sep 17 00:00:00 2001 From: esteinig Date: Fri, 26 Jan 2024 15:39:15 +1100 Subject: [PATCH] ci(stack): add deploy script and prod action --- .github/workflows/cicd-prod.yml | 23 ++++++++++++++++++ scripts/deploy.sh | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 .github/workflows/cicd-prod.yml create mode 100644 scripts/deploy.sh diff --git a/.github/workflows/cicd-prod.yml b/.github/workflows/cicd-prod.yml new file mode 100644 index 0000000..4897acd --- /dev/null +++ b/.github/workflows/cicd-prod.yml @@ -0,0 +1,23 @@ +name: CI/CD Workflow + +on: + pull_request: + branches: [ main ] + types: [ closed ] + +jobs: + deploy: + runs-on: ubuntu-latest + if: github.event.pull_request.merged == true && startsWith(github.head_ref, 'release/') + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Setup SSH Agent + uses: webfactory/ssh-agent@v0.8.0 + with: + ssh-private-key: ${{ secrets.SSH_PRODUCTION_PRIVATE_KEY }} + + - name: Deploy to production server + run: ssh ${{ secrets.SSH_PRODUCTION_SERVER }} 'bash -s' < scripts/deploy.sh prod ${{ secrets.SSH_PRODUCTION_PATH }} diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100644 index 0000000..44c7698 --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +# Navigate to your project directory +cd $2 + +# Depending on the environment, deploy the appropriate Docker Compose setup +if [ "$1" = "prod" ]; then + # Pull down the production stack + docker-compose -f docker-compose.web.yml --profile prod --profile server --project-name prod down + + # Make sure we are on `main` and pull latest stable changes + # this is the latest release as this script is triggered + # by the `cicd.yml` action workflow + + git checkout main + git pull origin main + + # Rebuild to include latest changes and up the stack again + docker-compose -f docker-compose.web.yml --profile prod --profile server --project-name prod up --build -d + + # Log this action + echo "$date Successfully deployed the production application" >> ~/brick_deploy_action.log + +elif [ "$1" = "dev" ]; then + # Pull down the development stack + docker-compose -f docker-compose.web.yml --profile dev --profile server-dev --project-name dev down + + # Make sure we are on `dev` and pull latest changes + git checkout dev + git pull origin dev + + # Rebuild to include latest changes and up the stack again + docker-compose -f docker-compose.web.yml --profile dev --profile server-dev --project-name dev up --build -d + + # Log this action + echo "$date Successfully deployed the development application" >> ~/brick_deploy_action.log + +else + # Deployment script was run with invalid command-line input + echo "$date Failed to run deploy script - `$2` is not a valid option!" >> ~/brick_deploy_action.log +fi \ No newline at end of file