Skip to content

Commit

Permalink
Add support for manual-tag
Browse files Browse the repository at this point in the history
- now supports a manual tag override through `manual-tag`.
- special `build-arg` value `{version}` replaced with `{tag}`.
  • Loading branch information
rgaudin committed Jan 8, 2021
1 parent 809eacc commit 3577c47
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v5

- now supports a manual tag override through `manual-tag`.
- special `build-arg` value `{version}` replaced with `{tag}`.

# v4

- added support for any registries (still defaults to docker.io + ghcr.io)
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ on:
- master
tags:
- v*
workflow_dispatch:
inputs:
version:
description: Specific version to build
required: false
default: ''

jobs:
build-and-push:
Expand All @@ -71,7 +77,8 @@ jobs:
latest-on-tag: true
restrict-to: openzim/zimit
build-args:
VERSION={version}
VERSION={tag}
manual-tag: ${{ github.event.inputs.version }}
```
**Note**: th top-part `on` is just a filter on running that workflow. You can omit it but it's safer to not run it on refs that you know won't trigger anything. See [documentation](https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#on).
Expand All @@ -83,11 +90,12 @@ jobs:
| `credentials`<font color=red>\*</font> | **List of credentials for all registries**<br />Use the `REGISTRY_USERNAME=xxx` and `REGISTRY_TOKEN=xxx` formats to specify.<br />`REGISTRY` refers to the uppercase registry domain name without `.`.<br />Ex: `GHCRIO_USERNAME=xxx` for `ghcr.io`.<br />_Notes_: Github token is a [PAT](https://github.com/settings/tokens) with `repo, workflow, write:packages` permissions.<br />Docker hub token is account password.|
| `context` | **Path in the repository to use as build context**<br />Relative to repository root. Ex: `dnscache` or `workers/slave`.<br />Defaults to `.`. |
| `dockerfile` | **Path to the Dockerfile recipe, relative to context**<br />Use `../` syntax if dockerfile is outside context.<br />Defaults to `Dockerfile`. |
| `build-args` | **Arguments for `docker build --build-arg`**<br />Special value `{version}` will be replaced with the tag to set.<br />Use the `name=value` format and separate each by a space or new line.|
| `build-args` | **Arguments for `docker build --build-arg`**<br />Special value `{tag}` will be replaced with the tag to set.<br />Use the `name=value` format and separate each by a space or new line.|
| `platforms` | **List of platforms to build-for**.<br />Ex.: `linux/armv/v7 linux/amd64`.<br />Defaults to `linux/amd64`. |
| `on-master` | **Tag to apply for every commit on default branch**.<br />Omit it if you don't want to push an image for non-tagged commits.<br />Only applies to commits on your default branch (`master` or `main`) |
| `tag-pattern` | **Regular expression to match tags with**.<br />Only git tags matching this regexp will trigger a build+push to the corresponding docker tag.<br />If not specifying a group, whole git tag is used as is on docker. |
| `latest-on-tag` | **Whether to push to docker tag `:latest` on every matched tag** (see `tag-pattern`)<br />Value must be `true` or `false`. Defaults to `false`. |
| `latest-on-tag` | **Whether to push to docker tag `:latest` on every matched tag** (see `tag-pattern`)<br />Also applies to `manual-tag`.<br />Value must be `true` or `false`. Defaults to `false`. |
| `manual-tag` | **Manual tag override**<br />Replaces `on-master` and `tag-pattern` if not empty.<br />Also triggers `:latest` if `latest-on-tag` is `true`. |
| `restrict-to` | **Don't push if action is run for a different repository**<br />Specify as `{owner}/{repository}`. |


Expand Down
9 changes: 7 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ inputs:
required: false
default: Dockerfile
build-args:
description: optional key/value pairs to pass as build arguments. {version} replaced with found version/latest (if any)
description: optional key/value pairs to pass as build arguments. {tag} replaced with found version (if any)
required: false
platforms:
description: specify platform to build for (one of linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6, etc)
Expand All @@ -38,9 +38,13 @@ inputs:
description: pattern to trigger image tagging (ex. 'dnscache-v*')
required: false
latest-on-tag:
description: should matched tag be tagged as 'latest' also? (true or false)
description: should matched tag (or manual-tag) be tagged as 'latest' also? (true or false)
required: false
default: false
manual-tag:
description: Specify the tag manually. Overrides on-master and tag-pattern.
required: false
default: ''
restrict-to:
description: repository path to limit this action to (ex. 'openzim/zimfarm') to prevent forks from triggering it.
required: false
Expand All @@ -62,6 +66,7 @@ runs:
ON_MASTER: ${{ inputs.on-master }}
TAG_PATTERN: ${{ inputs.tag-pattern }}
LATEST_ON_TAG: ${{ inputs.latest-on-tag }}
MANUAL_TAG: ${{ inputs.manual-tag }}
RESTRICT_TO: ${{ inputs.restrict-to }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
DOCKER_BUILDX_VERSION: 0.5.1
Expand Down
1 change: 1 addition & 0 deletions check_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def main():
"ON_MASTER",
"BUILD_ARGS",
"TAG_PATTERN",
"MANUAL_TAG",
"RESTRICT_TO",
"DOCKER_BUILDX_VERSION",
]
Expand Down
2 changes: 1 addition & 1 deletion docker_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def build_and_push_from_env():

if build_args:
for arg, value in build_args.items():
if value == "{version}":
if value == "{tag}":
value = tag
build_cmd += ["--build-arg", f"{arg}={value}"]

Expand Down
7 changes: 6 additions & 1 deletion find_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
def find_tag_from_env():

docker_tag_for_master = os.getenv("ON_MASTER")
manual_tag = os.getenv("MANUAL_TAG", "")
version_tag, latest = "", False

ref = os.getenv("GITHUB_REF").split("/", 2)[-1]
is_tag = os.getenv("GITHUB_REF").startswith("refs/tags/")
tag_regexp = os.getenv("TAG_PATTERN", "")

# manual override tag is set
if manual_tag:
version_tag = manual_tag
latest = os.getenv("LATEST_ON_TAG", "").lower() == "true"
# this is a commit on tag
if is_tag and tag_regexp:
elif is_tag and tag_regexp:
# convert from perl syntax (/pattern/) to python one
perl_re = re.compile(r"^/(.+)/$")
if perl_re.match(tag_regexp):
Expand Down
31 changes: 31 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def get_env(
# event config
is_on_main_branch=False,
is_tag=None,
manual_tag="",
):
if is_on_main_branch:
ref = "refs/heads/main"
Expand All @@ -46,6 +47,7 @@ def get_env(
"ON_MASTER": on_master or "",
"TAG_PATTERN": tag_pattern or "",
"LATEST_ON_TAG": "true" if latest_on_tag else "false",
"MANUAL_TAG": manual_tag,
}


Expand Down Expand Up @@ -245,3 +247,32 @@ def test_tag_without_tag_pattern(github_env, repo_name):
)
assert not tag
assert not latest


def test_manual_tag(github_env, repo_name):
tag, latest = launch_and_retrieve(
**get_env(
github_env=github_env,
repo_name=repo_name,
image_name="openzim/zimfarm-task-worker",
is_tag="uploader-v1.1.1",
manual_tag="toto",
)
)
assert tag == "toto"
assert not latest


def test_manual_tag_latest(github_env, repo_name):
tag, latest = launch_and_retrieve(
**get_env(
github_env=github_env,
repo_name=repo_name,
image_name="openzim/zimfarm-task-worker",
is_tag="uploader-v1.1.1",
manual_tag="toto",
latest_on_tag=True,
)
)
assert tag == "toto"
assert latest

0 comments on commit 3577c47

Please sign in to comment.