Skip to content

Commit

Permalink
Add Server Credentials to User Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gmitch215 committed Jul 26, 2024
1 parent 263c6dc commit 99e7321
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
9 changes: 5 additions & 4 deletions src/main/kotlin/io/codemc/api/jenkins/jenkins.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import io.codemc.api.JOB_MAVEN
import io.codemc.api.RESOURCE_CACHE
import io.codemc.api.USER_CONFIG
import org.jetbrains.annotations.VisibleForTesting
import java.net.URI
import java.net.URL

var jenkinsConfig: JenkinsConfig = JenkinsConfig("", "", "")
set(value) {
Expand All @@ -35,8 +33,11 @@ data class JenkinsConfig(
fun ping(): Boolean =
client.api().systemApi().systemInfo().jenkinsVersion() != null

fun createJenkinsUser(username: String): Boolean {
val config = RESOURCE_CACHE[USER_CONFIG]?.replace("{USERNAME}", username) ?: return false
fun createJenkinsUser(username: String, password: String): Boolean {
val config0 = RESOURCE_CACHE[USER_CONFIG] ?: return false
val config = config0
.replace("{USERNAME}", username)
.replace("{PASSWORD}", password)
val status = client.api().jobsApi().create("/", username, config)

return status.value()
Expand Down
12 changes: 11 additions & 1 deletion src/main/resources/templates/jenkins/job-freestyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,15 @@
<followSymlinks>false</followSymlinks>
</hudson.tasks.ArtifactArchiver>
</publishers>
<buildWrappers/>
<buildWrappers>
<org.jenkinsci.plugins.credentialsbinding.impl.SecretBuildWrapper plugin="[email protected]_254d">
<bindings>
<org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding>
<credentialsId>nexus-repository</credentialsId>
<usernameVariable>JENKINS_USERNAME</usernameVariable>
<passwordVariable>JENKINS_PASSWORD</passwordVariable>
</org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding>
</bindings>
</org.jenkinsci.plugins.credentialsbinding.impl.SecretBuildWrapper>
</buildWrappers>
</project>
12 changes: 11 additions & 1 deletion src/main/resources/templates/jenkins/job-maven.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,17 @@
<followSymlinks>false</followSymlinks>
</hudson.tasks.ArtifactArchiver>
</publishers>
<buildWrappers/>
<buildWrappers>
<org.jenkinsci.plugins.credentialsbinding.impl.SecretBuildWrapper plugin="[email protected]_254d">
<bindings>
<org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding>
<credentialsId>nexus-repository</credentialsId>
<usernameVariable>JENKINS_USERNAME</usernameVariable>
<passwordVariable>JENKINS_PASSWORD</passwordVariable>
</org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding>
</bindings>
</org.jenkinsci.plugins.credentialsbinding.impl.SecretBuildWrapper>
</buildWrappers>
<prebuilders/>
<postbuilders/>
<runPostStepsIfResult>
Expand Down
26 changes: 21 additions & 5 deletions src/main/resources/templates/jenkins/user-config.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version='1.1' encoding='UTF-8'?>
<com.cloudbees.hudson.plugins.folder.Folder plugin="cloudbees-folder@6.858.v898218f3609d">
<com.cloudbees.hudson.plugins.folder.Folder plugin="cloudbees-folder@6.928.v7c780211d66e">
<actions/>
<description/>
<description></description>
<properties>
<org.jenkinsci.plugins.configfiles.folder.FolderConfigFileProperty plugin="config-file-provider@968.ve1ca_eb_913f8c">
<org.jenkinsci.plugins.configfiles.folder.FolderConfigFileProperty plugin="config-file-provider@973.vb_a_80ecb_9a_4d0">
<configs class="sorted-set">
<comparator class="org.jenkinsci.plugins.configfiles.ConfigByIdComparator"/>
</configs>
Expand Down Expand Up @@ -43,13 +43,29 @@
</com.cloudbees.plugins.credentials.domains.Domain>
<java.util.concurrent.CopyOnWriteArrayList/>
</entry>
<entry>
<com.cloudbees.plugins.credentials.domains.Domain plugin="[email protected]_d7b_c7b_c9f">
<name>Server Credentials</name>
<description>Your credentials for your Nexus Repository</description>
<specifications/>
</com.cloudbees.plugins.credentials.domains.Domain>
<list>
<com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl plugin="[email protected]_d7b_c7b_c9f">
<id>nexus-repository</id>
<description>Your Nexus Login Details</description>
<username>{USERNAME}</username>
<password>{PASSWORD}</password>
<usernameSecret>true</usernameSecret>
</com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>
</list>
</entry>
</domainCredentialsMap>
</com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider_-FolderCredentialsProperty>
<org.jenkinsci.plugins.docker.workflow.declarative.FolderConfig plugin="docker-workflow@572.v950f58993843">
<org.jenkinsci.plugins.docker.workflow.declarative.FolderConfig plugin="docker-workflow@580.vc0c340686b_54">
<dockerLabel/>
<registry plugin="[email protected]_3cb_0a_6a_fb_29"/>
</org.jenkinsci.plugins.docker.workflow.declarative.FolderConfig>
<org.jenkinsci.plugins.pipeline.maven.MavenConfigFolderOverrideProperty plugin="pipeline-maven@1376.v18876d10ce9c">
<org.jenkinsci.plugins.pipeline.maven.MavenConfigFolderOverrideProperty plugin="pipeline-maven@1421.v610fa_b_e2d60e">
<settings class="jenkins.mvn.DefaultSettingsProvider"/>
<globalSettings class="jenkins.mvn.DefaultGlobalSettingsProvider"/>
<override>false</override>
Expand Down
3 changes: 2 additions & 1 deletion src/test/docker/jenkins/plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ plugins:
- artifactId: configuration-as-code
- artifactId: workflow-aggregator
- artifactId: badge
- artifactId: github-oauth
- artifactId: github-oauth
- artifactId: credentials
6 changes: 3 additions & 3 deletions src/test/kotlin/io/codemc/api/jenkins/TestJenkins.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ object TestJenkins {

@Test
fun testCreateJenkinsUser() {
assert(createJenkinsUser("test"))
assert(createJenkinsUser("test", "test_password"))
assert(getJenkinsUser("test").isNotEmpty())

assert(createJenkinsUser("MyPlayer123"))
assert(createJenkinsUser("MyPlayer123", "MyPassword456"))
assert(getJenkinsUser("MyPlayer123").isNotEmpty())
}

Expand All @@ -34,7 +34,7 @@ object TestJenkins {
val name = "TestUsername"
val url = "https://github.com/TestUsername/TestRepo"

assert(createJenkinsUser(name))
assert(createJenkinsUser(name, "TestPassword"))
assert(getJenkinsUser(name).isNotEmpty())

assert(createJenkinsJob(name, "TestJob_Freestyle", url, true))
Expand Down

0 comments on commit 99e7321

Please sign in to comment.