From b2dee514dfcdc7e8ad8093f44ced9f100c2c83ee Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sat, 30 May 2020 23:15:53 +0300 Subject: [PATCH] Add groovy script for docker hub credentials creation --- .../configuration/dockerCredentials.groovy | 42 +++++++++++++++++++ ...als.groovy => gitlabSSHCredentials.groovy} | 0 jenkins/configuration/security.groovy | 6 +-- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 jenkins/configuration/dockerCredentials.groovy rename jenkins/configuration/{credentials.groovy => gitlabSSHCredentials.groovy} (100%) diff --git a/jenkins/configuration/dockerCredentials.groovy b/jenkins/configuration/dockerCredentials.groovy new file mode 100644 index 0000000..268d370 --- /dev/null +++ b/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() \ No newline at end of file diff --git a/jenkins/configuration/credentials.groovy b/jenkins/configuration/gitlabSSHCredentials.groovy similarity index 100% rename from jenkins/configuration/credentials.groovy rename to jenkins/configuration/gitlabSSHCredentials.groovy diff --git a/jenkins/configuration/security.groovy b/jenkins/configuration/security.groovy index cf9f53a..ca981a5 100644 --- a/jenkins/configuration/security.groovy +++ b/jenkins/configuration/security.groovy @@ -14,12 +14,12 @@ import jenkins.security.s2m.AdminWhitelistRule def instance = Jenkins.getInstance() // Automate Admin Setup & Plugin Installs -def user = new File("/run/secrets/jenkins_admin_username").text.trim() -def pass = new File("/run/secrets/jenkins_admin_password").text.trim() +def adminUser = new File("/run/secrets/jenkins_admin_username").text.trim() +def adminPassword = new File("/run/secrets/jenkins_admin_password").text.trim() // Create Admin User def hudsonRealm = new HudsonPrivateSecurityRealm(false) -hudsonRealm.createAccount(user, pass) +hudsonRealm.createAccount(adminUser, adminPassword) instance.setSecurityRealm(hudsonRealm) // Set Auth to Full Control Once Logged In