name: 'Build a docker image and tag it' description: 'Build a docker image and tag it using podman or docker' author: 'Eric Coissac' inputs: image_name: description: 'name of the image' required: true image_tags: description: 'tags added to the image' required: true registry_server: description: 'Address of the image registry server' required: false default: "niepce.petite-maison-orange.fr" build_command: description: 'Command used to build the image (docker|podman)' required: false default: docker no_cache: description: 'Do we desactivate cache during image duilding' required: false default: false tag_command: description: 'Command used to tag the image (docker|podman)' required: false default: podman debug: description: 'Run the action in debug mode' required: false default: false type: boolean runs: using: 'composite' steps: - name: Setup cache uses: actions/cache@v3 with: path: ~/.npm key: dont-cache-${{ github.run_id }} - name: Load bash library uses: https://gargoton.petite-maison-orange.fr/pmo-actions/bash-library@main - name: Set debug mode if: inputs.debug == 'true' shell: bash run: | source "${{ github.action_path }}/../bashlib.sh" export DEBUG="ON" echo "DEBUG=ON" >> $GITHUB_ENV debug Debug switch on - name: Tash description shell: bash run: | source "${{ github.action_path }}/../bashlib.sh" step "Image name" "${{ inputs.image_name }}" step "Image tags" "${{ inputs.image_tags }}" - name: Build the image shell: bash run: | source "${{ github.action_path }}/../bashlib.sh" check_command ${{ inputs.build_command }} IFS=' ' read -r -a tags <<< "${{ inputs.image_tags }}" debug "Tags parsed: ${tags[*]}" sep="" if [[ -n "${tags[0]}" ]] ; then sep=":" fi step "Start building" "${{ inputs.image_name }}${sep}${tags[0]}" image="${{ inputs.registry_server }}/${{ inputs.image_name }}${sep}${tags[0]}" if [[ ${{ input.no_cache }} == "true" ]] ; then nocache="--pull --no-cache" else nocache="" fi if ${{ inputs.build_command }} build ${nocache} -t ${image} . ; then success "Image build" "${{ inputs.image_name }}" else error "Failed" "Cannot built the image ${{ inputs.image_name }}" fi - name: tag images shell: bash run: | source "${{ github.action_path }}/../bashlib.sh" check_command ${{ inputs.tag_command }} IFS=' ' read -r -a tags <<< "${{ inputs.image_tags }}" sep="" if [[ -n "${tags[0]}" ]] ; then sep=":" fi step "Start tagging" "${{ inputs.image_name }}${sep}${tags[0]}" image=${{ inputs.registry_server }}/${{ inputs.image_name }}${sep}${tags[0]} if [[ "${{ inputs.build_command }}" != "${{ inputs.tag_command }}" ]] ; then step "Transfer image" "From ${{ inputs.build_command }} to ${{ inputs.tag_command }}" 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 rm -f $$.image.tar fi for tag in "${tags[@]:1}" ; do 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 done