name: image # Reusable workflow: builds one image and tags it with the docker/metadata-action tag set # shared by every repo: # # - every build (Flux image automation sorts on it) # 1.2.3, 1.2, 1 on a v1.2.3 git tag (no "1" while major is 0) # latest on the default branch # # The timestamp is the commit time, not the build time, so the tag depends only on the # commit: a repo with several images calls this once per image (the calls run in # parallel) and all of them get the same tag. Rebuilding a commit re-pushes its tag. # # Images go to //, and to docker.io// # when the Docker Hub secrets are passed. # # One image per call: Gitea 1.27.0 can't expand a matrix built from inputs in a called # workflow (the run is silently not created). # # Caller: # # jobs: # test: ... # image: # needs: test # uses: traberph/ci/.gitea/workflows/image.yaml@v1.0.0 # with: # project: private # image: cv # secrets: # registry-username: ${{ secrets.REGISTRY_USERNAME }} # registry-token: ${{ secrets.REGISTRY_TOKEN }} # # optional, also pushes to Docker Hub # dockerhub-username: ${{ secrets.DOCKERHUB_USERNAME }} # dockerhub-token: ${{ secrets.DOCKERHUB_TOKEN }} on: workflow_call: inputs: registry: type: string default: registry.traberph.de project: description: Harbor project, e.g. public or private. type: string required: true image: description: Image name, in Harbor and on Docker Hub. type: string required: true context: type: string default: "." dockerfile: description: Defaults to /Dockerfile. type: string default: "" platforms: type: string default: linux/amd64,linux/arm64 build-args: description: Newline-separated KEY=value pairs. type: string default: "" push: description: Push the image. Pass false for pull requests. type: boolean default: true cache: description: Keep the layer cache in Harbor as :buildcache. type: boolean default: false secrets: registry-username: required: false registry-token: required: false # Optional: with both set, the image is also pushed to docker.io//. dockerhub-username: required: false dockerhub-token: required: false outputs: tag: description: The timestamp tag, -. value: ${{ jobs.build.outputs.tag }} version: description: The version metadata-action resolved (1.2.3 on v1.2.3, else the timestamp tag). value: ${{ jobs.build.outputs.version }} tags: description: Newline-separated full image references that were pushed. value: ${{ jobs.build.outputs.tags }} jobs: build: runs-on: ubuntu-latest outputs: tag: ${{ steps.ts.outputs.tag }} version: ${{ steps.meta.outputs.version }} tags: ${{ steps.meta.outputs.tags }} env: IMAGE: ${{ inputs.registry }}/${{ inputs.project }}/${{ inputs.image }} # Secrets can't be used in `if:` directly, env can. DOCKERHUB: ${{ secrets.dockerhub-username != '' && secrets.dockerhub-token != '' }} DOCKERHUB_USERNAME: ${{ secrets.dockerhub-username }} steps: - uses: actions/checkout@v7 - id: ts run: | ts="$(TZ=UTC git log -1 --format=%cd --date=format-local:%Y%m%d%H%M%S)" # same as metadata-action's {{sha}} sha="$(echo "${GITHUB_SHA}" | cut -c1-7)" images="${IMAGE}" if [ "${DOCKERHUB}" = "true" ]; then images="${images} docker.io/${DOCKERHUB_USERNAME}/${{ inputs.image }}" fi { echo "ts=${ts}" echo "tag=${ts}-${sha}" echo "images<> "$GITHUB_OUTPUT" - id: meta uses: docker/metadata-action@v6 with: images: ${{ steps.ts.outputs.images }} flavor: latest=false tags: | type=raw,value=${{ steps.ts.outputs.ts }}-{{sha}} type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/v0.') }} type=raw,value=latest,enable={{is_default_branch}} - if: contains(inputs.platforms, 'arm') uses: docker/setup-qemu-action@v4 - uses: docker/setup-buildx-action@v4 - if: inputs.push uses: docker/login-action@v4 with: registry: ${{ inputs.registry }} username: ${{ secrets.registry-username }} password: ${{ secrets.registry-token }} - if: inputs.push && env.DOCKERHUB == 'true' uses: docker/login-action@v4 with: username: ${{ secrets.dockerhub-username }} password: ${{ secrets.dockerhub-token }} - uses: docker/build-push-action@v7 with: context: ${{ inputs.context }} file: ${{ inputs.dockerfile || format('{0}/Dockerfile', inputs.context) }} platforms: ${{ inputs.platforms }} push: ${{ inputs.push }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} build-args: ${{ inputs.build-args }} cache-from: ${{ inputs.cache && format('type=registry,ref={0}:buildcache', env.IMAGE) || '' }} cache-to: ${{ inputs.cache && inputs.push && format('type=registry,ref={0}:buildcache,mode=max,image-manifest=true,oci-mediatypes=true', env.IMAGE) || '' }}