Skip to content

Instantly share code, notes, and snippets.

@pwalkr
Last active July 24, 2022 02:33
Show Gist options
  • Save pwalkr/67a1ce41e59d245a34aa7b67607b5499 to your computer and use it in GitHub Desktop.
Save pwalkr/67a1ce41e59d245a34aa7b67607b5499 to your computer and use it in GitHub Desktop.
Build docker container with dynamic tag
# This step generates an image tag based on git tree hash of the source folder so
# the tag changes whenever your source changes and will trigger new builds/pulls.
# If the image is tracking an upstream app, I'll add a "{{ base_tag }}-" prefix
# so I get a new image if the source version changes but my wrapper does not.
# This can be hidden in vars/main.yml to avoid a dedicated step
- set_fact:
# Extracted for shorter lines - this command assumes the parent directory to
# roles/ is the root of a git repository.
get_tag_cmd: git rev-parse HEAD:roles/myapp/files/docker
# For apps with an upstream version I'll add a {{ base_tag }}- prefix
image_name: 'local/myapp:{{ lookup("pipe", get_tag_cmd)[:7] }}'
- name: build docker image
community.docker.docker_image:
name: '{{ image_name }}'
source: build
build:
path: '{{ role_path }}/files/docker'
delegate_to: localhost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment