Merge pull request #46 from BUMETCS673/fix/docker-compose-failure #35
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 | |
with: | |
driver: docker | |
- 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: false | |
tags: getactive-frontend:latest | |
load: true | |
build-args: | | |
NODE_ENV=production | |
- name: List local Docker images after frontend build | |
run: docker images | |
- name: Build and push nginx image | |
uses: docker/build-push-action@v5 | |
with: | |
context: ./code/nginx | |
file: ./code/nginx/Dockerfile | |
push: true | |
no-cache: true | |
tags: ${{ env.REGISTRY }}/${{ env.NGINX_IMAGE }}:${{ env.TAG }} | |
- name: List local Docker images after nginx build | |
run: docker images | |
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.NGINX_IMAGE }}:.*|image: ${{ env.REGISTRY }}/${{ env.NGINX_IMAGE }}:${{ env.TAG }}|" docker-compose.yml | |
echo "Restarting services using docker-compose..." | |
docker-compose down | |
docker-compose up -d | |
echo "Cleaning up old Docker images..." | |
docker image prune -f | |
echo "Deployment to EC2 completed successfully!" |