feat: Skip Docker build if image already exists for commit
- Add check step to verify if image for commit SHA already exists
- Skip build step if image exists (saves 5+ minutes per deployment)
- Use git-{SHORT_SHA} tag to identify images by commit
- Only rebuild when code actually changed
- Improve build cache utilization
This commit is contained in:
@@ -100,8 +100,9 @@ jobs:
|
||||
# if: always() && (needs.test.result == 'success' || needs.test.result == 'skipped')
|
||||
runs-on: docker-build # Uses docker:dind image with Docker pre-installed
|
||||
outputs:
|
||||
image_tag: ${{ steps.meta.outputs.tag }}
|
||||
image_tag: ${{ steps.image_info.outputs.IMAGE_TAG || steps.build_image.outputs.IMAGE_TAG || 'git-' + github.sha }}
|
||||
commit_sha: ${{ steps.meta.outputs.commit_sha }}
|
||||
image_built: ${{ steps.check_image.outputs.SKIP_BUILD != 'true' }}
|
||||
steps:
|
||||
- name: Install git and setup environment
|
||||
shell: sh
|
||||
@@ -521,6 +522,7 @@ jobs:
|
||||
TAG="${SHORT_SHA}-$(date +%s)"
|
||||
|
||||
# Build with cache - verwende REGISTRY_TO_USE Variable
|
||||
echo "🏗️ Starte Docker Build für Commit ${SHORT_SHA}..."
|
||||
docker buildx build \
|
||||
--platform linux/amd64 \
|
||||
--file ./Dockerfile.production \
|
||||
@@ -528,17 +530,59 @@ jobs:
|
||||
--tag "${REGISTRY_TO_USE}/${IMAGE_NAME}:${TAG}" \
|
||||
--tag "${REGISTRY_TO_USE}/${IMAGE_NAME}:git-${SHORT_SHA}" \
|
||||
--cache-from type=registry,ref="${REGISTRY_TO_USE}/${IMAGE_NAME}:buildcache" \
|
||||
--cache-from type=registry,ref="${REGISTRY_TO_USE}/${IMAGE_NAME}:latest" \
|
||||
--cache-to type=registry,ref="${REGISTRY_TO_USE}/${IMAGE_NAME}:buildcache",mode=max \
|
||||
--build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') \
|
||||
--build-arg GIT_COMMIT=${COMMIT_SHA} \
|
||||
--build-arg GIT_BRANCH=${REF_NAME} \
|
||||
--push \
|
||||
.
|
||||
|
||||
echo "✅ Image erfolgreich gebaut und gepusht!"
|
||||
|
||||
- name: Use existing image (skip build)
|
||||
if: steps.check_image.outputs.SKIP_BUILD == 'true'
|
||||
shell: bash
|
||||
run: |
|
||||
echo "✅ Verwende vorhandenes Image: ${{ steps.check_image.outputs.EXISTING_IMAGE }}"
|
||||
echo "📝 Commit: ${{ github.sha }}"
|
||||
echo "🏷️ Image-Tag: ${{ steps.check_image.outputs.IMAGE_TAG }}"
|
||||
echo ""
|
||||
echo "⏭️ Build-Schritt wurde übersprungen, da Image bereits in Registry existiert."
|
||||
echo "💡 Dies spart Zeit und Ressourcen - das Image wird direkt für Deployment verwendet."
|
||||
|
||||
- name: Set image tag for deployment
|
||||
id: image_info
|
||||
shell: bash
|
||||
run: |
|
||||
COMMIT_SHA="${{ github.sha }}"
|
||||
if [ -z "$COMMIT_SHA" ]; then
|
||||
COMMIT_SHA=$(cd /workspace/repo && git rev-parse HEAD)
|
||||
fi
|
||||
SHORT_SHA=$(echo "$COMMIT_SHA" | cut -c1-7)
|
||||
|
||||
# Verwende vorhandenes Image oder neu gebautes
|
||||
if [ "${{ steps.check_image.outputs.SKIP_BUILD }}" = "true" ]; then
|
||||
IMAGE_TAG="git-${SHORT_SHA}"
|
||||
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_OUTPUT
|
||||
echo "IMAGE_EXISTS=true" >> $GITHUB_OUTPUT
|
||||
else
|
||||
# Tag aus neuem Build
|
||||
IMAGE_TAG="${SHORT_SHA}-$(date +%s)"
|
||||
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_OUTPUT
|
||||
echo "IMAGE_EXISTS=false" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
echo "📦 Verwendetes Image-Tag: $IMAGE_TAG"
|
||||
|
||||
- name: Image scan for vulnerabilities
|
||||
shell: bash
|
||||
run: |
|
||||
echo "✅ Image built successfully: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.tag }}"
|
||||
if [ "${{ steps.check_image.outputs.SKIP_BUILD }}" = "true" ]; then
|
||||
echo "⏭️ Vulnerability scan übersprungen (verwendet vorhandenes Image)"
|
||||
else
|
||||
echo "✅ Image built successfully: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.image_info.outputs.IMAGE_TAG }}"
|
||||
fi
|
||||
|
||||
# Job 3: Deploy to Production
|
||||
deploy:
|
||||
|
||||
Reference in New Issue
Block a user