Skip to content

Workflow file for this run

name: Blue/Green CD for Backend
on:
push:
# paths: ["backend/**"]
# branches: ["release", "*hotfix*"]
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
GITHUB_SHA: ${{ github.sha }}
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Generate .env file and SSL files
run: |
cd backend
echo "${{ secrets.ENV_FILE }}" > .env
echo "DOCKER_USERNAME=${{ secrets.DOCKER_USERNAME }}" >> .env
echo "GITHUB_SHA=${{ github.sha }}" >> .env
- name: Add Remote Host Key to known_hosts
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PUBLIC_KEY }}" >> ~/.ssh/known_hosts
- name: Copy some files needed for Deployment to Remote Server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
source: "backend/.env,backend/*.sh,backend/compose*deploy.yml,backend/*nginx*"
target: "~/app/"
overwrite: true
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
- name: Setup docker-compose
uses: KengoTODA/actions-setup-docker-compose@v1.2.1
with:
version: "2.24.5"
- name: Create a Builder Instance
run: |
docker buildx create --name buildx
docker buildx use buildx
- name: Build & Push Docker Images (Blue & Green)
run: |
cp package.json backend/package.json
cp package-lock.json backend/package-lock.json
cp -r packages backend/packages
cd backend
docker-compose -f compose.blue-build.yml build --builder buildx --build-arg BUILDKIT_INLINE_CACHE=1
docker-compose -f compose.green-build.yml build --builder buildx --build-arg BUILDKIT_INLINE_CACHE=1
docker-compose -f compose.blue-build.yml push
docker-compose -f compose.green-build.yml push
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
- name: Run a New Version of the application on Remote Server
uses: appleboy/ssh-action@master
env:
SLACK_WEBHOOK_URI: ${{ secrets.SLACK_WEBHOOK_URI_FOR_GITHUB_ACTIONS }}
GITHUB_WORKFLOW: ${{ github.workflow }}
GITHUB_REF: ${{ github.ref }}
GITHUB_ACTOR: ${{ github.actor }}
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
envs: SLACK_WEBHOOK_URI,GITHUB_WORKFLOW,GITHUB_REF,GITHUB_ACTOR
script: |
cd ~/app/backend
chmod +x deploy.sh
DEPLOYMENT_RESULT='Success'
./deploy.sh || DEPLOYMENT_RESULT='Fail'
chmod +x send-slack-message.sh
./send-slack-message.sh $SLACK_WEBHOOK_URI $DEPLOYMENT_RESULT $GITHUB_REF $GITHUB_ACTOR $GITHUB_WORKFLOW