Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ivan-pinatti/70f400fef4dc23651cd4c5138079785b to your computer and use it in GitHub Desktop.
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
#!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