siemens / kas

Setup tool for bitbake based projects
MIT License
353 stars 144 forks source link

pass environment variables from cmd line #39

Closed HerrMuellerluedenscheid closed 3 years ago

HerrMuellerluedenscheid commented 3 years ago

Hey, I'm trying to set an environment variable from the command line. In one of my recipes I set:

WANT_TARGET_BRANCH ?= master

How can I set that variable when calling kas? Would come very handy in my CI pipeline. I tried something like

WANT_TARGET_BRANCH=X bash -c 'kas shell dev.yml -c "bitbake my_tool"'

and

kas shell dev.yml -c "WANT_TARGET_BRANCH=X bitbake my_tool"

Neither was working so far. Best

jan-kiszka commented 3 years ago

Have a look at env from https://kas.readthedocs.io/en/latest/userguide.html#project-configuration.

This seems to become a FAQ. Suggestions how to improve to manual to make this easier findable are welcome.

HerrMuellerluedenscheid commented 3 years ago

Thanks @jan-kiszka! Not sure why I overlooked this part.

In case others stumble across the same issue: To make the example from above work, simply put an env block with defaults into your configuration (just as the documentation points out). In the upper case that would be:

env:
  WANT_TARGET_BRANCH = master

Then, invoke kas as follows to set WANT_TARGET_BRANCH from the command line:

kas shell dev.yml -c "WANT_TARGET_BRANCH=X bitbake my_tool"

Hope this little example helps others.

simonbuehler commented 1 year ago

have the same issue and can't seem to get it working with kas-container:

i need to pass a timestamp into yocto so a variable is set and only evaluated once, e.g.

MENDER_ARTIFACT_NAME="${IMAGE_BASENAME}-${MACHINE}-${@time.strftime('%Y%m%d%H%M%S', time.gmtime())}"

would not work as it changes during the build as its re-evaluated multiple times so i'd need to pass a value in from the outside (e.g. CI), is this supported by kas?

jan-kiszka commented 1 year ago

@simonbuehler Your particular issue should resolvable via immediate expansion (https://docs.yoctoproject.org/bitbake/2.2/bitbake-user-manual/bitbake-user-manual-metadata.html#immediate-variable-expansion).

simonbuehler commented 1 year ago

Unfortunately not, it evaluates it as soon as it parses that line, but it may still be evaluated multiple times, any other way to pass a variable value from outside?

simonbuehler commented 1 year ago

@jan-kiszka thanks for your input, for now i have a solution as outlined in #28 but i'd prefer not to use env but rather a parameter passed in for CI usage

jan-kiszka commented 1 year ago

We generally recommend CI parametrization via env vars, though. What issues do you see with it? Yeah, kas-container cannot parse the complete kas config hierarchy, thus needs help via --runtime-args "-e var=value".

simonbuehler commented 1 year ago

Well after a second thought its actually a no-issue with the env vars passed this ways, so handling it like ./kas/kas-container --runtime-args "-e BUILD_TIMESTAMP=$(date +%Y%m%d%H%M%S)" --ssh-dir ~/.ssh/ build kas-mydistro/distro.yml works for me, thanks for the hint!