Skip to content

Instantly share code, notes, and snippets.

@ivan-pinatti
Last active January 18, 2019 22:28
Show Gist options
  • Save ivan-pinatti/87b19e40f9b22fa3b9fa04194bdf176c to your computer and use it in GitHub Desktop.
Save ivan-pinatti/87b19e40f9b22fa3b9fa04194bdf176c to your computer and use it in GitHub Desktop.
Jenkins - Set Bitbucket Oauth plugin parameters via groovy script - #jenkins #groovy #bitbucket #oauth
#!groovy
// imports
import hudson.security.AuthorizationStrategy
import hudson.security.SecurityRealm
import jenkins.model.Jenkins
import org.jenkinsci.plugins.BitbucketSecurityRealm
// parameters
def bitbucketSecurityRealmParameters = [
clientID: '012345678901234567',
clientSecret: '012345678901234567012345678901'
]
// https://github.com/jenkinsci/bitbucket-oauth-plugin/blob/bitbucket-oauth-0.5/src/main/java/org/jenkinsci/plugins/BitbucketSecurityRealm.java
SecurityRealm bitbucketSecurityRealm = new BitbucketSecurityRealm(
bitbucketSecurityRealmParameters.clientID,
bitbucketSecurityRealmParameters.clientSecret
)
// authorization strategy - full control when logged in
AuthorizationStrategy authorizationStrategy = new hudson.security.FullControlOnceLoggedInAuthorizationStrategy()
// authorization strategy - set anonymous read to false
authorizationStrategy.setAllowAnonymousRead(false)
// get Jenkins instance
Jenkins jenkins = Jenkins.getInstance()
// add configuration to Jenkins
jenkins.setSecurityRealm(bitbucketSecurityRealm)
jenkins.setAuthorizationStrategy(authorizationStrategy)
// save current Jenkins state to disk
jenkins.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment