Skip to content

Instantly share code, notes, and snippets.

@jchauncey
Created December 14, 2014 20:40
Show Gist options
  • Save jchauncey/c9a933287fb1e9f4263b to your computer and use it in GitHub Desktop.
Save jchauncey/c9a933287fb1e9f4263b to your computer and use it in GitHub Desktop.
Jenkins master in Docker
import jenkins.model.*
import hudson.model.*
import hudson.security.*
import org.jenkinsci.plugins.*
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.domains.*
import com.cloudbees.plugins.credentials.common.*
import com.cloudbees.jenkins.plugins.sshcredentials.impl.*
def env = System.getenv()
github_private_key = env['GITHUB_PRIVATE_KEY']
if(github_private_key) {
global_domain = Domain.global()
credentials_store =
Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()
credentials = new BasicSSHUserPrivateKey(
CredentialsScope.GLOBAL,
null,
"github",
new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource(github_private_key),
null,
"Private key for accessing github"
)
username_matcher = CredentialsMatchers.withUsername("github")
available_credentials =
CredentialsProvider.lookupCredentials(
StandardUsernameCredentials.class,
Jenkins.getInstance(),
hudson.security.ACL.SYSTEM,
new SchemeRequirement("ssh")
)
existing_credentials =
CredentialsMatchers.firstOrNull(
available_credentials,
username_matcher
)
if(existing_credentials != null) {
credentials_store.updateCredentials(
global_domain,
existing_credentials,
credentials
)
} else {
credentials_store.addCredentials(global_domain, credentials)
}
}
import jenkins.model.Jenkins
import hudson.model.FreeStyleProject
import hudson.plugins.git.GitSCM
import hudson.plugins.git.UserRemoteConfig
import hudson.plugins.git.BranchSpec
import hudson.plugins.git.SubmoduleConfig
import hudson.plugins.git.extensions.impl.PathRestriction
import hudson.model.Cause
import hudson.model.Cause.UserIdCause
import javaposse.jobdsl.plugin.ExecuteDslScripts
import javaposse.jobdsl.plugin.RemovedJobAction
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.domains.*
import com.cloudbees.plugins.credentials.common.*
import com.cloudbees.jenkins.plugins.sshcredentials.impl.*
def env = System.getenv()
seed_jobs_repo = env['SEED_JOBS_REPO']
if(seed_jobs_repo) {
if(!Jenkins.instance.getItemMap().containsKey("seed-job")) {
def seedJob = Jenkins.instance.createProject(FreeStyleProject.class, "seed-job")
username_matcher = CredentialsMatchers.withUsername("github")
available_credentials =
CredentialsProvider.lookupCredentials(
StandardUsernameCredentials.class,
Jenkins.getInstance(),
hudson.security.ACL.SYSTEM,
new SchemeRequirement("ssh")
)
existing_credentials =
CredentialsMatchers.firstOrNull(
available_credentials,
username_matcher
)
def userRemoteConfig = new UserRemoteConfig(seed_jobs_repo, null, null, existing_credentials.id)
def scm = new GitSCM(
Collections.singletonList(userRemoteConfig),
Collections.singletonList(new BranchSpec("master")),
false,
Collections.<SubmoduleConfig>emptyList(),
null,
null,
null)
scm.getExtensions().add(new PathRestriction("jobs/**/*.*",""))
seedJob.scm = scm
def scriptLocation = new ExecuteDslScripts.ScriptLocation("false", "jobs/**/*.groovy", null)
seedJob.buildersList.add(new ExecuteDslScripts(scriptLocation, false, RemovedJobAction.DELETE))
seedJob.save()
seedJob.scheduleBuild(new Cause.UserIdCause())
}
}
FROM quay.io/rallysoftware/java:jre8
MAINTAINER "Jonathan Chauncey <[email protected]>"
RUN yum update all && yum install -y --quiet git tar gcc openssl-devel && yum clean all
ENV JENKINS_HOME /opt/jenkins
RUN mkdir -p /opt/jenkins/plugins
ADD http://mirrors.jenkins-ci.org/war/latest/jenkins.war /opt/jenkins.war
ADD https://updates.jenkins-ci.org/latest/git.hpi /opt/jenkins/plugins/git.hpi
ADD https://updates.jenkins-ci.org/latest/git-client.hpi /opt/jenkins/plugins/git-client.hpi
ADD https://updates.jenkins-ci.org/latest/scm-api.hpi /opt/jenkins/plugins/scm-api.hpi
ADD https://www.dropbox.com/s/m5j02jcf1k209ge/job-dsl.hpi?dl=1 /opt/jenkins/plugins/job-dsl.hpi
ADD https://updates.jenkins-ci.org/latest/ssh-slaves.hpi /opt/jenkins/plugins/ssh-slaves.hpi
ADD https://updates.jenkins-ci.org/latest/ssh-credentials.hpi /opt/jenkins/plugins/ssh-credentials.hpi
ADD https://updates.jenkins-ci.org/latest/credentials.hpi /opt/jenkins/plugins/credentials.hpi
ADD https://updates.jenkins-ci.org/latest/durable-task.hpi /opt/jenkins/plugins/durable-task.hpi
ADD https://updates.jenkins-ci.org/latest/docker-plugin.hpi /opt/jenkins/plugins/docker-plugin.hpi
ADD https://updates.jenkins-ci.org/latest/rbenv.hpi /opt/jenkins/plugins/rbenv.hpi
ADD https://updates.jenkins-ci.org/latest/ruby-runtime.hpi /opt/jenkins/plugins/ruby-runtime.hpi
ADD init.groovy.d /opt/jenkins/init.groovy.d
ADD start.sh /opt/start.sh
RUN chmod +x /opt/start.sh
CMD ["/opt/start.sh"]
EXPOSE 8080
#!/bin/bash
java ${JAVA_OPTS} -jar /opt/jenkins.war
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment