Popular Posts

Custom Jenkins Docker Image

How to run
Dockerfile to build customized Jenkins docker image with pre-installed theme and required plugins.
Dockerfile
from jenkins/jenkins:latest
#FROM openjdk

# skip Setup Wizard during first startup
ENV JAVA_OPTS="-Djenkins.install.runSetupWizard=false"

ENV JENKINS_USER=admin
ENV JENKINS_PASS=password

# COPY custom scripts into jenkin initialization directory
COPY jenkins-security.groovy /usr/share/jenkins/ref/init.groovy.d/ #(1)
COPY jenkins-theme.groovy /usr/share/jenkins/ref/init.groovy.d/    #(2)

#Copy jenkins-material-theme.css into /var/jenkins_home/userContent directory
COPY jenkins-material-theme.css /var/jenkins_home/userContent/    #(3)
RUN /usr/local/bin/install-plugins.sh simple-theme-plugin


# Distributed Builds plugins
RUN /usr/local/bin/install-plugins.sh ssh-slaves

# install Notifications and Publishing plugins
RUN /usr/local/bin/install-plugins.sh email-ext
RUN /usr/local/bin/install-plugins.sh mailer
RUN /usr/local/bin/install-plugins.sh slack

# Artifacts
RUN /usr/local/bin/install-plugins.sh htmlpublisher

# UI
RUN /usr/local/bin/install-plugins.sh greenballs
RUN /usr/local/bin/install-plugins.sh simple-theme-plugin

#Blue Ocean Pipeline
RUN /usr/local/bin/install-plugins.sh blueocean
RUN /usr/local/bin/install-plugins.sh blueocean-dashboard
RUN /usr/local/bin/install-plugins.sh blueocean-git-pipeline
RUN /usr/local/bin/install-plugins.sh blueocean-github-pipeline

# Scaling
RUN /usr/local/bin/install-plugins.sh kubernetes


# https://www.hugeinc.com/articles/list-of-useful-jenkins-plugins
RUN /usr/local/bin/install-plugins.sh build-monitor-plugin
RUN /usr/local/bin/install-plugins.sh build-name-setter
RUN /usr/local/bin/install-plugins.sh build-pipeline-plugin
RUN /usr/local/bin/install-plugins.sh build-timeout


# testing
RUN /usr/local/bin/install-plugins.sh cobertura
RUN /usr/local/bin/install-plugins.sh jacoco
RUN /usr/local/bin/install-plugins.sh performance


# install Maven
USER root
#RUN apt-get update && apt-get install -y maven


USER jenkins
  1. jenkins-security.groovy file contains steps to create credentials and other initialization process.

  2. jenkins-theme.groovy file apply custom theme to Jenkins UI

  3. jenkins-material-theme.css custom CSS you can download from http://afonsof.com/jenkins-material-theme/

jenkins-security.groovy
#!groovy
import jenkins.model.*
import hudson.security.*
import jenkins.security.s2m.AdminWhitelistRule
import jenkins.install.InstallState
import com.cloudbees.plugins.credentials.impl.*
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.domains.*
import org.jenkinsci.plugins.plaincredentials.impl.*
import hudson.util.*

def env = System.getenv()
def instance = Jenkins.getInstance()

// able to read without login
def hudsonRealm = new HudsonPrivateSecurityRealm(false)
hudsonRealm.createAccount(env.JENKINS_USER, env.JENKINS_PASS)
instance.setSecurityRealm(hudsonRealm)
// use FullControlOnceLoggedInAuthorizationStrategy for jenkin version 3 and above
def strategy = new FullControlOnceLoggedInAuthorizationStrategy()
// read on mode enabled?
strategy.setAllowAnonymousRead(false)
instance.setAuthorizationStrategy(strategy)
instance.save()

Jenkins.instance.getInjector().getInstance(AdminWhitelistRule.class).setMasterKillSwitch(false)
instance.setInstallState(InstallState.INITIAL_SETUP_COMPLETED)


// SMTP Configuration for Jenkins

def jenkinsLocationConfiguration = JenkinsLocationConfiguration.get()
jenkinsLocationConfiguration.setAdminAddress("Jenkins<xxxxxx@gmail.com>")
jenkinsLocationConfiguration.setUrl("https://codergists.blogspot.com")
jenkinsLocationConfiguration.save()

def desc = instance.getDescriptor("hudson.tasks.Mailer")
desc.setSmtpAuth("xxxxxx@gmail.com", "xxxxxx")
desc.setReplyToAddress("noreply@gmail.com")
desc.setSmtpHost("smtp.gmail.com")
desc.setUseSsl(true)
desc.setSmtpPort("587")
desc.setCharset("UTF-8")
desc.save()


// Jenkins Global Security Credentials
// Generate GIT access key xxxxxxxxxxxxxxxa41e1ab0502b2xxxxxxxxxxxxxxx

Credentials c = (Credentials) new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL,"GitUsernamePwd", "GitUsernamePwd", "yourgitusername", "xxxxxxxxxxxxxxxa41e1ab0502b2xxxxxxxxxxxxxxx")
SystemCredentialsProvider.getInstance().getStore().addCredentials(Domain.global(), c)

Credentials c1 = (Credentials) new StringCredentialsImpl(CredentialsScope.GLOBAL,"GitSecretText","SecretText",Secret.fromString("xxxxxxxxxxxxxxxa41e1ab0502b2xxxxxxxxxxxxxxx"))
SystemCredentialsProvider.getInstance().getStore().addCredentials(Domain.global(), c1)
jenkins-theme.groovy
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.simpletheme.CssUrlThemeElement;

Jenkins jenkins = Jenkins.get()

def themeDecorator = jenkins.getExtensionList(org.codefirst.SimpleThemeDecorator.class).first()

// apply custom theme to jenkins UI
// content under userContent can be accissible through http://jenkins-host/userContent/jenkins-material-theme.css
themeDecorator.setElements([
  new CssUrlThemeElement('/userContent/jenkins-material-theme.css')
])
jenkins.save()

How to run

Create empty directory and add all three files into that directory and perform below commands.

run.sh
# create docker image and tag with name tvajjala/jenkins

$/> docker build . -t tvajjala/jenkins

#Run jenkins
$/> docker run -d -p 8080:8080 --rm tvajjala/jenkins

No comments:

Post a Comment