I have a GitLab pipeline that builds a simple app, like https://github.com/spring-projects/spring-petclinic using the spring-boot:build-image target. The build works locally, and I deploy or run the image as expected. However, to get deployment working on our lifecycle creator like this:
image: paketobuildpacks/builder
variables:
REGISTRY_GROUP_PROJECT: $CI_REGISTRY/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME
before_script:
- mkdir ~/.docker
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_JOB_TOKEN\"}}}" >> ~/.docker/config.json
script:
- /cnb/lifecycle/creator -app=. $REGISTRY_GROUP_PROJECT:latest
This sets up the Kubernetes runner properly, and I can execute the build as expected, without having to change anything on the pom.xml definition. However, when trying to update one of the dependencies, I have to set an environment variable to one of the layers, as for some reason the current version will not use the correct packaged jar that I want.
So I want to do a very simple export BP_MAVEN_BUILT_MODULE=myModule before the script step - however, this is not picked up by the lifecycle creator at all. The documentation leads me to believe that I cannot set the environment variable without having to create an entire buildpack just for this - I am trying to look for a solution that doesn't require me to change the entire way of how the build is done just for a single environment variable.
I can verify that the environment variable is being ignored because it still uses the default and prints it on the execution steps, but the result is still a failed build. I have tried setting the variable in different ways, but it is never picked up.
Is there something really simple that I am missing, or is there truly no other way than creating a buildpack just for this?
Mind you, I can create the image just fine by running on my machine:
$ pack build myModule --builder paketobuildpacks/builder:base --env BP_MAVEN_BUILT_MODULE=myModule
And it will pick up the variable properly with pack 0.27.0.
SO only recommended me the exact answer that I needed after I posted it... Configure container image labels using CNB Paketo Image-Labels-Buildpack and the lifecycle creator
Only had to change the platform key on the command to have the environment name properly, like this if you want to change the JVM_VERSION to 17:
$ mkdir -p platform/env
$ echo "17.*" >> platform/env/BP_JVM_VERSION
$ /cnb/lifecycle/creator -app=. -platform platform $REGISTRY_GROUP_PROJECT:latest
It is a bit weird to have to create the platform folder and I am not sure whether it will change something else, but @VeryDogeWow had a much better way of asking the same thing.
Sadly with the accepted answer I had: echo "17.*" >> platform/env/BP_JVM_VERSION gives bash: /platform/env/BP_JVM_VERSION: Permission denied, because the directory platform is already created inside the builder image... I guess that the builder images somehow changed since the answer was given - and now contain the folder platform already. But what works is the following:
Create a different platform directory in the cnb user home:
mkdir -p ~/platform/env
Now create the environment variables as described in the buildpack platform spec documentation in the form of
Each file SHALL define a single environment variable, where the file name defines the key and the file contents define the value.
inside the ~/platform/env directory:
echo -n "25" >> ~/platform/env/BP_JVM_VERSION
Inside ~/platform/env you should find a file BP_JVM_VERSION with the content 25.
It is extremely important to use the
-nparameter withecho, since it removes the trailing newline! For me I ran into an odyssey, since there is an issue in the buildpack implementation tracked at https://github.com/paketo-buildpacks/libpak/issues/416, where the newline is causing parameters to NOT WORK with the lifecycle. I can only advise you to double check the contents of the files you create like~/platform/env/BP_JVM_VERSIONin an editor:
wrong (containing newline):

correct (NO newline):

Finally run the /cnb/lifecycle/creator command with an appended -platform /home/cnb/platform (WITHOUT the trailing /env!, otherwise it won't use the environment configuration):
/cnb/lifecycle/creator -app . -platform /home/cnb/platform $REGISTRY_GROUP_PROJECT:latest
I also put together a blog post describing all needed steps in detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With