Files
build-image/action.yaml

131 lines
3.9 KiB
YAML
Raw Normal View History

2025-04-14 11:26:34 +02:00
name: 'Build a docker image and tag it'
description: 'Build a docker image and tag it using podman or docker'
2025-04-14 10:57:40 +02:00
author: 'Eric Coissac'
inputs:
2025-04-14 11:16:56 +02:00
image_name:
description: 'name of the image'
2025-04-14 10:57:40 +02:00
required: true
2025-04-14 11:16:56 +02:00
image_tags:
description: 'tags added to the image'
2025-04-14 10:57:40 +02:00
required: true
2025-04-14 11:16:56 +02:00
registry_server:
description: 'Address of the image registry server'
2025-04-14 10:57:40 +02:00
required: false
2025-04-14 11:16:56 +02:00
default: "niepce.petite-maison-orange.fr"
build_command:
description: 'Command used to build the image (docker|podman)'
2025-04-14 10:57:40 +02:00
required: false
2025-04-14 11:16:56 +02:00
default: docker
2025-04-28 20:31:54 +02:00
no_cache:
description: 'Do we desactivate cache during image duilding'
required: false
default: false
2025-04-14 11:16:56 +02:00
tag_command:
description: 'Command used to tag the image (docker|podman)'
required: false
default: podman
2025-04-14 10:57:40 +02:00
debug:
description: 'Run the action in debug mode'
required: false
default: false
type: boolean
runs:
using: 'composite'
2025-04-14 11:29:42 +02:00
2025-04-14 10:57:40 +02:00
steps:
2025-04-14 12:11:06 +02:00
- name: Load bash library
uses: https://gargoton.petite-maison-orange.fr/pmo-actions/bash-library@main
2025-04-14 10:57:40 +02:00
- name: Set debug mode
2025-04-14 16:27:05 +02:00
if: inputs.debug == 'true'
2025-04-14 10:57:40 +02:00
shell: bash
run: |
2025-04-14 12:17:33 +02:00
source "${{ github.action_path }}/../bashlib.sh"
2025-04-14 10:57:40 +02:00
export DEBUG="ON"
2025-04-14 16:07:58 +02:00
echo "DEBUG=ON" >> $GITHUB_ENV
2025-04-14 10:57:40 +02:00
debug Debug switch on
2025-04-14 12:17:33 +02:00
- name: Tash description
shell: bash
run: |
source "${{ github.action_path }}/../bashlib.sh"
step "Image name" "${{ inputs.image_name }}"
step "Image tags" "${{ inputs.image_tags }}"
2025-04-14 10:57:40 +02:00
2025-04-14 11:16:56 +02:00
- name: Build the image
2025-04-14 10:57:40 +02:00
shell: bash
run: |
2025-04-14 12:11:06 +02:00
source "${{ github.action_path }}/../bashlib.sh"
2025-04-14 10:57:40 +02:00
2025-04-14 11:16:56 +02:00
check_command ${{ inputs.build_command }}
2025-04-14 10:57:40 +02:00
2025-04-14 11:36:00 +02:00
IFS=' ' read -r -a tags <<< "${{ inputs.image_tags }}"
debug "Tags parsed: ${tags[*]}"
2025-04-14 11:16:56 +02:00
sep=""
if [[ -n "${tags[0]}" ]] ; then
sep=":"
fi
2025-04-14 10:57:40 +02:00
2025-04-14 12:19:34 +02:00
step "Start building" "${{ inputs.image_name }}${sep}${tags[0]}"
image="${{ inputs.registry_server }}/${{ inputs.image_name }}${sep}${tags[0]}"
2025-04-28 20:31:54 +02:00
if [[ ${{ input.no_cache }} == "true" ]] ; then
nocache="--no-cache"
else
nocache=""
fi
if ${{ inputs.build_command }} build ${nocache} -t ${image} . ; then
2025-04-14 12:34:07 +02:00
success "Image build" "${{ inputs.image_name }}"
else
2025-04-14 12:44:22 +02:00
error "Failed" "Cannot built the image ${{ inputs.image_name }}"
2025-04-14 12:34:07 +02:00
fi
2025-04-14 10:57:40 +02:00
2025-04-14 11:29:42 +02:00
- name: tag images
2025-04-14 11:36:00 +02:00
shell: bash
run: |
2025-04-14 12:11:06 +02:00
source "${{ github.action_path }}/../bashlib.sh"
2025-04-14 11:36:00 +02:00
2025-04-14 11:16:56 +02:00
check_command ${{ inputs.tag_command }}
2025-04-14 10:57:40 +02:00
2025-04-14 11:36:00 +02:00
IFS=' ' read -r -a tags <<< "${{ inputs.image_tags }}"
sep=""
if [[ -n "${tags[0]}" ]] ; then
sep=":"
fi
2025-04-14 12:19:34 +02:00
step "Start tagging" "${{ inputs.image_name }}${sep}${tags[0]}"
2025-04-14 11:16:56 +02:00
image=${{ inputs.registry_server }}/${{ inputs.image_name }}${sep}${tags[0]}
2025-04-14 10:57:40 +02:00
2025-04-14 11:16:56 +02:00
if [[ "${{ inputs.build_command }}" != "${{ inputs.tag_command }}" ]] ; then
2025-04-14 11:41:26 +02:00
step "Transfer image" "From ${{ inputs.build_command }} to ${{ inputs.tag_command }}"
2025-04-14 12:34:07 +02:00
if ${{ inputs.build_command }} save ${image} -o $$.image.tar \
&& ${{ inputs.tag_command }} load -i $$.image.tar
then
success "Success" "Image transfer from ${{ inputs.build_command }} to ${{ inputs.tag_command }}"
else
error "Transfert failed" "for image ${image} from ${{ inputs.build_command }} to ${{ inputs.tag_command }}"
fi
2025-04-14 11:47:45 +02:00
rm -f $$.image.tar
2025-04-14 10:57:40 +02:00
fi
2025-04-14 11:16:56 +02:00
for tag in "${tags[@]:1}" ; do
2025-04-14 12:34:07 +02:00
if ${{ inputs.tag_command }} tag ${image} \
${{ inputs.registry_server }}/${{ inputs.image_name }}:${tag}
then
success "Image tagging" "${{ inputs.registry_server }}/${{ inputs.image_name }}:${tag}"
else
error "Failed" "tagging of image ${{ inputs.image_name }}:${tag}"
fi
2025-04-14 11:16:56 +02:00
done
2025-04-14 10:57:40 +02:00