1
votes

I'm trying to specify the GithubProjectProperty in a Jenkins Multibranch pipeline. I've been unsuccessful attempting to set an entry in the option block to control this value.

The pipeline syntax snippet generator suggests:

properties([
    $class: 'GithubProjectProperty',
    displayName: '',
    projectUrlStr: 'https://myServer/myOrg/myRepo'
])

None of the following appear to work:

1) Try to put the properties directly in the options block

options {
        // Set the URL for the GitHub project option
        properties([
            $class: 'GithubProjectProperty',
            displayName: '',
            projectUrlStr: 'https://myServer/myOrg/myRepo'
        ])
}

ERROR: The ‘properties’ section has been renamed as of version 0.8. Use ‘options’ instead

2) Remove the properties keyword but leave the option in the options block

options {
        // Set the URL for the GitHub project option
        [
            $class: 'GithubProjectProperty',
            displayName: '',
            projectUrlStr: 'https://myServer/myOrg/myRepo'
        ]
}

ERROR: Options cannot be defined as maps

3) Treat the GitHubProjectProperty as if it can be instantiated (like office365ConnectorWebhooks)

options {
        // Set the URL for the GitHub project option
        GithubProjectProperty('https://myServer/myOrg/myRepo')
}

ERROR: Invalid option type "GithubProjectProperty". Valid option types: [authorizationMatrix, buildDiscarder, catchError, checkoutToSubdirectory, disableConcurrentBuilds, disableResume, durabilityHint, newContainerPerStage, office365ConnectorWebhooks, overrideIndexTriggers, parallelsAlwaysFailFast, preserveStashes, quietPeriod, rateLimitBuilds, retry, script, skipDefaultCheckout, skipStagesAfterUnstable, timeout, waitUntil, warnError, withContext, withCredentials, withEnv, ws]

4) Treat the GitHubProjectProperty as if it can be instantiated but inside a script block (because script is supposed to be valid according to attempt #3)

options {
    script {
        // Set the URL for the GitHub project option
        GithubProjectProperty('https://myServer/myOrg/myRepo')
    }
}

ERROR: Options definitions cannot have blocks

The office-365-connector-plugin is a working plugin that is supported in the options block of a Jenkinsfile. I compared its code with the github-plugin source on GitHub and noticed the following line:

@Extension
public static final class DescriptorImpl extends JobPropertyDescriptor {

The code is lacking a @Symbol directive that the office365ConnectorWebhooks appears to provide in its code:

@Extension
@Symbol("office365ConnectorWebhooks")
public final class WebhookJobPropertyDescriptor extends JobPropertyDescriptor {

Is there some special syntax to use to add the GitHub URL to a multibranch pipeline or does that plugin just not support managing it through a Jenkinsfile?

1

1 Answers

1
votes

The ability to specify options in a pipeline using a Jenkinsfile requires a Symbol. There is a proposed fix in the Jenkins github-plugin that adds the necessary Symbol directive but it is not currently part of the plugin as of version 1.30.0.

See: https://issues.jenkins-ci.org/browse/JENKINS-62339

It's possible for a developer to build their own updated plugin in the meantime by updating the following file: src/main/java/com/coravy/hudson/plugins/github/GithubProjectProperty.java in the source found at: https://github.com/jenkinsci/github-plugin

Add the Symbol:

    import org.jenkinsci.Symbol;
...
    @Extension
    @Symbol("githubProjectProperty")
    public static final class DescriptorImpl extends JobPropertyDescriptor {
...

And for good measure, make sure the code specifies the correct function signature for newInstance:

   @Override
   public JobProperty<?> newInstance(@Nonnull StaplerRequest req, JSONObject formData)
           throws Descriptor.FormException {

The updated plugin may be installed by a Jenkins admin using the Advanced option in the Plugin Manager to upload an .hpi file from outside the central plugin repository