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: Check for existing image
- name: Set project list id: check-image
id: set_projects
run: | run: |
echo "projects<<EOF" >> $GITHUB_OUTPUT IMAGE_NAME=ghcr.io/${{ github.repository_owner }}/${{ matrix.project }}:${{ steps.set-revision.outputs.target_revision }}
echo "admin-dashboard" >> $GITHUB_OUTPUT if docker manifest inspect $IMAGE_NAME > /dev/null 2>&1; then
echo "battleship" >> $GITHUB_OUTPUT echo "image_exists=true" >> $GITHUB_OUTPUT
echo "calculator" >> $GITHUB_OUTPUT else
echo "cv" >> $GITHUB_OUTPUT echo "image_exists=false" >> $GITHUB_OUTPUT
echo "cv-project" >> $GITHUB_OUTPUT fi
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: Build and Push Docker Image
- name: Build and push Docker images if: steps.check-image.outputs.image_exists == 'false'
id: build_push uses: docker/build-push-action@v5
env: with:
PROJECTS: ${{ steps.set_projects.outputs.projects }} context: ${{ matrix.project }}
run: | push: true
# Loop through each project directory tags: ghcr.io/${{ github.repository_owner }}/${{ matrix.project }}:${{ steps.set-revision.outputs.target_revision }}
for project in $(cat <<< "$PROJECTS"); do target: ${{ matrix.project }}
# Check if the project has changes
if git diff --quiet HEAD^ HEAD -- "$project" || git diff --quiet HEAD^ HEAD -- "${project}/devops"; then - name: Build and Push Docker Image If Changed
echo "Changes detected in project: $project" if: steps.check-image.outputs.image_exists == 'true'
# Set the image name dynamically using github context uses: docker/build-push-action@v5
image_name="ghcr.io/${{ github.repository_owner }}/$project:${{ github.sha }}" with:
# Build the docker image context: ${{ matrix.project }}
echo "Building image: $image_name" push: true
docker build -t "$image_name" "$project/devops" tags: ghcr.io/${{ github.repository_owner }}/${{ matrix.project }}:${{ steps.set-revision.outputs.target_revision }}
# Push the docker image to the github container registry target: ${{ matrix.project }}
echo "Pushing image: $image_name" cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ matrix.project }}:${{ steps.set-revision.outputs.target_revision }}
docker push "$image_name"
else
echo "No changes detected in project: $project"
fi
done