Skip to content

Commit

Permalink
fix(docker)!: Proper image tagging 🏷️
Browse files Browse the repository at this point in the history
Now the images are properly tagged. For example, the image php-dev:8 has the same digest as php-dev:8.2. As a bonus, the build workflow has been split, so only the appropriate image will be rebuilt.

BREAKING CHANGE: from now on, the "latest" tag refers to the latest image of this repository, not the latest image of the corresponding service.
  • Loading branch information
gremo committed Jul 3, 2023
1 parent 8f49f55 commit d4e9593
Show file tree
Hide file tree
Showing 6 changed files with 387 additions and 128 deletions.
99 changes: 99 additions & 0 deletions .github/actions/version-meta/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: Version meta
description: Extract the version meta from the given version and comparable versions

inputs:
version:
required: true
description: Version from which to extract information
compare-to:
required: true
description: JSON array containing versions to compare to

outputs:
latest:
description: Whether the version is the latest
value: ${{ steps.version.outputs.latest }}
latestMajor:
description: Whether the version is the latest major (among the same major)
value: ${{ steps.version.outputs.latestMajor }}
latestMinor:
description: Whether the version is the latest minor (among the same major/minor)
value: ${{ steps.version.outputs.latestMinor }}
major:
description: Version's major number
value: ${{ steps.version.outputs.major }}
minor:
description: Version's minor number
value: ${{ steps.version.outputs.minor }}
patch:
description: Version's patch number
value: ${{ steps.version.outputs.patch }}

runs:
using: 'composite'
steps:
- id: version
shell: bash
run: |
version="${{ inputs.version }}"
comparable='${{ inputs.compare-to }}'
versions=$(echo "$comparable" | jq '.[]' | sort -rV | jq -r '.' | tr '\n' ' ')
major=$(echo "$version" | cut -d'.' -f1)
minor=$(echo "$version" | cut -d'.' -f2)
patch=$(echo "$version" | cut -d'.' -f3)
latest=true
latestMajor=true
latestMinor=true
for cversion in $versions; do
if [ "$cversion" = "$version" ]; then
continue
fi
cmajor=$(echo "$cversion" | cut -d. -f1)
cminor=$(echo "$cversion" | cut -d. -f2)
cpatch=$(echo "$cversion" | cut -d. -f3)
# Not the latest if a higher version exists
if [ "$latest" = "true" ] &&
[ "$cmajor" -gt "$major" ] ||
{ [ "$cmajor" = "$major" ] && [ "$cminor" -gt "$minor" ]; } ||
{ [ "$cmajor" = "$major" ] && [ "$cminor" = "$minor" ] && [ "$cpatch" -gt "$patch" ]; }; then
latest=false
fi
# Not the latest major (within the same major) if a higher minor or patch version exists
if [ "$latestMajor" = "true" ] &&
{ [ "$cmajor" = "$major" ] && [ "$cminor" -gt "$minor" ]; } ||
{ [ "$cmajor" = "$major" ] && [ "$cminor" = "$minor" ] && [ "$cpatch" -gt "$patch" ]; }; then
latestMajor=false
fi
# Not the latest minor (within the same major/minor) if a higher patch version exists
if [ "$latestMinor" = "true" ] &&
[ "$cmajor" = "$major" ] && [ "$cminor" = "$minor" ] && [ "$cpatch" -gt "$patch" ]; then
latestMinor=false
fi
done
{
echo "latest=$latest"
echo "latestMajor=$latestMajor"
echo "latestMinor=$latestMinor"
echo "major=$major"
echo "minor=$minor"
echo "patch=$patch"
} >> "$GITHUB_OUTPUT"
echo "::group::Version"
echo "version: $version"
echo "compare-to: $comparable"
echo "versions: $versions"
echo "latest: $latest"
echo "latestMajor: $latestMajor"
echo "latestMinor: $latestMinor"
echo "major: $major"
echo "minor: $minor"
echo "patch: $patch"
echo "::endgroup::"
95 changes: 95 additions & 0 deletions .github/workflows/build_caddy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Build Caddy

on:
push:
branches:
- main
paths:
- '.github/workflows/build_caddy.yaml'
- 'docker/caddy/**'

schedule:
- cron: '0 0 * * *'

workflow_dispatch:

env:
IMAGE_PREFIX: symfonysail
VERSIONS: '["2.6"]'

jobs:
setup:
runs-on: ubuntu-latest
outputs:
versions: ${{ steps.set-output.outputs.versions }}
steps:
- name: Set output
id: set-output
run: |
echo "versions=${{ toJson(env.VERSIONS) }}" >> "$GITHUB_OUTPUT"
build:
runs-on: ubuntu-latest
needs: setup
name: '${{ matrix.target }}:${{ matrix.version }}'
env:
foo: bar
strategy:
fail-fast: false
matrix:
version: ${{ fromJSON(needs.setup.outputs.versions) }}
target: ['caddy-base']

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Version meta
id: version
uses: ./.github/actions/version-meta
with:
version: ${{ matrix.version }}
compare-to: ${{ needs.setup.outputs.versions }}

- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
flavor: |
latest=false
images: ${{ env.IMAGE_PREFIX }}/${{ matrix.target }}
tags: |
type=raw,value=latest,enable=${{ steps.version.outputs.latest }}
type=raw,value=${{ steps.version.outputs.major }},enable=${{ steps.version.outputs.latestMajor }}
type=raw,value=${{ format('{0}.{1}', steps.version.outputs.major, steps.version.outputs.minor) }},enable=${{ steps.version.outputs.latestMinor }}
labels: |
org.opencontainers.image.title=Symfony Sail Caddy image
org.opencontainers.image.description=Caddy base Docker image used by the Symfony Sail project
org.opencontainers.image.version=${{ matrix.version }}
org.opencontainers.image.licenses=ISC
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v4
env:
cache: '${{ matrix.target }}:${{ matrix.version }}'
with:
context: '{{defaultContext}}:docker/caddy'
target: ${{ matrix.target }}
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
push: ${{ github.event_name != 'pull_request' }}
build-args: |
BASE_BUILDER_IMAGE=${{ format('{0}-builder', matrix.version) }}-alpine
BASE_IMAGE=${{ format('{0}-alpine', matrix.version) }}
cache-from: type=gha,scope=${{ env.cache }}
cache-to: type=gha,mode=max,scope=${{ env.cache }}
94 changes: 94 additions & 0 deletions .github/workflows/build_mariadb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Build MariaDB

on:
push:
branches:
- main
paths:
- '.github/workflows/build_mariadb.yaml'
- 'docker/mariadb/**'

schedule:
- cron: '0 0 * * *'

workflow_dispatch:

env:
IMAGE_PREFIX: symfonysail
VERSIONS: '["10.11", "10.10", "10.9", "10.8", "10.7", "10.6"]'

jobs:
setup:
runs-on: ubuntu-latest
outputs:
versions: ${{ steps.set-output.outputs.versions }}
steps:
- name: Set output
id: set-output
run: |
echo "versions=${{ toJson(env.VERSIONS) }}" >> "$GITHUB_OUTPUT"
build:
runs-on: ubuntu-latest
needs: setup
name: '${{ matrix.target }}:${{ matrix.version }}'
env:
foo: bar
strategy:
fail-fast: false
matrix:
version: ${{ fromJSON(needs.setup.outputs.versions) }}
target: ['mariadb-base']

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Version meta
id: version
uses: ./.github/actions/version-meta
with:
version: ${{ matrix.version }}
compare-to: ${{ needs.setup.outputs.versions }}

- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
flavor: |
latest=false
images: ${{ env.IMAGE_PREFIX }}/${{ matrix.target }}
tags: |
type=raw,value=latest,enable=${{ steps.version.outputs.latest }}
type=raw,value=${{ steps.version.outputs.major }},enable=${{ steps.version.outputs.latestMajor }}
type=raw,value=${{ format('{0}.{1}', steps.version.outputs.major, steps.version.outputs.minor) }},enable=${{ steps.version.outputs.latestMinor }}
labels: |
org.opencontainers.image.title=Symfony Sail MariaDB image
org.opencontainers.image.description=MariaDB base Docker image used by the Symfony Sail project
org.opencontainers.image.version=${{ matrix.version }}
org.opencontainers.image.licenses=ISC
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v4
env:
cache: '${{ matrix.target }}:${{ matrix.version }}'
with:
context: '{{defaultContext}}:docker/mariadb'
target: ${{ matrix.target }}
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
push: ${{ github.event_name != 'pull_request' }}
build-args: |
BASE_IMAGE=${{ matrix.version }}
cache-from: type=gha,scope=${{ env.cache }}
cache-to: type=gha,mode=max,scope=${{ env.cache }}
94 changes: 94 additions & 0 deletions .github/workflows/build_php.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Build PHP

on:
push:
branches:
- main
paths:
- '.github/workflows/build_php.yaml'
- 'docker/php/**'

schedule:
- cron: '0 0 * * *'

workflow_dispatch:

env:
IMAGE_PREFIX: symfonysail
VERSIONS: '["8.2", "8.1", "8.0", "7.4"]'

jobs:
setup:
runs-on: ubuntu-latest
outputs:
versions: ${{ steps.set-output.outputs.versions }}
steps:
- name: Set output
id: set-output
run: |
echo "versions=${{ toJson(env.VERSIONS) }}" >> "$GITHUB_OUTPUT"
build:
runs-on: ubuntu-latest
needs: setup
name: '${{ matrix.target }}:${{ matrix.version }}'
env:
foo: bar
strategy:
fail-fast: false
matrix:
version: ${{ fromJSON(needs.setup.outputs.versions) }}
target: ['php-dev', 'php-prod']

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Version meta
id: version
uses: ./.github/actions/version-meta
with:
version: ${{ matrix.version }}
compare-to: ${{ needs.setup.outputs.versions }}

- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
flavor: |
latest=false
images: ${{ env.IMAGE_PREFIX }}/${{ matrix.target }}
tags: |
type=raw,value=latest,enable=${{ steps.version.outputs.latest }}
type=raw,value=${{ steps.version.outputs.major }},enable=${{ steps.version.outputs.latestMajor }}
type=raw,value=${{ format('{0}.{1}', steps.version.outputs.major, steps.version.outputs.minor) }},enable=${{ steps.version.outputs.latestMinor }}
labels: |
org.opencontainers.image.title=Symfony Sail PHP image
org.opencontainers.image.description=PHP ${{ matrix.target == 'php-dev' && 'development' || 'production' }} base Docker image used by the Symfony Sail project
org.opencontainers.image.version=${{ matrix.version }}
org.opencontainers.image.licenses=ISC
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v4
env:
cache: '${{ matrix.target }}:${{ matrix.version }}'
with:
context: '{{defaultContext}}:docker/php'
target: ${{ matrix.target }}
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
push: ${{ github.event_name != 'pull_request' }}
build-args: |
BASE_IMAGE=${{ format('{0}-fpm', matrix.version) }}
cache-from: type=gha,scope=${{ env.cache }}
cache-to: type=gha,mode=max,scope=${{ env.cache }}
Loading

0 comments on commit d4e9593

Please sign in to comment.