`git push` and buildArgs

I’ve spent a fair amount of cycles bringing Screenly OSE back to it’s former glory recently. As part of the update, I’ve overhauled the Balena support.

One thing that I have struggled a bit with however is build arguments. In short, because Screenly OSE requires build arguments to fully work. This is, among many things, so that we can pull in the right Qt version (which is different for the different pi boards).

As far as I can tell, there are two ways to set docker build arguments in Balena’s pipeline.

  • Using .balena/balena.yml (link)
  • Using balena-cli

Initially, my gut feeling was that balena.yml would be the most straight forward approach, so I wrote a little script that would generate it:

#!/bin/bash

# vim: tabstop=4 shiftwidth=4 softtabstop=4
# -*- sh-basic-offset: 4 -*-

set -euo pipefail

GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
GIT_SHORT_HASH=$(git rev-parse --short HEAD)
GIT_HASH=$(git rev-parse HEAD)

read -p "What is the target device? (pi1-pi4)? " -r DEVICE_TYPE

mkdir -p .balena
cat <<EOF > .balena/balena.yml
build-variables:
  global:
    - GIT_HASH=$GIT_HASH
    - GIT_SHORT_HASH=$GIT_SHORT_HASH
    - GIT_BRANCH=$GIT_BRANCH
  services:
      srly-ose-viewer:
        - DEVICE_TYPE=$DEVICE_TYPE
EOF

However, since this file is unique to (almost each) build, we cannot commit it in the git repo. Hence we need to include it in .gitignore. This of course create a second problem: Balena will not pick it up. Unless I’m missing anything obvious here, this approach will not work.

That bring us to the next option: using the balena-cli. This of course has other downsides, such as adding more complexity for the end-user.

I did play a bit with the source -buildArg (link), but this doesn’t seem like a good fit. Instead, I’d like to use the push feature, but this argument does not seem to support build arguments.

Any pointers here would be helpful.

In a perfect world, I’d like to run/set these as some sort of hook.

I know you already know this @vpetersson but just for reference for anyone else looking, this issue relates also to the comment at the bottom of this github issue…

Hi @vpeterson, what version of balena-cli are you using? Since v12.0.0 balena CLI no longer takes into account .gitignore for the build (only .dockerignore), which unless I’m misunderstanding something would still make it a viable option with your script?

Success! I was able to get this working.

Now to the next issue.

In .balena.yml we have these lines:

  applicationConfigVariables:
    - BALENA_HOST_CONFIG_gpu_mem: 128
    - BALENA_HOST_CONFIG_framebuffer_depth: 32
    - BALENA_HOST_CONFIG_framebuffer_ignore_alpha: 1
    - BALENA_HOST_CONFIG_dtparam: "\"i2c_arm=on\",\"spi=on\",\"audio=on\",\"vc4-kms-v3d\""

Is it possible to also set these values in .balena/balena.yml?

Hi

You can use the configUrl option https://www.balena.io/docs/learn/deploy/deploy-with-balena-button/#balenayml-configuration-file or you could also take look at this example repo - https://github.com/balena-io-playground/example-build-secrets-and-variables/blob/master/.balena/balena.yml

Thanks @anujdeshpande, but I’m a bit confused.

  • There is no ‘configUrl’ option to the balena push command (ref).
  • The Deploy to Balena button does already have all these settings, but they are not being consumed by balena push as far as I can tell.
  • There is no settings for this in .balena/balena.yml (ref). You can only seem to set build-variables and build-secrets there. To my knowledge, these cannot be used for setting the BALENA_HOST_CONFIG_* settings.

Apologies - I misunderstood your original question. I see that you are looking for something specific only to balena push.
So you want to move the .balena.yml file to a custom directory, and want the cli to pull it from that directory. I have pinged the engineer working on the CLI, but perhaps one way around this is to use a makefile which gives you a cleaner interface. So make push would pass the env vars by expanding the push command to include the variables.
Would something like that work or does that feel hack-y to you?

So you want to move the .balena.yml file to a custom directory, and want the cli to pull it from that directory. I have pinged the engineer working on the CLI, but perhaps one way around this is to use a makefile which gives you a cleaner interface.

My understanding is that there are two balena.ymls (which I find confusing). One is in /balena.yml and one is in /.balena/balena.yml. The former appears to be used by Deploy to Balena and the latter by the CLI. Perhaps I’m mistaken, but that is my understanding based on the fact that they have very different structures. Please correct me if I’m wrong.

With regards to using a Makefile, I’m not sure this would be ideal given that it will be used by end-users rather than myself. Also, there’s not anything you can do with a Makefile that you can’t do with a shell script.

You are correct @vpetersson. :+1: And yes, it is confusing to have two balena.yml files. I suspect that the developers who introduced the /balena.yml solution were not aware of (or had forgotten about) the existence of /.balena/balena.yml. Hopefully the files will converge at some point, but for now they serve different purposes and cannot be interchanged.

Thanks for clarifying, @pdcastro.

So i guess this is implicit, but there is no way to either set BALENA_HOST_CONFIG_* in /.balena/balena.yml nor to reference /.balena.yml in there? Basically, I just want to pass these settings on to balena deploy.

The variables and secrets defined in .balena/balena.yml are build-time only. Currenlty there is no support for what you ask through that file, but you may achieve that by running balena env add - https://www.balena.io/docs/reference/balena-cli/#env-add-name-value.

Thanks! That sounds like a viable approach. Just need to write a wrapper script around balena deploy i guess.