Browse Source

Add groovy script for docker hub credentials creation

master
Apostolos Fanakis 5 years ago
parent
commit
b2dee514df
  1. 42
      jenkins/configuration/dockerCredentials.groovy
  2. 0
      jenkins/configuration/gitlabSSHCredentials.groovy
  3. 6
      jenkins/configuration/security.groovy

42
jenkins/configuration/dockerCredentials.groovy

@ -0,0 +1,42 @@
// This script is based on the source bellow:
// https://gist.github.com/ivan-pinatti/830ec918781060df03b12efd4a14096e
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.common.*
import com.cloudbees.plugins.credentials.domains.Domain
import com.cloudbees.plugins.credentials.impl.*
import jenkins.model.Jenkins
import org.jenkinsci.plugins.plaincredentials.impl.*
def dockerhubUser = new File("/run/secrets/jenkins_dockerhub_username").text.trim()
def dockerhubPassword = new File("/run/secrets/jenkins_dockerhub_password").text.trim()
def jenkinsKeyUsernameWithPasswordParameters = [
description: 'Apella docker-hub',
id : 'docker-hub-apella',
password : dockerhubPassword,
userName : dockerhubUser
]
// Gets Jenkins instance
Jenkins jenkins = Jenkins.getInstance()
// Gets credentials domain
def globalDomain = Domain.global()
// Gets credentials store
def credentialsStore = jenkins.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()
// Defines username and password credentials
def jenkinsKeyUsernameWithPassword = new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL,
jenkinsKeyUsernameWithPasswordParameters.id,
jenkinsKeyUsernameWithPasswordParameters.description,
jenkinsKeyUsernameWithPasswordParameters.userName,
jenkinsKeyUsernameWithPasswordParameters.secret
)
// Adds credential to store
credentialsStore.addCredentials(globalDomain, jenkinsKeyUsernameWithPassword)
// Saves to disk
jenkins.save()

0
jenkins/configuration/credentials.groovy → jenkins/configuration/gitlabSSHCredentials.groovy

6
jenkins/configuration/security.groovy

@ -14,12 +14,12 @@ import jenkins.security.s2m.AdminWhitelistRule
def instance = Jenkins.getInstance() def instance = Jenkins.getInstance()
// Automate Admin Setup & Plugin Installs // Automate Admin Setup & Plugin Installs
def user = new File("/run/secrets/jenkins_admin_username").text.trim() def adminUser = new File("/run/secrets/jenkins_admin_username").text.trim()
def pass = new File("/run/secrets/jenkins_admin_password").text.trim() def adminPassword = new File("/run/secrets/jenkins_admin_password").text.trim()
// Create Admin User // Create Admin User
def hudsonRealm = new HudsonPrivateSecurityRealm(false) def hudsonRealm = new HudsonPrivateSecurityRealm(false)
hudsonRealm.createAccount(user, pass) hudsonRealm.createAccount(adminUser, adminPassword)
instance.setSecurityRealm(hudsonRealm) instance.setSecurityRealm(hudsonRealm)
// Set Auth to Full Control Once Logged In // Set Auth to Full Control Once Logged In

Loading…
Cancel
Save