build-image-workflow/action.yml
Shakar Bakr 80887ec86b
Add JSON output and extraction using jq
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
2024-03-03 14:05:48 +03:00

112 lines
2.9 KiB
YAML

name: "Build, Scan and Push Image"
description: "Build, Scan and Push Image to Self Hosted Registry"
inputs:
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
timeout:
description: "Timeout"
required: false
default: "10"
username:
required: true
description: "Username for registry"
password:
required: true
description: "Password for registry"
build-secrets:
required: false
description: "Secrets for build"
outputs:
tag:
description: "Image Tag"
value: ${{ fromJson(steps.meta.outputs.json).tags[0] }}
tags:
description: "Image Tags"
value: ${{ steps.meta.outputs.tags }}
runs:
using: "composite"
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Run a command and output JSON
id: run-command
shell: bash
run: |
echo '{"tags": ["tag1", "tag2", "tag3"]}' > output.json
- name: Display JSON
run: cat output.json
shell: bash
- name: Extract specific value from JSON using jq
id: extract-json
run: echo "::set-output name=tag::$(echo '${{ steps.run-command.outputs.json }}' | jq -r '.tags[0]')"
shell: bash
- name: Display Extracted Value
run: |
echo "Extracted Tag: ${{ steps.extract-json.outputs.tag }}"
shell: bash
- id: meta
name: Extract Metadata
uses: docker/metadata-action@v5
with:
images: ${{ inputs.registry }}/${{ inputs.image }}
flavor: latest=false
tags: |
# Cache
type=raw,value=${{ github.ref_name }}-cache
# 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: echo
run: |
echo $(echo '${{ steps.meta.outputs.json }}' | jq -r '.tags[0]') > meta.json
shell: bash
- name: dsad
run: |
cat meta.json
shell: bash
- 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@v5
with:
push: true
file: ${{ inputs.file }}
tags: ${{ steps.meta.outputs.tags }}
cache-to: type=inline
cache-from: type=registry,ref=${{ inputs.registry }}/${{ inputs.image }}:${{ github.ref_name }}-cache
build-args: ${{ inputs.build-args }}
secrets: ${{ inputs.build-secrets }}