Created
May 21, 2018 14:50
-
-
Save ivan-pinatti/70f400fef4dc23651cd4c5138079785b to your computer and use it in GitHub Desktop.
Jenkins - Set Bitbucket Cloud Endpoint plugin parameters via groovy script to manage hooks - #jenkins #groovy #bitbucket #bitbucketcloud #bitbucketendpoint #hooks #webhooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!groovy | |
// imports | |
import com.cloudbees.plugins.credentials.* | |
import com.cloudbees.plugins.credentials.domains.Domain | |
import com.cloudbees.plugins.credentials.impl.* | |
import hudson.util.Secret | |
import jenkins.model.Jenkins | |
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.* | |
// parameters | |
def bitbucketEndpointKeyParameters = [ | |
description: 'Bitbucket Cloud Endpoint Key', | |
id: 'bitbucket-cloud-endpoint-key', | |
secret: '12345678901234567890', | |
userName: 'my-bitbucket-username-here' | |
] | |
def bitbucketEndpointParameters = [ | |
manageHooks: true, | |
credentialsId: bitbucketEndpointKeyParameters.id | |
] | |
// get Jenkins instance | |
Jenkins jenkins = Jenkins.getInstance() | |
// get credentials domain | |
def domain = Domain.global() | |
// get credentials store | |
def store = jenkins.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore() | |
// define Bitbucket secret | |
def bitbucketCloudKey = new UsernamePasswordCredentialsImpl( | |
CredentialsScope.GLOBAL, | |
bitbucketEndpointKeyParameters.id, | |
bitbucketEndpointKeyParameters.description, | |
bitbucketEndpointKeyParameters.userName, | |
bitbucketEndpointKeyParameters.secret | |
) | |
// add credential to store | |
store.addCredentials(domain, bitbucketCloudKey) | |
// get Bitbucket endpoint configuration | |
def bitbucketEndpoint = jenkins.getDescriptor("com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration") | |
// define Bitbucket cloud endpoint | |
def bitbucketCloudEndpoint = new BitbucketCloudEndpoint( | |
bitbucketEndpointParameters.manageHooks, | |
bitbucketEndpointParameters.credentialsId | |
) | |
// update endpoint | |
bitbucketEndpoint.updateEndpoint(bitbucketCloudEndpoint) | |
// save to disk | |
jenkins.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment