fix: pipeline

This commit is contained in:
Smigz 2025-05-02 16:54:53 +00:00
parent f91c08dddb
commit 59cb48ffb0

View file

@ -1,77 +1,60 @@
name: Build Docker Images name: Build and Push Docker Images
on: on:
push: push:
branches: branches:
- main - main
pull_request:
branches:
- main
jobs: jobs:
build: build-and-push:
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions: strategy:
packages: write matrix:
contents: read project: [admin-dashboard, auth-expressjs, battleship, calculator, cv, cv-project, inventory, library, messages, nodejs-mini-message-board, portfolio, restaurant, shopping-cart, signup_form, tictactoe, todo, weather]
steps: steps:
# Checkout the repository
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
# Log into the GitHub Container Registry - name: Set target revision
- name: Login to GHCR id: set-revision
run: |
echo "target_revision=${GITHUB_SHA}" >> $GITHUB_OUTPUT
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker Login
uses: docker/login-action@v3 uses: docker/login-action@v3
with: with:
registry: ghcr.io registry: ghcr.io
username: ${{ github.actor }} username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }} password: ${{ secrets.GITHUB_TOKEN }}
# Define the list of project directories
- name: Set project list
id: set_projects
run: |
echo "projects<<EOF" >> $GITHUB_OUTPUT
echo "admin-dashboard" >> $GITHUB_OUTPUT
echo "battleship" >> $GITHUB_OUTPUT
echo "calculator" >> $GITHUB_OUTPUT
echo "cv" >> $GITHUB_OUTPUT
echo "cv-project" >> $GITHUB_OUTPUT
echo "inventory" >> $GITHUB_OUTPUT
echo "library" >> $GITHUB_OUTPUT
echo "messages" >> $GITHUB_OUTPUT
echo "nodejs-mini-message-board" >> $GITHUB_OUTPUT
echo "portfolio" >> $GITHUB_OUTPUT
echo "restaurant" >> $GITHUB_OUTPUT
echo "shopping-cart" >> $GITHUB_OUTPUT
echo "signup_form" >> $GITHUB_OUTPUT
echo "tictactoe" >> $GITHUB_OUTPUT
echo "todo" >> $GITHUB_OUTPUT
echo "weather" >> $GITHUB_OUTPUT
echo "auth-expressjs" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
# Build and push Docker images for changed projects - name: Check for existing image
- name: Build and push Docker images id: check-image
id: build_push
env:
PROJECTS: ${{ steps.set_projects.outputs.projects }}
run: | run: |
# Loop through each project directory IMAGE_NAME=ghcr.io/${{ github.repository_owner }}/${{ matrix.project }}:${{ steps.set-revision.outputs.target_revision }}
for project in $(cat <<< "$PROJECTS"); do if docker manifest inspect $IMAGE_NAME > /dev/null 2>&1; then
# Check if the project has changes echo "image_exists=true" >> $GITHUB_OUTPUT
if git diff --quiet HEAD^ HEAD -- "$project" || git diff --quiet HEAD^ HEAD -- "${project}/devops"; then else
echo "Changes detected in project: $project" echo "image_exists=false" >> $GITHUB_OUTPUT
# Set the image name dynamically using github context fi
image_name="ghcr.io/${{ github.repository_owner }}/$project:${{ github.sha }}"
# Build the docker image - name: Build and Push Docker Image
echo "Building image: $image_name" if: steps.check-image.outputs.image_exists == 'false'
docker build -t "$image_name" "$project/devops" uses: docker/build-push-action@v5
# Push the docker image to the github container registry with:
echo "Pushing image: $image_name" context: ${{ matrix.project }}
docker push "$image_name" push: true
else tags: ghcr.io/${{ github.repository_owner }}/${{ matrix.project }}:${{ steps.set-revision.outputs.target_revision }}
echo "No changes detected in project: $project" target: ${{ matrix.project }}
fi
done - name: Build and Push Docker Image If Changed
if: steps.check-image.outputs.image_exists == 'true'
uses: docker/build-push-action@v5
with:
context: ${{ matrix.project }}
push: true
tags: ghcr.io/${{ github.repository_owner }}/${{ matrix.project }}:${{ steps.set-revision.outputs.target_revision }}
target: ${{ matrix.project }}
cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ matrix.project }}:${{ steps.set-revision.outputs.target_revision }}