name: Build and Push Docker Image
on:
release:
types: [published]
workflow_dispatch:
inputs:
release_tag:
description: "Release tag to build Docker image from"
required: false
type: string
env:
DOCKER_HUB_USERNAME: eholk
IMAGE_NAME: eholk/ebg
jobs:
build-and-push:
name: Build and Push Docker Image
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ inputs.release_tag || github.ref }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ env.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
- name: Get version from Cargo.toml
id: get_version
run: |
if [ "${{ github.event_name }}" = "release" ]; then
# For release events, use the tag
VERSION=${GITHUB_REF#refs/tags/v}
elif [ -n "${{ inputs.release_tag }}" ]; then
# For workflow_dispatch with release_tag, use the provided tag
VERSION=${{ inputs.release_tag }}
# Remove 'v' prefix if present
VERSION=${VERSION#v}
else
# For manual triggers without release_tag, extract from Cargo.toml
VERSION=$(grep '^version = ' Cargo.toml | sed 's/version = "\(.*\)"/\1/')
fi
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Get Rust version from rust-toolchain.toml
id: get_rust_version
run: |
RUST_CHANNEL=$(grep -oP 'channel\s*=\s*"\K[^"]+' rust-toolchain.toml)
echo "RUST_CHANNEL=$RUST_CHANNEL" >> $GITHUB_ENV
echo "rust_channel=$RUST_CHANNEL" >> $GITHUB_OUTPUT
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=semver,pattern={{version}},value=${{ steps.get_version.outputs.version }}
type=semver,pattern={{major}}.{{minor}},value=${{ steps.get_version.outputs.version }}
type=semver,pattern={{major}},value=${{ steps.get_version.outputs.version }}
type=raw,value=latest
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
RUST_VERSION=${{ steps.get_rust_version.outputs.rust_channel }}
EBG_VERSION=${{ steps.get_version.outputs.version }}