Skip to main content

Earthly Conditional Tags

I was able to work on ghc-musl a bit today, and one of the things I worked on is the new tag syntax. I attempted to implement conditional tags, where users can enable/disable certain tags via command-line arguments, and I discovered that there are some limitations. Thankfully, it is able to do what I need for this project.

New images will be tagged like the following:

  • ghc-musl:ghc9.2.4-alpine3.16.1-20220730
  • ghc-musl:ghc9.2.4-alpine3.16.1
  • ghc-musl:ghc9.2.4

This allows users to specify image names according to their needs. An image tagged with the GHC version, Alpine version, and (UTC) build date never changes. An image tagged with just the GHC version and Alpine version may be updated to include minor/security package releases, but the fixed Alpine version means that major upgrades should not break builds. An image tagged with just the GHC version may be updated to include new package releases, both major and minor.

Updating the GHC-only tag should be done with caution, so that it always points to an image using the latest (supported) Alpine version. First, I tried adding the follow arguments to the image target in order to allow users to enable/disable certain tags via command-line arguments:

ARG TAG_DATE=1
ARG TAG_ALPINE=0
ARG TAG_GHC=0

The idea was to allow use of command-line arguments to specify which tags are created/updated, as follows:

IF [ "$TAG_DATE" = "1" ]
  SAVE IMAGE --push "${IMAGE_NAME}:ghc${GHC}-alpine${ALPINE_VERSION}-${DATE}"
END
IF [ "$TAG_ALPINE" = "1" ]
  SAVE IMAGE --push "${IMAGE_NAME}:ghc${GHC}-alpine${ALPINE_VERSION}"
END
IF [ "$TAG_GHC" = "1" ]
  SAVE IMAGE --push "${IMAGE_NAME}:ghc${GHC}"
END

Unfortunately, this does not work! Earthly gives the following error message:

Error: build target: build main: failed to solve:
Earthfile line 103:2 apply BUILD +image: earthfile2llb for +image:
Earthfile line 95:2 no non-push commands allowed after a --push
in      +image --GHC=9.2.4
in      +ghc9.2.4

I only really care about enabling/disabling the GHC-only tag, though. Earthly does not allow non-push commands after a push command, but that allows for the first push command to be conditional. I removed the TAG_DATE and TAG_ALPINE flags, and now use the following:

IF [ "$TAG_GHC" = "1" ]
  SAVE IMAGE --push "${IMAGE_NAME}:ghc${GHC}"
END
SAVE IMAGE --push "${IMAGE_NAME}:ghc${GHC}-alpine${ALPINE_VERSION}"
SAVE IMAGE --push "${IMAGE_NAME}:ghc${GHC}-alpine${ALPINE_VERSION}-${DATE}"

This works!

Author

Travis Cardwell

Published

Tags
Related Blog Entries