build-image-workflow/action.yml
Shkar T. Noori ce1914c7e6
Refactor caching mechanism in action.yml
This update modifies the caching strategy by removing the previous cache type and introducing a new registry-based cache for builds. This change enhances the efficiency of the caching process during CI/CD workflows.
2025-05-09 01:27:45 +03:00

99 lines
2.6 KiB
YAML

name: "Build, Scan and Push Image"
description: "Build, Scan and Push Image to Self Hosted Registry"
inputs:
push:
description: "Push to Registry"
required: false
default: "true"
image:
description: "Image Name"
required: true
build-args:
description: "Build Arguments"
required: false
file:
description: "Dockerfile Path"
required: false
registry:
description: "Registry URL"
required: true
default: reg.dev.krd
username:
required: true
description: "Username for registry"
password:
required: true
description: "Password for registry"
build-secrets:
required: false
description: "Build Secrets"
outputs:
digest:
description: "Digest"
value: ${{ steps.build-and-push.outputs.digest }}
tag:
description: "Image Tag"
value: ${{ steps.set_tag.outputs.tag }}
tags:
description: "Image Tags"
value: ${{ steps.meta.outputs.tags }}
runs:
using: "composite"
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- id: meta
name: Extract Metadata
uses: docker/metadata-action@v5
with:
images: ${{ inputs.registry }}/${{ inputs.image }}
flavor: latest=false
tags: |
# Pull Request
type=ref,event=pr
type=ref,event=pr,suffix=-{{sha}},priority=8887 # 2
# Branches
type=ref,event=branch
type=ref,event=branch,suffix=-{{sha}},priority=8888 # 2
# Releases
type=semver,pattern={{major}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{version}},priority=9999 #1
- name: Login to Registry
uses: docker/login-action@v3
with:
registry: ${{ inputs.registry }}
username: ${{ inputs.username }}
password: ${{ inputs.password }}
- name: Build Docker images
uses: docker/build-push-action@v6
id: build-and-push
with:
push: ${{ inputs.push }}
file: ${{ inputs.file }}
tags: ${{ steps.meta.outputs.tags }}
cache-to: type=registry,ref=${{ inputs.registry }}/${{ inputs.image }}:buildcache,mode=max
cache-from: type=registry,ref=${{ inputs.registry }}/${{ inputs.image }}:buildcache
build-args: ${{ inputs.build-args }}
secrets: ${{ inputs.build-secrets }}
- name: Set Tag
id: set_tag
shell: bash
run: |
extracted_tag=$(echo "$json" | jq -r '.tags | .[0]')
echo "tag=$extracted_tag" >> $GITHUB_OUTPUT
env:
tags: ${{ steps.meta.outputs.tags }}
json: ${{ steps.meta.outputs.json }}