Skip to content

Commit

Permalink
Update build-docker.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
jifollan authored and GitHub Enterprise committed Mar 20, 2025
1 parent 1e53838 commit 9727b05
Showing 1 changed file with 56 additions and 19 deletions.
75 changes: 56 additions & 19 deletions .github/workflows/build-docker.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and Publish Docker Images to DockerHub
name: Build, Publish, and Deploy Docker Images

on:
push:
Expand All @@ -11,45 +11,82 @@ on:
- '**/*.yml'

jobs:
build:
build-and-publish:
runs-on: [self-hosted]

steps:
# Step 1: Checkout the code
- name: Checkout code
uses: actions/checkout@v2

# Step 2: Set up Docker Buildx (only if needed on your self-hosted runner)
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

# Step 3: Log in to Docker Hub
# Step 2: Log in to Docker Hub
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }} # Store Docker Hub username in GitHub secrets
password: ${{ secrets.DOCKER_PASSWORD }} # Store Docker Hub password in GitHub secrets

# Step 4: Build the Backend Docker image
# Step 3: Build the Backend Docker image
- name: Build Backend Docker image
run: |
docker build --network=host -t jonafoll/backend:${{ github.sha }} ./backend
docker tag jonafoll/backend:${{ github.sha }} jonafoll/backend:latest
sudo docker build -t jonafoll/backend:${{ github.sha }} ./backend
sudo docker tag jonafoll/backend:${{ github.sha }} jonafoll/backend:latest
# Step 5: Build the Frontend Docker image
# Step 4: Build the Frontend Docker image
- name: Build Frontend Docker image
run: |
docker build --network=host -t jonafoll/frontend:${{ github.sha }} ./frontend
docker tag jonafoll/frontend:${{ github.sha }} jonafoll/frontend:latest
sudo docker build -t jonafoll/frontend:${{ github.sha }} ./frontend
sudo docker tag jonafoll/frontend:${{ github.sha }} jonafoll/frontend:latest
# Step 6: Push the Backend Docker image to Docker Hub
# Step 5: Push the Backend Docker image to Docker Hub
- name: Push Backend Docker image to Docker Hub
run: |
docker push jonafoll/backend:${{ github.sha }}
docker push jonafoll/backend:latest
sudo docker push jonafoll/backend:${{ github.sha }}
sudo docker push jonafoll/backend:latest
# Step 7: Push the Frontend Docker image to Docker Hub
# Step 6: Push the Frontend Docker image to Docker Hub
- name: Push Frontend Docker image to Docker Hub
run: |
docker push jonafoll/frontend:${{ github.sha }}
docker push jonafoll/frontend:latest
sudo docker push jonafoll/frontend:${{ github.sha }}
sudo docker push jonafoll/frontend:latest
deploy:
runs-on: [self-hosted]
needs: build-and-publish # Ensure this job runs after build-and-publish

steps:
# Step 1: Log in to Docker Hub
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

# Step 2: Pull the latest Docker images
- name: Pull Backend Docker image
run: sudo docker pull jonafoll/backend:latest

- name: Pull Frontend Docker image
run: sudo docker pull jonafoll/frontend:latest

# Step 3: Stop and remove existing containers
- name: Stop and remove existing containers
run: |
sudo docker stop backend-container || true
sudo docker rm backend-container || true
sudo docker stop frontend-container || true
sudo docker rm frontend-container || true
# Step 4: Run the updated Docker images
- name: Run Backend Docker container
run: sudo docker run -d --name backend-container -p 5000:5000 jonafoll/backend:latest

- name: Run Frontend Docker container
run: sudo docker run -d --name frontend-container -p 3000:3000 jonafoll/frontend:latest

# Step 5: Verify the containers are running
- name: Verify Backend container
run: sudo docker ps --filter "name=backend-container" --format "{{.Status}}" | grep -q "Up"

- name: Verify Frontend container
run: sudo docker ps --filter "name=frontend-container" --format "{{.Status}}" | grep -q "Up"

0 comments on commit 9727b05

Please sign in to comment.