Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid redirect operator in justfile docker commands #1804

Merged
merged 4 commits into from
Nov 13, 2024

Conversation

Fraser999
Copy link
Contributor

Summary

This replaces sed '...' <<< text with echo text | sed '...' in the main justfile.

Background

On Ubuntu, the default shell doesn't recognise the redirect operator <<<, hence the just commands docker-build and docker-build-and-load both fail on Ubuntu.

Changes

  • Replaced usage of <<< by piping echo output.

Testing

Ran the commands on Ubuntu and macOS.

Changelogs

No updates required.

@Fraser999 Fraser999 requested a review from a team as a code owner November 12, 2024 10:52
@Fraser999 Fraser999 requested review from itamarreif and quasystaty1 and removed request for itamarreif November 12, 2024 10:52
Copy link
Member

@SuperFluffy SuperFluffy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch is needed but still broken. Let's fix it for all users.

justfile Outdated
@@ -14,11 +14,11 @@ default_repo_name := 'ghcr.io/astriaorg'

# Builds docker image for the crate. Defaults to 'local' tag.
docker-build crate tag=default_docker_tag repo_name=default_repo_name:
docker buildx build --load --build-arg TARGETBINARY={{crate}} -f containerfiles/Dockerfile -t {{repo_name}}/$(sed 's/^[^-]*-//' <<< {{crate}}):{{tag}} .
docker buildx build --load --build-arg TARGETBINARY={{crate}} -f containerfiles/Dockerfile -t {{repo_name}}/$(echo {{crate}} | sed 's/^[^-]*-//'):{{tag}} .
Copy link
Member

@SuperFluffy SuperFluffy Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These commands are broken on my system (also before this patch) because I am neither running posix shell nor bash. Let's fix it for all users by prepending it with a #!/usr/bin/env bash (to make it work with the previous HERESTRING) or #!/usr/bin/env sh (to use your command substitution $(...) - we should probably do the latter).

@SuperFluffy
Copy link
Member

On a second note: we should probably just list the commands to build services so that we don't have to rely on the mapping crates/astria/<SERVICE> -> ghcr.io/astriaorg/` always working.

We only have a handful of services so this wouldn't cause too much boilerplate.

justfile Outdated Show resolved Hide resolved
justfile Outdated Show resolved Hide resolved
justfile Outdated
docker-build crate tag=default_docker_tag repo_name=default_repo_name: (_crate_short_name crate "quiet")
#!/usr/bin/env sh
set -eu
short_name=`just _crate_short_name {{crate}}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
short_name=`just _crate_short_name {{crate}}`
short_name=$(just _crate_short_name {{crate}})

My comment re command substitution was due to my shell (fish), but setting #!/usr/bin/env sh fixes this (and backticks is probably more confusing nowadays than $()).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave this as is, since Just uses backticks natively, so I guess this is a little less surprising in this context.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disagree: just uses that for its own DSL (the portion you linked), while here we have an inline script and command substitution is considered better practice and is strictly preferred over backticks.

justfile Outdated
docker-build-and-load crate tag=default_docker_tag repo_name=default_repo_name: (_crate_short_name crate "quiet")
#!/usr/bin/env sh
set -eu
short_name=`just _crate_short_name {{crate}}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
short_name=`just _crate_short_name {{crate}}`
short_name=$(just _crate_short_name {{crate}})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above

# Maps a crate name to the shortened name used in the docker tag.
# If `quiet` is an empty string the shortened name will be echoed. If `quiet` is a non-empty string,
# the only output will be in the case of an error, where the input `crate` is not a valid one.
_crate_short_name crate quiet="":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of prefixing with _, maybe an attribute makes it clearer?

Suggested change
_crate_short_name crate quiet="":
#[private]
crate_short_name crate:

You will note I also removed the quiet argument - I am not 100% we need it? I notice it is being used as an input to both docker-* recipes, but you also have set -eu to fail early. Can we just rely on that or is there extra use with having the arg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're using the leading underscore style elsewhere in this file rather than the [private] attribute, so I'll leave as is.

We need the quiet arg to silence the output on success. The recipe is being invoked twice in each of the two parent recipes.

The first time it is being specified as a dependency so that it is invoked before running the parent. This is purely to validate the specified crate is a supported one. If that invocation fails, we get a clear error message and exit code 2. In the success case, without the quiet arg, we'd get the shortened crate name printed to the console, which is confusing and needless noise. So in that invocation, we set quiet to a non-empty string to suppress the output on success.

The second time it is called is in the body of the parent recipe, and in that case we require the successful output to populate our shell variable short_name, so we set quiet to an empty string.

Without the first call (when we call it as a dependency), if we only called in the body and the specified crate was invalid, we'd get no helpful error message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the explanation!

justfile Show resolved Hide resolved
Copy link
Member

@SuperFluffy SuperFluffy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check if we need to also add the CLI to the list of built services.

@quasystaty1
Copy link
Contributor

There's a minor issue with the build logic for the cli Docker image unrelated to this PR. The astria-cli package is the only one prefixed with astria, causing the just command to tag the local image as ghcr.io/astriaorg/cli:local instead of ghcr.io/astriaorg/astria-cli:local. For consistency and specific use cases, this should ideally be updated, though it’s not something we use regularly

@Fraser999 Fraser999 added this pull request to the merge queue Nov 13, 2024
@Fraser999
Copy link
Contributor Author

Agreed with @quasystaty1 that the CLI issue will be handled in a follow-up PR.

Merged via the queue into main with commit 6708727 Nov 13, 2024
46 checks passed
@Fraser999 Fraser999 deleted the fraser/justfile-fix branch November 13, 2024 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants