Skip to content

Support a new field to set image tag for docker and docker_image languages #2968

@lhriley

Description

@lhriley

search you tried in the issue tracker

rev docker

describe your actual problem

When creating / using hooks based on the docker_image language, it would be really useful to support passing a new tagfield to the function that runs the hook.

This would allow the hook owner to set a floating default (such as latest) in the git repo, but allow users to override this value in order to pin to a specific version of the hook's container image.

This would help to mitigate compatibility issues across major version changes and reduce the overhead of maintaining hooks based on container images in general.

To maintain backward compatibility, the entry string concatenation would only happen in the case that the tag field was not None

This seems relatively simple to implement in this function:

def run_hook(
prefix: Prefix,
entry: str,
args: Sequence[str],
file_args: Sequence[str],
*,
is_local: bool,
require_serial: bool,
color: bool,
) -> tuple[int, bytes]: # pragma: win32 no cover
cmd = docker_cmd() + lang_base.hook_cmd(entry, args)
return lang_base.run_xargs(
cmd,
file_args,
require_serial=require_serial,
color=color,
)

pseudo code:

def run_hook(
        prefix: Prefix,
        entry: str,
        args: Sequence[str],
        file_args: Sequence[str],
        *,
        is_local: bool,
        require_serial: bool,
        color: bool,
) -> tuple[int, bytes]:  # pragma: win32 no cover
    if tag:
       # probably requires regex to match the container name if `entry` contains flags or other options: ie [myhost.foo/]myrepo/mycontainer --foo bar
       container_name, remainder = <regex to get container_name>
       entry = f"{container_name}:{tag}{remainder}"
    cmd = docker_cmd() + lang_base.hook_cmd(entry, args)
    return lang_base.run_xargs(
        cmd,
        file_args,
        require_serial=require_serial,
        color=color,
    )

Examples of proposed hook (source):

new field:

- id: helm-docs-container
  args: []
  description: Uses the container image of 'helm-docs' to create documentation from the Helm chart's 'values.yaml' file, and inserts the result into a corresponding 'README.md' file.
  entry: jnorwood/helm-docs
  tag: v1.11.0
  files: (README\.md\.gotmpl|(Chart|requirements|values)\.yaml)$
  language: docker_image
  name: Helm Docs Container
  require_serial: true
- id: helm-docs-container
  args: []
  description: Uses the container image of 'helm-docs' to create documentation from the Helm chart's 'values.yaml' file, and inserts the result into a corresponding 'README.md' file.
  entry: jnorwood/helm-docs
  tag: latest
  files: (README\.md\.gotmpl|(Chart|requirements|values)\.yaml)$
  language: docker_image
  name: Helm Docs Container
  require_serial: true

pre-commit --version

pre-commit 3.3.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions