Skip to content

Build and Publish Docker Demo #14

Build and Publish Docker Demo

Build and Publish Docker Demo #14

name: Build and Publish Docker Demo
on:
push:
tags:
- "v*"
workflow_dispatch:
inputs:
version:
description: "Image version to publish (e.g. 1.7.0). Required unless run from a v* tag."
required: false
type: string
env:
# Set the DOCKER_IMAGE repository variable to publish to a different Docker Hub
# repo; DOCKER_USERNAME/DOCKER_PASSWORD must be able to push to it.
REGISTRY_IMAGE: ${{ vars.DOCKER_IMAGE || 'stackoverflowdocker/stackoverflow-backstage-demo' }}
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
# Changesets releases create per-package tags, not v* tags, so a manual
# run from a branch has no semver ref to derive image tags from. The
# `version` input supplies it; on a v* tag push it is empty and the git
# tag is used instead.
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
tags: |
type=semver,pattern={{version}},value=${{ inputs.version }}
type=semver,pattern={{major}}.{{minor}},value=${{ inputs.version }}
type=raw,value=latest,enable=${{ github.ref_type == 'tag' || inputs.version != '' }}
- name: Check image tags
if: steps.meta.outputs.tags == ''
run: |
echo "::error::No image tags were generated. Run this workflow from a v* tag, or pass a semver 'version' input (e.g. 1.7.0)."
exit 1
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push (amd64)
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Inspect image
run: |
docker buildx imagetools inspect \
${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}