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

[JENKINS-53535] Make Bitbucket Server, Owner and repository environment variables for Bitbucket Team/Project based jobs #949

Merged
merged 1 commit into from
Dec 29, 2024

Conversation

nfalco79
Copy link
Member

Add as environment variables some bitbucket useful informations like:

  • repository name (slug);
  • owner name (slug);
  • project key;
  • the server URL.


private final String owner;
private final String repository;
private final String projectKey;

Check warning

Code scanning / Jenkins Security Scan

Jenkins: Plaintext password storage Warning

Field should be reviewed whether it stores a password and is serialized to disk: projectKey
@nfalco79 nfalco79 force-pushed the feature/JENKINS-53535 branch 4 times, most recently from 505d307 to da0a382 Compare December 22, 2024 17:45
@nfalco79 nfalco79 marked this pull request as ready for review December 29, 2024 15:08
@nfalco79
Copy link
Member Author

It looks like neither buildEnv.groovy nor buildEnv.jelly are considered to contribute the "Pipeline Syntax -> Global Variables Reference".

I was looking around when I read this comment of @jglick in a resource file.

@MarkEWaite Is this feature not yet enabled for those who extend GitSCMExtension.populateEnvironmentVariables?

@nfalco79 nfalco79 force-pushed the feature/JENKINS-53535 branch 4 times, most recently from 1fb669f to ba76107 Compare December 29, 2024 18:10
…nt variables for Bitbucket Team/Project based jobs

Add as environment variables some bitbucket useful informations like:
- repository name (slug);
- owner name (slug);
- project key;
- the server URL.
@nfalco79 nfalco79 force-pushed the feature/JENKINS-53535 branch from ba76107 to 72753d2 Compare December 29, 2024 18:20
@nfalco79 nfalco79 merged commit 54952c4 into 933.x Dec 29, 2024
17 checks passed
@nfalco79 nfalco79 deleted the feature/JENKINS-53535 branch December 29, 2024 18:40
nfalco79 added a commit that referenced this pull request Dec 29, 2024
…nt variables for Bitbucket Team/Project based jobs (#949)

Add as environment variables some bitbucket useful informations like:
- repository name (slug);
- owner name (slug);
- project key;
- the server URL.
@nfalco79
Copy link
Member Author

Documentation will be available when JENKINS-75072 will be implemented

@SunMar
Copy link

SunMar commented Dec 30, 2024

@nfalco79 Thank you for the nice feature! When are these environment variables populated? I just updated to the latest master cloudbees-bitbucket-branch-source-1000.0.0952.vd3b_c076c4c41.hpi (can't use the normal release yet due to #866) but I do not see these new environment variables appear in when running a Multibranch Pipeline job. I added a simple echo sh(returnStdout: true, script: 'env') after the checkout scm step to see which environment variables are available, but there are no BITBUCKET_* variables in there. We are using Bitbucket Data Center 9.2. Not sure if it's because of the master version or something I haven't setup correctly.

For PRs there already was the CHANGE_URL variable, which I use in my post-build notification, but I actually still had on my todo list to also add a link for branches and tags, which would be much simpler to construct with these environment variables.

@nfalco79
Copy link
Member Author

nfalco79 commented Jan 2, 2025

When are these environment variables populated?

Are contributed but git extensions that means after a checkout scm step.
cloudbees-bitbucket-branch-source-1000.0.0952.vd3b_c076c4c41.hpi

Just to be frank I had test only in branch 933.x, let me see if using new git-plugin works as expected

@nfalco79
Copy link
Member Author

nfalco79 commented Jan 2, 2025

@SunMar it was my fault! When I cherry pick the commit into master I failed to resolve conflicts, the BitbucketEnvVarExtension it's never instantiated.

@SunMar
Copy link

SunMar commented Jan 2, 2025

@nfalco79 yes it works now, thank you!

@SunMar
Copy link

SunMar commented Jan 2, 2025

@nfalco79 it works but when examining my Jenkins controller logs (for something unrelated) I noticed this come by for every build:

jenkins-controller  | 2025-01-02T08:36:12.282918394Z 2025-01-02 08:36:12.281+0000 [id=552]      WARNING o.j.p.s.d.DescribableParameter#uncoerce: failed to uncoerce com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension@f1d1b63
jenkins-controller  | 2025-01-02T08:36:12.282962321Z java.lang.UnsupportedOperationException: no public field ‘owner’ (or getter method) found in class com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension

It's a warning and everything works, also the BITBUCKET_OWNER variable is populated with the same value as the project key. Not sure if that "owner" is the same as the one from the warning though.

EDIT: here is the full stack trace:

jenkins-controller  | 2025-01-02T08:40:46.142739049Z 2025-01-02 08:40:46.142+0000 [id=877]      WARNING o.j.p.s.d.DescribableParameter#uncoerce: failed to uncoerce com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension@451b73ce
jenkins-controller  | 2025-01-02T08:40:46.142794842Z java.lang.UnsupportedOperationException: no public field ‘owner’ (or getter method) found in class com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension
jenkins-controller  | 2025-01-02T08:40:46.142800853Z    at PluginClassLoader for structs//org.jenkinsci.plugins.structs.describable.DescribableParameter.getValue(DescribableParameter.java:161)
jenkins-controller  | 2025-01-02T08:40:46.142803107Z    at PluginClassLoader for structs//org.jenkinsci.plugins.structs.describable.DescribableParameter.inspect(DescribableParameter.java:142)
jenkins-controller  | 2025-01-02T08:40:46.142804798Z    at PluginClassLoader for structs//org.jenkinsci.plugins.structs.describable.DescribableModel.uninstantiate2(DescribableModel.java:662)
jenkins-controller  | 2025-01-02T08:40:46.142806489Z    at PluginClassLoader for structs//org.jenkinsci.plugins.structs.describable.DescribableModel.uninstantiate2_(DescribableModel.java:771)
jenkins-controller  | 2025-01-02T08:40:46.142808085Z    at PluginClassLoader for structs//org.jenkinsci.plugins.structs.describable.DescribableParameter.uncoerce(DescribableParameter.java:196)
jenkins-controller  | 2025-01-02T08:40:46.142809682Z    at PluginClassLoader for structs//org.jenkinsci.plugins.structs.describable.DescribableParameter.uncoerce(DescribableParameter.java:190)
jenkins-controller  | 2025-01-02T08:40:46.142811185Z    at PluginClassLoader for structs//org.jenkinsci.plugins.structs.describable.DescribableParameter.inspect(DescribableParameter.java:142)
jenkins-controller  | 2025-01-02T08:40:46.142812876Z    at PluginClassLoader for structs//org.jenkinsci.plugins.structs.describable.DescribableModel.uninstantiate2(DescribableModel.java:662)
jenkins-controller  | 2025-01-02T08:40:46.142814566Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.actions.ArgumentsActionImpl.sanitizeObjectAndRecordMutation(ArgumentsActionImpl.java:211)
jenkins-controller  | 2025-01-02T08:40:46.142816163Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.actions.ArgumentsActionImpl.sanitizeMapAndRecordMutation(ArgumentsActionImpl.java:327)
jenkins-controller  | 2025-01-02T08:40:46.142817854Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.actions.ArgumentsActionImpl.sanitizeStepArguments(ArgumentsActionImpl.java:314)
jenkins-controller  | 2025-01-02T08:40:46.142836169Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.actions.ArgumentsActionImpl.<init>(ArgumentsActionImpl.java:77)
jenkins-controller  | 2025-01-02T08:40:46.142838330Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:280)
jenkins-controller  | 2025-01-02T08:40:46.142840020Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:196)
jenkins-controller  | 2025-01-02T08:40:46.142841523Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:124)
jenkins-controller  | 2025-01-02T08:40:46.142843120Z    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:47)
jenkins-controller  | 2025-01-02T08:40:46.142844717Z    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
jenkins-controller  | 2025-01-02T08:40:46.142846219Z    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
jenkins-controller  | 2025-01-02T08:40:46.142847722Z    at PluginClassLoader for workflow-cps//com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
jenkins-controller  | 2025-01-02T08:40:46.142849225Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.LoggingInvoker.methodCall(LoggingInvoker.java:117)
jenkins-controller  | 2025-01-02T08:40:46.142850822Z    at PluginClassLoader for workflow-cps//com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:90)
jenkins-controller  | 2025-01-02T08:40:46.142852325Z    at PluginClassLoader for workflow-cps//com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:116)
jenkins-controller  | 2025-01-02T08:40:46.142853827Z    at PluginClassLoader for workflow-cps//com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:85)
jenkins-controller  | 2025-01-02T08:40:46.142855518Z    at jdk.internal.reflect.GeneratedMethodAccessor99.invoke(Unknown Source)
jenkins-controller  | 2025-01-02T08:40:46.142857115Z    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
jenkins-controller  | 2025-01-02T08:40:46.142858618Z    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
jenkins-controller  | 2025-01-02T08:40:46.142860214Z    at PluginClassLoader for workflow-cps//com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
jenkins-controller  | 2025-01-02T08:40:46.142861717Z    at PluginClassLoader for workflow-cps//com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:75)
jenkins-controller  | 2025-01-02T08:40:46.142863314Z    at PluginClassLoader for workflow-cps//com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
jenkins-controller  | 2025-01-02T08:40:46.142865099Z    at PluginClassLoader for workflow-cps//com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:65)
jenkins-controller  | 2025-01-02T08:40:46.142866789Z    at jdk.internal.reflect.GeneratedMethodAccessor106.invoke(Unknown Source)
jenkins-controller  | 2025-01-02T08:40:46.142868386Z    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
jenkins-controller  | 2025-01-02T08:40:46.142869983Z    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
jenkins-controller  | 2025-01-02T08:40:46.142873458Z    at PluginClassLoader for workflow-cps//com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
jenkins-controller  | 2025-01-02T08:40:46.142875149Z    at PluginClassLoader for workflow-cps//com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
jenkins-controller  | 2025-01-02T08:40:46.142876745Z    at PluginClassLoader for workflow-cps//com.cloudbees.groovy.cps.Next.step(Next.java:83)
jenkins-controller  | 2025-01-02T08:40:46.142878154Z    at PluginClassLoader for workflow-cps//com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:147)
jenkins-controller  | 2025-01-02T08:40:46.142879657Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:17)
jenkins-controller  | 2025-01-02T08:40:46.142881254Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:49)
jenkins-controller  | 2025-01-02T08:40:46.142882851Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:180)
jenkins-controller  | 2025-01-02T08:40:46.142884447Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:422)
jenkins-controller  | 2025-01-02T08:40:46.142886044Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:330)
jenkins-controller  | 2025-01-02T08:40:46.142887547Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:294)
jenkins-controller  | 2025-01-02T08:40:46.142889050Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.lambda$wrap$4(CpsVmExecutorService.java:140)
jenkins-controller  | 2025-01-02T08:40:46.142890647Z    at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
jenkins-controller  | 2025-01-02T08:40:46.142892149Z    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
jenkins-controller  | 2025-01-02T08:40:46.142893558Z    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
jenkins-controller  | 2025-01-02T08:40:46.142895249Z    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
jenkins-controller  | 2025-01-02T08:40:46.142896752Z    at jenkins.util.ErrorLoggingExecutorService.lambda$wrap$0(ErrorLoggingExecutorService.java:51)
jenkins-controller  | 2025-01-02T08:40:46.142898536Z    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
jenkins-controller  | 2025-01-02T08:40:46.142900039Z    at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
jenkins-controller  | 2025-01-02T08:40:46.142901542Z    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
jenkins-controller  | 2025-01-02T08:40:46.142902951Z    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
jenkins-controller  | 2025-01-02T08:40:46.142904454Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$1.call(CpsVmExecutorService.java:53)
jenkins-controller  | 2025-01-02T08:40:46.142906050Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$1.call(CpsVmExecutorService.java:50)
jenkins-controller  | 2025-01-02T08:40:46.142907741Z    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:136)
jenkins-controller  | 2025-01-02T08:40:46.142910935Z    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:275)
jenkins-controller  | 2025-01-02T08:40:46.142933759Z    at PluginClassLoader for workflow-cps//org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.lambda$categoryThreadFactory$0(CpsVmExecutorService.java:50)
jenkins-controller  | 2025-01-02T08:40:46.142935543Z    at java.base/java.lang.Thread.run(Unknown Source)


def l = namespace(lib.JenkinsTagLib)

// Base on javadoc in EnvironmentContributor Jenkins provides other extension points (such as SCM) to contribute environment variables to builds, and for those plugins, Jenkins also looks for /buildEnv.groovy and aggregates them.
Copy link
Member

Choose a reason for hiding this comment

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

Does this file actually work? I cannot think how it would.

Copy link
Member Author

@nfalco79 nfalco79 Jan 2, 2025

Choose a reason for hiding this comment

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

Indeed it does not. Seems buildEnv.groovy is ignored for GitSCMExtension

Copy link
Member Author

Choose a reason for hiding this comment

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

private final String projectKey;
private final String serverURL;

public BitbucketEnvVarExtension(@Nullable String owner, @NonNull String repository, @Nullable String projectKey, @NonNull String serverURL) {
Copy link
Member

Choose a reason for hiding this comment

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

You are missing getters, and @DataBoundConstructor, and a Descriptor. Does this actually work? It does not look like it would.

Copy link
Member Author

Choose a reason for hiding this comment

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

It works, also other GitSCMExtension in this plugin works without @DataBoundConstructor
Anyway In master I have added this annotation and descriptor.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it works because XStream is able to build the object using reflection without use constructor (unsafe class) and inject fields

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

As example the GitClientAuthenticatorExtension won't work with @DataBoundConstructor. It fails with
Could not instantiate arguments for com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl. Secrets are involved, so details are available on more verbose logging levels.

@nfalco79
Copy link
Member Author

nfalco79 commented Jan 3, 2025

Fixes #809 #447

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants