From 61439adf9b1b7caa135155f951ee8d9c834a4df3 Mon Sep 17 00:00:00 2001 From: Frederik Baetens Date: Thu, 22 Apr 2021 22:03:59 +0200 Subject: [PATCH 1/2] describe github.run number as a reliable increasing build number Signed-off-by: Frederik Baetens --- docs/guides/sortable-image-tags.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/guides/sortable-image-tags.md b/docs/guides/sortable-image-tags.md index 49f78761..ff064911 100644 --- a/docs/guides/sortable-image-tags.md +++ b/docs/guides/sortable-image-tags.md @@ -36,6 +36,7 @@ $ date +%s Alternatively, you can use a serial number as part of the tag. Some CI platforms will provide a build number in an environment variable, but that may not be reliable to use as a serial number -- check the platform documentation. +For example, Github makes availabe the variable `github.run_number` which can be used as a reliable ever increasing serial number. A commit count can be a reasonable stand-in for a serial number, if you build an image per commit and you don't rewrite the branch in question: @@ -112,7 +113,7 @@ using the `tagFilter` field. You can filter for a particular branch to restrict built from that branch. Here is an example that filters for only images built from `main` branch, and selects the most -recent according the timestamp (created with `date +%s`): +recent according to a timestamp (created with `date +%s`) or according to the run number (`github.run_number` for example): ```yaml apiVersion: image.toolkit.fluxcd.io/v1alpha2 From c9e0bc0807ffe41d161a67b4819165b939922c26 Mon Sep 17 00:00:00 2001 From: Frederik Baetens Date: Thu, 22 Apr 2021 22:11:32 +0200 Subject: [PATCH 2/2] add github.run number github actions workflow example Signed-off-by: Frederik Baetens --- docs/guides/sortable-image-tags.md | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/docs/guides/sortable-image-tags.md b/docs/guides/sortable-image-tags.md index ff064911..cbf7b8ad 100644 --- a/docs/guides/sortable-image-tags.md +++ b/docs/guides/sortable-image-tags.md @@ -106,6 +106,39 @@ jobs: ${{ env.IMAGE }}:${{ steps.prep.outputs.BUILD_ID }} ``` +### Alternative example utilizing github.run_number + +Here is another example example of a [GitHub Actions job][gha-syntax] which tags images using Github action's built in `run_number` +and the git SHA1: + +```yaml +jobs: + build-push: + env: + IMAGE: org/my-app + runs-on: ubuntu-latest + steps: + # These are prerequisites for the docker build step + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and publish container image with tag + uses: docker/build-push-action@v2 + with: + push: true + context: . + file: ./Dockerfile + tags: | + ${{ env.IMAGE }}:${{ github.sha }}-${{ github.run_number }} +``` + ## Using in an `ImagePolicy` object When creating an `ImagePolicy` object, you will need to extract just the timestamp part of the tag, @@ -125,6 +158,7 @@ spec: imageRepositoryRef: name: image-repo filterTags: + ## use "pattern: '(?P.*)-.+'" if you copied the workflow example using github.run_number pattern: '^main-[a-f0-9]+-(?P[0-9]+)' extract: '$ts' policy: