Skip to content

🐳 Docker Publish πŸŽ† #31

🐳 Docker Publish πŸŽ†

🐳 Docker Publish πŸŽ† #31

name: Docker CI/CD
# 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"
required: true
ref:
description: "Branch to pull from"
required: false
push:
branches-ignore:
- "main"
tags:
- "v*.*.*"
paths-ignore:
- ./github
env:
REGISTRY: docker.io
IMAGE_NAME: ${{ github.repository }}
# IMAGE_TAG: ${{ github.ref_name }}-${{ github.sha }}
jobs:
build-and-push:
outputs:
image_tag: ${{ steps.set_image_tag.outputs.IMAGE_TAG }}
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- 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 ref
id: set_ref
run: |
if [[ ${{ github.event.inputs.ref }} ]]; then
echo "ref=${{ github.event.inputs.ref }}" >> $GITHUB_OUTPUT
else
echo "ref=${{ github.ref_name }}" >> $GITHUB_OUTPUT
fi
shell: bash
- name: Determine IMAGE_TAG
id: set_image_tag
run: |
if [[ ${{ github.event.inputs.tag }} ]]; then
echo "IMAGE_TAG=${{ github.event.inputs.tag }}" >> $GITHUB_OUTPUT
else
echo "IMAGE_TAG=${{ steps.set_ref.outputs.ref }}-${{ github.sha }}" >> $GITHUB_OUTPUT
fi
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.tag }}
create-release:
needs: build-and-push
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install Docker
run: |
sudo apt-get update
sudo apt-get install -y docker.io
- 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: ${{ github.ref }}
release_name: Release ${{ github.sha }}
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