Skip to content

🐳 Docker Publish πŸŽ† #35

🐳 Docker Publish πŸŽ†

🐳 Docker Publish πŸŽ† #35

name: 🐳 Docker Publish πŸŽ†
# This workflow is designed to build a Docker image, upload it to Docker Hub,
# and create a GitHub release with the image tar file every time there's a commit
# on branches other than 'main'.
on:
workflow_dispatch:
inputs:
tag:
description: "Tag for the release e.g. (v1.0.0)"
required: true
ref:
description: "Branch to pull from"
required: true
env:
REGISTRY: docker.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
outputs:
image_tag: ${{ steps.set_image_tag.outputs.IMAGE_TAG }}
ref: ${{ steps.set_ref.outputs.ref }}
runs-on: ubuntu-latest
steps:
- name: Determine ref
id: set_ref
run: |
echo "ref=${{ github.repository_id }}" >> $GITHUB_OUTPUT
shell: bash
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ steps.set_ref.outputs.ref }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKER_REGISTRY_USER }}
password: ${{ secrets.DOCKER_REGISTRY_PASSWD }}
- name: Determine IMAGE_TAG
id: set_image_tag
run: |
echo "IMAGE_TAG=${{ github.event.inputs.ref }}-${{ github.event.inputs.tag }}" >> $GITHUB_OUTPUT
shell: bash
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.set_image_tag.outputs.IMAGE_TAG }}
cache-from: type=gha
cache-to: type=gha,mode=max
create-release:
needs: build-and-push
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ needs.build-and-push.outputs.ref }}
- name: Load Docker image
run: |
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.build-and-push.outputs.image_tag }}
docker save ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.build-and-push.outputs.image_tag }} > image.tar
- name: Upload Docker image as artifact
uses: actions/upload-artifact@v4
with:
name: docker-image
path: image.tar
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ needs.build-and-push.outputs.image_tag }}
release_name: Release ${{ needs.build-and-push.outputs.image_tag }}
draft: false
prerelease: false
body: "Release of ${{ env.IMAGE_NAME }}:${{ needs.build-and-push.outputs.image_tag }}"
# - name: Upload Docker image to GitHub Release
# uses: actions/upload-release-asset@v1
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# with:
# upload_url: ${{ steps.create_release.outputs.upload_url }}
# asset_path: ./image.tar
# asset_name: docker-image-${{ needs.build-and-push.outputs.image_tag }}.tar
# asset_content_type: application/octet-stream