fix(ci): update cd workflow #38
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CD workflow | |
on: | |
push: | |
tags: | |
- 'v*' | |
branches-ignore: | |
- '**' | |
env: | |
REGISTRY: ghcr.io | |
DB_IMAGE: bumetcs673/cs673olsum25team1/db | |
BACKEND_IMAGE: bumetcs673/cs673olsum25team1/backend | |
FRONTEND_IMAGE: bumetcs673/cs673olsum25team1/frontend | |
NGINX_IMAGE: bumetcs673/cs673olsum25team1/nginx | |
TAG: ${{ github.ref_name }} | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Log in to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Build and push database image | |
uses: docker/build-push-action@v5 | |
with: | |
context: ./code/database | |
file: ./code/database/Dockerfile | |
push: true | |
tags: ${{ env.REGISTRY }}/${{ env.DB_IMAGE }}:${{ env.TAG }} | |
- name: Build and push backend image | |
uses: docker/build-push-action@v5 | |
with: | |
context: ./code/backend | |
file: ./code/backend/Dockerfile | |
push: true | |
tags: ${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE }}:${{ env.TAG }} | |
- name: Build frontend image | |
uses: docker/build-push-action@v5 | |
with: | |
context: ./code/frontend | |
file: ./code/frontend/Dockerfile | |
push: true | |
tags: ${{ env.REGISTRY }}/${{ env.FRONTEND_IMAGE }}:${{ env.TAG }} | |
build-args: | | |
NODE_ENV=production | |
deploy: | |
needs: build-and-push | |
runs-on: ubuntu-latest | |
steps: | |
- name: Deploy to EC2 | |
uses: appleboy/ssh-action@v1.0.3 | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
set -e | |
PROJECT_DIR="/srv/getactive" | |
echo "Navigating to project directory: $PROJECT_DIR" | |
cd $PROJECT_DIR | |
echo "Logging into GitHub Container Registry..." | |
echo ${{ secrets.GITHUB_TOKEN }} | docker login ${{ env.REGISTRY }} -u ${{ github.actor }} --password-stdin | |
echo "Pulling latest images..." | |
docker pull ${{ env.REGISTRY }}/${{ env.DB_IMAGE }}:${{ env.TAG }} | |
docker pull ${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE }}:${{ env.TAG }} | |
docker pull ${{ env.REGISTRY }}/${{ env.NGINX_IMAGE }}:${{ env.TAG }} | |
echo "Updating image tags in docker-compose.yml..." | |
sed -i "s|image: ${{ env.REGISTRY }}/${{ env.DB_IMAGE }}:.*|image: ${{ env.REGISTRY }}/${{ env.DB_IMAGE }}:${{ env.TAG }}|" docker-compose.yml | |
sed -i "s|image: ${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE }}:.*|image: ${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE }}:${{ env.TAG }}|" docker-compose.yml | |
sed -i "s|image: ${{ env.REGISTRY }}/${{ env.FRONTEND_IMAGE }}:.*|image: ${{ env.REGISTRY }}/${{ env.FRONTEND_IMAGE }}:${{ env.TAG }}|" docker-compose.yml | |
echo "Restarting services using docker-compose..." | |
docker-compose down --remove-orphans --timeout 30 | |
docker-compose up -d | |
echo "Cleaning up old Docker images..." | |
docker image prune -f | |
echo "Deployment to EC2 completed successfully!" |