diff --git a/README.md b/README.md index 79dc423..c9c605f 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ These keys mirror the [Swarmia Deployment API](https://help.swarmia.com/settings | `docker-image` | Docker Image | | | `docker-custom-tag` | Docker Custom Tag to be set on the image | | | `docker-tag-timestamp` | Insert a UTC timestamp into `dev`/`main`/`master` branch tags (`dev--`) to make them sortable for Flux image automation | `false` | +| `docker-tag-keep-v-prefix` | Keep the leading `v` on release (`v*`) tags (`v1.2.3` → `v1.2.3`). Default strips it (`v1.2.3` → `1.2.3`) | `false` | | `docker-username` | Username for the Docker Registry | | | `docker-password` | Password for the Docker Registry | | | `docker-file` | Dockerfile | `./Dockerfile` | @@ -163,7 +164,7 @@ The generated image tag depends on the Git ref: | `dev` branch | `dev-` | `dev--` | `dev` | | `main` branch | `main-` | `main--` | `main` | | `master` branch | `master-` | `master--` | `master` | -| `v*` tag (prod) | the version, e.g. `2025.50.14` (CalVer) | _(unchanged)_ | `latest` | +| `v*` tag (prod) | the version with the `v` stripped, e.g. `v2025.50.14` → `2025.50.14` (or kept with `docker-tag-keep-v-prefix: 'true'`) | _(unchanged)_ | `latest` | | other branch | `` (not pushed) | _(unchanged)_ | — | By default branch tags keep the legacy `-` shape. Set diff --git a/action.yml b/action.yml index 2950518..53fa5b9 100644 --- a/action.yml +++ b/action.yml @@ -21,6 +21,10 @@ inputs: description: 'Insert a UTC timestamp into dev/main/master branch tags (e.g. dev-20260602143055-) to make them sortable for Flux image automation. Opt-in; defaults to the legacy - format.' required: false default: 'false' + docker-tag-keep-v-prefix: + description: 'Keep the leading "v" on release (v*) tags (v1.2.3 -> v1.2.3). Opt-in; defaults to stripping it (v1.2.3 -> 1.2.3).' + required: false + default: 'false' docker-username: description: 'Username for the Docker Registry' required: false @@ -117,6 +121,7 @@ runs: env: INPUT_DOCKER_CUSTOM_TAG: ${{ inputs.docker-custom-tag }} INPUT_DOCKER_TAG_TIMESTAMP: ${{ inputs.docker-tag-timestamp }} + INPUT_DOCKER_TAG_KEEP_V_PREFIX: ${{ inputs.docker-tag-keep-v-prefix }} INPUT_DOCKER_DISABLE_RETAGGING: ${{ inputs.docker-disable-retagging }} INPUT_DOCKER_REGISTRY: ${{ inputs.docker-registry }} INPUT_DOCKER_IMAGE: ${{ inputs.docker-image }} diff --git a/scripts/generate-tags.sh b/scripts/generate-tags.sh index 036154a..9ca3a75 100755 --- a/scripts/generate-tags.sh +++ b/scripts/generate-tags.sh @@ -2,7 +2,8 @@ # Generates Docker image tags based on the current Git ref. # # Required env vars: GITHUB_REF, GITHUB_SHA, INPUT_DOCKER_REGISTRY, INPUT_DOCKER_IMAGE -# Optional env vars: INPUT_DOCKER_CUSTOM_TAG, INPUT_DOCKER_DISABLE_RETAGGING +# Optional env vars: INPUT_DOCKER_CUSTOM_TAG, INPUT_DOCKER_DISABLE_RETAGGING, +# INPUT_DOCKER_TAG_TIMESTAMP, INPUT_DOCKER_TAG_KEEP_V_PREFIX # # Outputs (via GITHUB_OUTPUT): build, latest, push, tag, tag_list @@ -65,7 +66,13 @@ elif [[ $GITHUB_REF == refs/heads/dev ]]; then LATEST="dev" PUSH="true" elif [[ $GITHUB_REF == refs/tags/v* ]]; then - TAG="${GITHUB_REF:11}" + # By default the leading "v" is stripped (v1.2.3 -> 1.2.3). Set + # INPUT_DOCKER_TAG_KEEP_V_PREFIX=true to keep it (v1.2.3 -> v1.2.3). + if [[ "${INPUT_DOCKER_TAG_KEEP_V_PREFIX:-false}" == "true" ]]; then + TAG="${GITHUB_REF#refs/tags/}" + else + TAG="${GITHUB_REF:11}" + fi LATEST="latest" PUSH="true" BUILD="${INPUT_DOCKER_DISABLE_RETAGGING:-false}" diff --git a/tests/generate-tags.bats b/tests/generate-tags.bats index 387a9aa..515eb53 100644 --- a/tests/generate-tags.bats +++ b/tests/generate-tags.bats @@ -14,6 +14,8 @@ setup() { # Timestamp suffix is opt-in; default off so the legacy - format # is the baseline. Tests that exercise the suffix set the flag explicitly. unset INPUT_DOCKER_TAG_TIMESTAMP + # Keeping the "v" prefix on release tags is opt-in; default off. + unset INPUT_DOCKER_TAG_KEEP_V_PREFIX # Pin the timestamp so the opt-in branch tags are deterministic in tests. export BUILD_TIMESTAMP="20260602143055" } @@ -143,6 +145,31 @@ teardown() { assert_output_value "push" "true" } +@test "version tag strips the v prefix by default" { + export GITHUB_REF="refs/tags/v1.2.3" + run "$SCRIPT" + assert_success + assert_output_value "tag" "1.2.3" +} + +@test "version tag keeps the v prefix when flag is enabled" { + export INPUT_DOCKER_TAG_KEEP_V_PREFIX="true" + export GITHUB_REF="refs/tags/v1.2.3" + run "$SCRIPT" + assert_success + assert_output_value "tag" "v1.2.3" + assert_output_value "latest" "latest" + assert_output_value "push" "true" +} + +@test "keep-v-prefix flag does not affect non-v tags" { + export INPUT_DOCKER_TAG_KEEP_V_PREFIX="true" + export GITHUB_REF="refs/tags/release-1" + run "$SCRIPT" + assert_success + assert_output_value "tag" "release-1" +} + @test "version tag uses docker-disable-retagging for build flag" { export GITHUB_REF="refs/tags/v1.2.3" export INPUT_DOCKER_DISABLE_RETAGGING="true"