Skip to content

Release Image

Release Image #14

Workflow file for this run

name: Release Image
on:
workflow_call:
inputs:
docker_tag:
description: 'Docker image tag, e.g. latest'
required: true
type: string
default: 'latest'
build_context:
description: 'Docker build context, default is current directory, e.g. ./Docker/hello-world'
required: true
type: string
default: './Docker/hello-world'
docker_file_name:
description: 'Docker file name in the build context, e.g. Dockerfile, default is Dockerfile'
required: false
type: string
default: 'Dockerfile'
build_platforms:
description: 'Docker build platforms, e.g. linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/ppc64le,linux/s390x,linux/386,linux/riscv64'
required: false
type: string
default: 'linux/amd64'
docker_image_name:
description: 'Docker image name, e.g. hello-world, default is the last part of the build context. If the build context is ./Docker/hello-world, the image name is hello-world'
required: false
default: ''
type: string
build_args:
description: 'Docker build args list, e.g. VERSION=1.0.0,ENV=prod'
required: false
type: string
default: 'BUILD_DATE=$(date +%Y-%m-%d)'
workflow_dispatch:
inputs:
docker_tag:
description: 'Docker image tag, e.g. latest'
required: true
type: string
default: 'latest'
build_context:
description: 'Docker build context, default is current directory, e.g. ./Docker/hello-world'
required: true
type: string
default: './Docker/hello-world'
docker_file_name:
description: 'Docker file name in the build context, e.g. Dockerfile, default is Dockerfile'
required: false
type: string
default: 'Dockerfile'
build_platforms:
description: 'Docker build platforms, e.g. linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/ppc64le,linux/s390x,linux/386,linux/riscv64'
required: false
type: string
default: 'linux/amd64'
docker_image_name:
description: 'Docker image name, e.g. hello-world, default is the last part of the build context. If the build context is ./Docker/hello-world, the image name is hello-world'
required: false
default: ''
type: string
build_args:
description: 'Docker build args list, e.g. VERSION=1.0.0,ENV=prod'
required: false
type: string
default: 'BUILD_DATE=$(date +%Y-%m-%d)'
permissions:
packages: write
contents: read
jobs:
setup:
name: Setup
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch' || github.event_name == 'workflow_call' || github.event_name == 'push'
outputs:
IMAGE_FULL_NAME: ${{ steps.set_vars.outputs.IMAGE_FULL_NAME }}
IMAGE_NAME: ${{ steps.set_vars.outputs.IMAGE_NAME }}
IMAGE_TAG: ${{ steps.set_vars.outputs.IMAGE_TAG }}
BUILD_CONTEXT: ${{ steps.determine-context.outputs.BUILD_CONTEXT }}
BUILD_FILE: ${{ steps.set_vars.outputs.BUILD_FILE }}
BUILD_PLATFORMS: ${{ github.event.inputs.build_platforms || inputs.build_platforms }}
BUILD_ARGS: ${{ steps.set_vars.outputs.BUILD_ARGS }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Output inputs for debugging
run: |
echo "The build tag is ${{ github.event.inputs.docker_tag || inputs.docker_tag }}"
echo "The build context is ${{ github.event.inputs.build_context || inputs.build_context }}"
echo "The docker image name is ${{ github.event.inputs.docker_image_name || inputs.docker_image_name }}"
echo "The build args are ${{ github.event.inputs.build_args || inputs.build_args }}"
echo "The build platforms are ${{ github.event.inputs.build_platforms || inputs.build_platforms }}"
echo "The docker file is ${{ github.event.inputs.docker_file_name || inputs.docker_file_name }}"
- name: Determine Docker Context
id: determine-context
run: |
echo "BUILD_CONTEXT=${{ github.event.inputs.build_context || inputs.build_context }}" >> $GITHUB_OUTPUT
- name: Check context exist
run: |
if [ ! -d "${{ steps.determine-context.outputs.BUILD_CONTEXT }}" ]; then
echo "Docker context ${{ steps.determine-context.outputs.BUILD_CONTEXT }} does not exist"
exit 1
fi
- name: Set vars
id: set_vars
run: |
CONTEXT="${{ steps.determine-context.outputs.build_context }}"
DOKCER_IMAGE_TAG="${{ github.event.inputs.docker_tag || inputs.docker_tag }}"
if [ -z "${{ github.event.inputs.docker_image_name || inputs.docker_image_name }}" ]; then
DOCKER_IMAGE_NAME=$(basename $CONTEXT)
else
DOCKER_IMAGE_NAME="${{ github.event.inputs.docker_image_name || inputs.docker_image_name }}"
fi
BUILD_ARGS="${{ github.event.inputs.build_args || inputs.build_args }}"
if [ -z "$BUILD_ARGS" ]; then
BUILD_ARGS="BUILD_DATE=$(date +%Y-%m-%d)"
fi
{
echo "IMAGE_FULL_NAME=${{ github.actor }}/${DOCKER_IMAGE_NAME}:${DOKCER_IMAGE_TAG}"
echo "IMAGE_NAME=${{ github.actor }}/$DOCKER_IMAGE_NAME"
echo "IMAGE_TAG=$DOKCER_IMAGE_TAG"
echo "BUILD_ARGS=$BUILD_ARGS"
echo "BUILD_FILE=${{ steps.determine-context.outputs.BUILD_CONTEXT }}/${{ github.event.inputs.docker_file_name || inputs.docker_file_name || 'Dockerfile' }}"
} >> $GITHUB_OUTPUT
docker-release:
name: Publish Docker images
needs: [setup]
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Log in to ALIYUNCS
uses: docker/login-action@v3
with:
registry: registry.cn-beijing.aliyuncs.com
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_ALIYUNCS_PASSWORD }}
- name: Log in to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Output build description
id: output_build_description
run: |
echo "Docker Build context: ${{ needs.setup.outputs.BUILD_CONTEXT }}"
echo "Docker Image name: ${{ needs.setup.outputs.IMAGE_NAME }}"
echo "Docker Image full name: ${{ needs.setup.outputs.IMAGE_FULL_NAME }}"
echo "Docker Build platforms: ${{ needs.setup.outputs.BUILD_PLATFORMS }}"
echo "Docker file: ${{ needs.setup.outputs.BUILD_FILE }}"
echo "Docker tag: ${{ needs.setup.outputs.IMAGE_TAG }}"
echo "Docker build args: ${{ needs.setup.outputs.BUILD_ARGS }}"
BUILD_TRIGGER_DESCRIPTION="Docker Image Release triggered by ${{ github.event_name }} event, Bulld Docker image ${{ needs.setup.outputs.IMAGE_FULL_NAME }} with tag ${{ needs.setup.outputs.IMAGE_TAG }} from ${{ needs.setup.outputs.BUILD_FILE }} in ${{ needs.setup.outputs.BUILD_CONTEXT }} with build args ${{ needs.setup.outputs.BUILD_ARGS }} and platforms ${{ needs.setup.outputs.BUILD_PLATFORMS }} on ${{ github.run_id }}"
echo "BUILD_TRIGGER_DESCRIPTION=$BUILD_TRIGGER_DESCRIPTION" >> $GITHUB_OUTPUT
echo "Description: $BUILD_TRIGGER_DESCRIPTION"
- name: Split Build Args
id: split_build_args
run: |
IFS=',' read -r -a build_args <<< "${{ needs.setup.outputs.BUILD_ARGS }}"
for i in {1..7}; do
eval "BUILD_ARGS_$i=${build_args[$i-1]:-}"
echo "BUILD_ARGS_$i=${build_args[$i-1]:-}" >> $GITHUB_OUTPUT
done
- name: Docker build and push
uses: docker/build-push-action@v6
with:
context: ${{ needs.setup.outputs.BUILD_CONTEXT }}
file: ${{ needs.setup.outputs.BUILD_FILE }}
push: true
tags: |
${{ needs.setup.outputs.IMAGE_FULL_NAME }}
registry.cn-beijing.aliyuncs.com/${{ needs.setup.outputs.IMAGE_FULL_NAME }}
ghcr.io/${{ needs.setup.outputs.IMAGE_FULL_NAME }}
platforms: ${{ needs.setup.outputs.BUILD_PLATFORMS }}
build-args: |
${{ steps.split_build_args.outputs.BUILD_ARGS_1 }}
${{ steps.split_build_args.outputs.BUILD_ARGS_2 }}
${{ steps.split_build_args.outputs.BUILD_ARGS_3 }}
${{ steps.split_build_args.outputs.BUILD_ARGS_4 }}
${{ steps.split_build_args.outputs.BUILD_ARGS_5 }}
${{ steps.split_build_args.outputs.BUILD_ARGS_6 }}
${{ steps.split_build_args.outputs.BUILD_ARGS_7 }}
- name: Send Apprise Notification
run: |
curl -X POST \
-F "tag=devops" \
-F "body=${{ steps.output_build_description.outputs.BUILD_TRIGGER_DESCRIPTION }} DockerHub: https://hub.docker.com/r/${{ needs.setup.outputs.IMAGE_NAME }}/tags" \
"${{ secrets.APPRISE_HTTP_URL }}"