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

Possibility to use scm variable in multibranch pipeline #28

Open
testuser7 opened this issue Nov 15, 2016 · 12 comments
Open

Possibility to use scm variable in multibranch pipeline #28

testuser7 opened this issue Nov 15, 2016 · 12 comments

Comments

@testuser7
Copy link

It will be nice if we can use scm variable in multibranch pipeline jenkinsfile.

@tomasbjerre
Copy link
Contributor

Do you have an example of where it does not work?

@testuser7
Copy link
Author

For now I need to manually specify projectKey and repoSlug:

                if (env.CHANGE_ID) {
                    step([$class: 'ViolationsToBitbucketServerRecorder',
                          config: [useUsernamePasswordCredentials        : true,
                                   projectKey                            : 'prokey',
                                   repoSlug                              : 'repository-name',
                                   pullRequestId                         : env.CHANGE_ID,
                                   commentOnlyChangedContent             : false,
                                   commentOnlyChangedContentContext      : 0,
                                   createCommentWithAllSingleFileComments: false,
                                   createSingleFileComments              : true,
                                   violationConfigs                      : [[pattern : '.*/checkstyle-result\\.xml$',
                                                                             reporter: 'CHECKSTYLE']]]])
                }

Simplified configuration example:

                if (env.CHANGE_ID) {
                    step([$class: 'ViolationsToBitbucketServerRecorder',
                          config: [repositoryConfig                      : scm,
                                   pullRequestId                         : env.CHANGE_ID,
                                   commentOnlyChangedContent             : false,
                                   commentOnlyChangedContentContext      : 0,
                                   createCommentWithAllSingleFileComments: false,
                                   createSingleFileComments              : true,
                                   violationConfigs                      : [[pattern : '.*/checkstyle-result\\.xml$',
                                                                             reporter: 'CHECKSTYLE']]]])
                }

@tomasbjerre
Copy link
Contributor

Are you sure scm has these values? And if it does, cant you do something like this?

projectKey                            : env.repositoryConfig.projectKey,

@testuser7
Copy link
Author

Yes, I can simply use: checkout scm to checkout repository.
Method scm.getKey() return full repository url.

There is no any environment variables containing project key or repository name.

@tomasbjerre
Copy link
Contributor

Perhaps if you use https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket and let it provide these variables for you? Then I think you can do:

projectKey                            : env.PULL_REQUEST_TO_REPO_PROJECT_KEY,

If you parameterize the job with a String parameter like PULL_REQUEST_TO_REPO_PROJECT_KEY

@testuser7
Copy link
Author

Right now I'm using Bitbucket Branch Source Plugin with Post Webhooks for Bitbucket. Direct usage of scm variable will be most reasonable.

@tomasbjerre
Copy link
Contributor

Perhaps you can get the project key from getRepoOwner and repository from getRepository. But I'm just guessing here =)

@testuser7
Copy link
Author

I'm not sure if I can access this methods from pipeline groovy script.

@jansohn
Copy link

jansohn commented Nov 28, 2017

I'd also appreciate this feature.

@jetersen
Copy link
Member

jetersen commented Jan 24, 2018

Definitely possible to get the ID and even more: https://github.com/jenkinsci/office-365-connector-plugin/blob/53f1823060c4a7aba72294a08c0efba6751664d5/src/main/java/jenkins/plugins/office365connector/ActionableBuilder.java#L59-L89

Here you get the full URL so ya to the PR even
String urlString = oma.getObjectUrl();

@amandel
Copy link
Member

amandel commented Apr 30, 2019

For me the following pipeline snipped could collect all needed data:

@NonCPS
void publishBuildResults() {
  try {
    def gitUrl = scmVars.GIT_URL_1
    // https://myserver.example.com/bitbucket/scm/SETOOLS/setools-sbs-bitbucket.git
    def urlMatcher = gitUrl =~ '^(https?://[^/]*/bitbucket)/scm/([^/]*)/([^/]*).git$'
    if (urlMatcher) {
      def bitbucketServer = urlMatcher[0][1]
      def projectKey = urlMatcher[0][2]
      def repoSlug = urlMatcher[0][3]
      def changeId = pipeline.env.CHANGE_ID;
      def credentialIdTemp = pipeline.scm.getUserRemoteConfigs()[0].credentialsId;
      pipeline.ViolationsToBitbucketServer([
          bitbucketServerUrl                    : bitbucketServer,
          commentOnlyChangedContent             : true,
          commentOnlyChangedContentContext      : 5,
          createCommentWithAllSingleFileComments: false,
          createSingleFileComments              : true,
          maxNumberOfViolations                 : 99999,
          keepOldComments                       : true,
          projectKey                            : projectKey,
          pullRequestId                         : changeId,
          repoSlug                              : repoSlug,
          credentialsId                         : credentialIdTemp,
          commentTemplate                       : """
**{{violation.severity}}**: {{violation.message}}
*Reporter: {{violation.reporter}}{{#violation.rule}} //  Rule: {{violation.rule}}{{/violation.rule}}*
""",
          violationConfigs            : [
              // Many more formats available, check https://github.com/tomasbjerre/violations-lib
              [parser: 'SONAR', pattern: '.*/target/sonar/sonar-report.json', reporter: 'SONAR']
          ]
      ])
    } else {
      pipeline.echo("Does not look like a bitbucket server - think about migrating?")
    }
  } catch (error) {
    // do not leak errors but report them.
    pipeline.echo("Error during result publish: " + error)
  }
}

@robross0606
Copy link

From what I can tell, this is really more the fault of Bitbucket Branch Source Plugin because it doesn't expose the Bitbucket JSON payload data (project, slug, etc.) from the webhook trigger into the pipeline.

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

No branches or pull requests

6 participants