name: Docker Build + Push description: Build and push Docker images for Gitea Actions using the docker daemon at DOCKER_HOST author: eric inputs: dockerfile: description: Path to Dockerfile (relative to build context) default: Dockerfile context: description: Build context path (relative to repo root) default: . image-name: description: Full image name (e.g. registry/image or image) required: true tags: description: Additional tags as CSV appended to defaults default: "" registry: description: Docker registry for login (e.g. ghcr.io) default: "" registry-username: description: Docker registry username default: "" registry-password: description: Docker registry password (use secrets.DOCKER_PASSWORD) default: "" push: description: Push images to registry after build default: "false" build-args: description: Build args as CSV KEY=VALUE default: "" outputs: image: description: Full image name with tag value: ${{ steps.meta.outputs.image }} tag: description: The primary tag used (date-SHA) value: ${{ steps.meta.outputs.tag }} digest: description: Image digest (only available after push) value: ${{ steps.push-digest.outputs.digest }} runs: using: composite env: DOCKER_HOST: tcp://docker.local:2375 DOCKER_BUILDKIT: "1" steps: - name: Compute metadata id: meta shell: bash run: | # Generate tags TAGS="latest" if [ -n "${{ inputs.tags }}" ]; then TAGS="$TAGS,${{ inputs.tags }}" fi SHORT_SHA="$(git rev-parse --short HEAD 2>/dev/null || echo 'local')" DATE_TAG="$(date +%Y-%m-%d)" TAG="$DATE_TAG-$SHORT_SHA" TAGS="$TAGS,$TAG" # Add git tag if this is a tag push if [[ "${{ github.ref }}" == refs/tags/* ]]; then TAG_NAME="${{ github.ref_name }}" TAGS="$TAGS,$TAG_NAME" fi echo "image=${{ inputs.image-name }}" >> $GITHUB_OUTPUT echo "tag=$TAG" >> $GITHUB_OUTPUT echo "tags=$TAGS" >> $GITHUB_OUTPUT - name: Log in to Docker registry shell: bash run: | if [ -n "${{ inputs.registry }}" ] && [ -n "${{ inputs.registry-password }}" ]; then echo "${{ inputs.registry-password }}" | docker login ${{ inputs.registry }} -u ${{ inputs.registry-username }} --password-stdin fi - name: Build image id: build shell: bash run: | BUILD_ARGS_FLAG="" if [ -n "${{ inputs.build-args }}" ]; then for arg in $(echo "${{ inputs.build-args }}" | tr ',' ' '); do BUILD_ARGS_FLAG="$BUILD_ARGS_FLAG --build-arg=$arg" done fi set -x docker build \ --tag "${{ inputs.image-name }}:${{ steps.meta.outputs.tag }}" \ --tag "${{ inputs.image-name }}:latest" \ $(for t in $(echo ${{ steps.meta.outputs.tags }} | tr ',' ' '); do echo -n "--tag ${{ inputs.image-name }}:$t "; done) \ --file ${{ inputs.context }}/${{ inputs.dockerfile }} \ $BUILD_ARGS_FLAG \ ${{ inputs.context }} - name: Push image id: push if: inputs.push == 'true' shell: bash run: | set -x docker push "${{ inputs.image-name }}:${{ steps.meta.outputs.tag }}" docker push "${{ inputs.image-name }}:latest" for t in $(echo ${{ steps.meta.outputs.tags }} | tr ',' ' '); do docker push "${{ inputs.image-name }}:$t" done - name: Get image digest id: push-digest shell: bash if: inputs.push == 'true' run: | DIGEST="$(docker inspect "${{ inputs.image-name }}:${{ steps.meta.outputs.tag }}" --format '{{.Digest}}' 2>/dev/null)" echo "digest=$DIGEST" >> $GITHUB_OUTPUT