Jenkins @Library load dynamic branch from variable

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Jenkins @Library load dynamic branch from variable

Chris Carpenter
Is there a way to dynamically load the library from dynamic branch variable like this:

@Library("jenkins-pipeline@${env.BRANCH_NAME}") _

In a multi branch pipeline you have access to the current branch name in the environment variable `BRANCH_NAME`. I'd like to be able to test changes to my pipeline before committing them to master branch potentially breaking builds.

I get the following warning in the console output:

WorkflowScript: @Library value jenkins-pipeline@$env.BRANCH_NAME was not a constant; did you mean to use the library step instead?


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/ca65b535-4225-41ef-9497-ac69a10098c2%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Jenkins @Library load dynamic branch from variable

Jan Monterrubio
Check the loading dynamic libraries portion. It might do what you want. 

On Wed, Jan 15, 2020 at 15:12 Chris Carpenter <[hidden email]> wrote:
Is there a way to dynamically load the library from dynamic branch variable like this:

@Library("jenkins-pipeline@${env.BRANCH_NAME}") _

In a multi branch pipeline you have access to the current branch name in the environment variable `BRANCH_NAME`. I'd like to be able to test changes to my pipeline before committing them to master branch potentially breaking builds.

I get the following warning in the console output:

WorkflowScript: @Library value jenkins-pipeline@$env.BRANCH_NAME was not a constant; did you mean to use the library step instead?


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/ca65b535-4225-41ef-9497-ac69a10098c2%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CADgiF9KA3CF_jfGc95b_gscAceu9172WwZL_TcS9vnOyDfj-ww%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Jenkins @Library load dynamic branch from variable

Chris Carpenter
It seems with this you lose some functionality with code in the `src` directory.

Using classes from the src/ directory is also possible, but trickier. Whereas the @Library annotation prepares the “classpath” of the script prior to compilation, by the time a library step is encountered the script has already been compiled. Therefore you cannot import or otherwise “statically” refer to types from the library.

Is there a way to specify the branch dynamically in the global library scm settings? I see in the legacy git loader There is reference to env variables in the `Branches to build` section.

${ENV_VARIABLE}
It is also possible to use environment variables. In this case the variables are evaluated and the result is used as described above.
E.g. ${TREEISH}, refs/tags/${TAGNAME},... 

Is this able to load the branch based on the current multibranch pipeline env? I didn't see any variable reference in the modern scm git config.

On Wednesday, January 15, 2020 at 11:59:29 PM UTC-5, Jan Monterrubio wrote:
<a href="https://jenkins.io/doc/book/pipeline/shared-libraries/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjenkins.io%2Fdoc%2Fbook%2Fpipeline%2Fshared-libraries%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzi5Xo827_lODtglxm9wkc-kRKTg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjenkins.io%2Fdoc%2Fbook%2Fpipeline%2Fshared-libraries%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzi5Xo827_lODtglxm9wkc-kRKTg&#39;;return true;">https://jenkins.io/doc/book/pipeline/shared-libraries/

Check the loading dynamic libraries portion. It might do what you want. 

On Wed, Jan 15, 2020 at 15:12 Chris Carpenter <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="kt2_RUEXCwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">chriscar...@...> wrote:
Is there a way to dynamically load the library from dynamic branch variable like this:

@Library("jenkins-pipeline@${env.BRANCH_NAME}") _

In a multi branch pipeline you have access to the current branch name in the environment variable `BRANCH_NAME`. I'd like to be able to test changes to my pipeline before committing them to master branch potentially breaking builds.

I get the following warning in the console output:

WorkflowScript: @Library value jenkins-pipeline@$env.BRANCH_NAME was not a constant; did you mean to use the library step instead?


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="kt2_RUEXCwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jenkins...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/ca65b535-4225-41ef-9497-ac69a10098c2%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/ca65b535-4225-41ef-9497-ac69a10098c2%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/ca65b535-4225-41ef-9497-ac69a10098c2%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/ca65b535-4225-41ef-9497-ac69a10098c2%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/7c19684f-ca4a-4bcb-90f5-9cb7aca16233%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Jenkins @Library load dynamic branch from variable

Jan Monterrubio
When testing library changes, I usually make a new folder and specify the branch I am testing there. I’ve never tried to do the dynamic environment variable. 

Is your end goal to integration test your library before merging it to master? We do something similar by standing up a local Jenkins and using docker based agents. 

On Thu, Jan 16, 2020 at 07:01 Chris Carpenter <[hidden email]> wrote:
It seems with this you lose some functionality with code in the `src` directory.

Using classes from the src/ directory is also possible, but trickier. Whereas the @Library annotation prepares the “classpath” of the script prior to compilation, by the time a library step is encountered the script has already been compiled. Therefore you cannot import or otherwise “statically” refer to types from the library.

Is there a way to specify the branch dynamically in the global library scm settings? I see in the legacy git loader There is reference to env variables in the `Branches to build` section.

${ENV_VARIABLE}
It is also possible to use environment variables. In this case the variables are evaluated and the result is used as described above.
E.g. ${TREEISH}, refs/tags/${TAGNAME},... 

Is this able to load the branch based on the current multibranch pipeline env? I didn't see any variable reference in the modern scm git config.

On Wednesday, January 15, 2020 at 11:59:29 PM UTC-5, Jan Monterrubio wrote:
Check the loading dynamic libraries portion. It might do what you want. 

On Wed, Jan 15, 2020 at 15:12 Chris Carpenter <[hidden email]> wrote:
Is there a way to dynamically load the library from dynamic branch variable like this:

@Library("jenkins-pipeline@${env.BRANCH_NAME}") _

In a multi branch pipeline you have access to the current branch name in the environment variable `BRANCH_NAME`. I'd like to be able to test changes to my pipeline before committing them to master branch potentially breaking builds.

I get the following warning in the console output:

WorkflowScript: @Library value jenkins-pipeline@$env.BRANCH_NAME was not a constant; did you mean to use the library step instead?


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/ca65b535-4225-41ef-9497-ac69a10098c2%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/7c19684f-ca4a-4bcb-90f5-9cb7aca16233%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CADgiF9LKaYktERDQZeQn2ZTAdjMosevwHn9sXk-BjUA6%2B%2B0m4Q%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Jenkins @Library load dynamic branch from variable

Chris Carpenter
Yes it's mostly to ease testing changes. Our pipelines are tied to kubernetes/openshift so there's a lot of interaction there that I need to make sure a whole pipeline still runs. Was hoping to dynamically update the right branch of the pipeline library so we don't accidentally commit a to master when it's ready.

On Thursday, January 16, 2020 at 1:56:07 PM UTC-5, Jan Monterrubio wrote:
When testing library changes, I usually make a new folder and specify the branch I am testing there. I’ve never tried to do the dynamic environment variable. 

Is your end goal to integration test your library before merging it to master? We do something similar by standing up a local Jenkins and using docker based agents. 

On Thu, Jan 16, 2020 at 07:01 Chris Carpenter <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="FsJA8ehECwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">chriscar...@...> wrote:
It seems with this you lose some functionality with code in the `src` directory.

Using classes from the src/ directory is also possible, but trickier. Whereas the @Library annotation prepares the “classpath” of the script prior to compilation, by the time a library step is encountered the script has already been compiled. Therefore you cannot import or otherwise “statically” refer to types from the library.

Is there a way to specify the branch dynamically in the global library scm settings? I see in the legacy git loader There is reference to env variables in the `Branches to build` section.

${ENV_VARIABLE}
It is also possible to use environment variables. In this case the variables are evaluated and the result is used as described above.
E.g. ${TREEISH}, refs/tags/${TAGNAME},... 

Is this able to load the branch based on the current multibranch pipeline env? I didn't see any variable reference in the modern scm git config.

On Wednesday, January 15, 2020 at 11:59:29 PM UTC-5, Jan Monterrubio wrote:
<a href="https://jenkins.io/doc/book/pipeline/shared-libraries/" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjenkins.io%2Fdoc%2Fbook%2Fpipeline%2Fshared-libraries%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzi5Xo827_lODtglxm9wkc-kRKTg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjenkins.io%2Fdoc%2Fbook%2Fpipeline%2Fshared-libraries%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzi5Xo827_lODtglxm9wkc-kRKTg&#39;;return true;">https://jenkins.io/doc/book/pipeline/shared-libraries/

Check the loading dynamic libraries portion. It might do what you want. 

On Wed, Jan 15, 2020 at 15:12 Chris Carpenter <[hidden email]> wrote:
Is there a way to dynamically load the library from dynamic branch variable like this:

@Library("jenkins-pipeline@${env.BRANCH_NAME}") _

In a multi branch pipeline you have access to the current branch name in the environment variable `BRANCH_NAME`. I'd like to be able to test changes to my pipeline before committing them to master branch potentially breaking builds.

I get the following warning in the console output:

WorkflowScript: @Library value jenkins-pipeline@$env.BRANCH_NAME was not a constant; did you mean to use the library step instead?


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/ca65b535-4225-41ef-9497-ac69a10098c2%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/ca65b535-4225-41ef-9497-ac69a10098c2%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/ca65b535-4225-41ef-9497-ac69a10098c2%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/ca65b535-4225-41ef-9497-ac69a10098c2%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="FsJA8ehECwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jenkins...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/7c19684f-ca4a-4bcb-90f5-9cb7aca16233%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/7c19684f-ca4a-4bcb-90f5-9cb7aca16233%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/7c19684f-ca4a-4bcb-90f5-9cb7aca16233%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/7c19684f-ca4a-4bcb-90f5-9cb7aca16233%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/aa3c1694-036d-48ae-b54d-2513a13ce0b4%40googlegroups.com.