Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hobo GitOps! #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ Vagrant.configure("2") do |config|
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get update
apt-get install -y docker-ce dos2unix

# Temporarily tied to a specific version of Docker due to https://github.com/docker/for-linux/issues/591
apt-get install -y docker-ce=18.06.1~ce~3-0~ubuntu dos2unix

curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
Expand Down
16 changes: 14 additions & 2 deletions images/jenkins-base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,20 @@ ENV JENKINS_SHA d8ed5a7033be57aa9a84a5342b355ef9f2ba6cdb490db042a6d03efb23ca1e83
ENV JENKINS_UC https://updates.jenkins.io
ENV JENKINS_URL https://repo.jenkins-ci.org/public/org/jenkins-ci/main/jenkins-war/${JENKINS_VERSION}/jenkins-war-${JENKINS_VERSION}.war

# Optional extra hook for Job DSL examples, used in one of the init scripts if present. No env var == no job DSL set up
ENV DSL_REPO https://github.com/sheehan/job-dsl-gradle-example.git
# Optional extra hook for Job DSL examples, used in one of the init scripts if present. No DSL repo env var == no job DSL set up
ENV GITOPS_DSL_REPO https://github.com/Cervator/GitOpsUtilityJobs.git

# The manifest repo is meant to contain additional configuration
ENV GITOPS_MANIFEST_REPO https://github.com/Cervator/GitOpsManifest.git

# This context allows a set of tiered Jenkins Masters to pull config out from a specific tier
ENV GITOPS_JENKINS_CONTEXT dev

# Default examples work without credentials but will read this one if set (creating it in Jenkins is a different topic)
# ENV GIT_CREDENTIAL SomeCredential

# Optional extra - set a different branch to use by default (which is master)
# ENV GITOPS_PLATFORM_LEVEL develop

# Jenkins is run with user `jenkins`, uid = 1000
# If you bind mount a volume from the host or a data container,
Expand Down
24 changes: 12 additions & 12 deletions images/jenkins-plugins/files/dslScripts/GentleRestartJenkins.groovy
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
job('JenkinsGentleRestart') {
job('BundledJenkinsGentleRestart') {
description('Gently swaddles Jenkins into goodnight mode, sings it a lullaby while waiting for any jobs to finish, then restarts.')
label("master")
steps {
systemGroovyCommand ('''
/*
Copyright (c) 2015-2018 Sam Gleske - https://github.com/samrocketman/jenkins-script-console-scripts

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
Expand All @@ -27,32 +27,32 @@ job('JenkinsGentleRestart') {
This script starts a background thread which will wait for Jenkins to finish
executing jobs before restarting. The thread will abort if shutdown mode is
disabled before jobs finish.

Tested on Jenkins ver. 2.7.1
*/

import hudson.model.RestartListener
import java.util.logging.Level
import java.util.logging.Logger
import jenkins.model.*

//user configurable variable
if(!binding.hasVariable('timeout_seconds')) {
timeout_seconds = 10
}

if(timeout_seconds in String) {
timeout_seconds = Integer.decode(timeout_seconds)
}

//type check user defined parameters/bindings
if(!(timeout_seconds in Integer)) {
throw new Exception('PARAMETER ERROR: timeout_seconds must be an integer.')
}

Logger logger = Logger.getLogger('jenkins.instance.restart')
Jenkins.instance.doQuietDown();

//start a background thread
def thread = Thread.start {
logger.log(Level.INFO, "Jenkins safe restart initiated.")
Expand All @@ -70,7 +70,7 @@ job('JenkinsGentleRestart') {
}
}
}

println 'A safe restart has been scheduled. See the Jenkins logs for restart status updates. Logger is jenkins.instance.restart.'
''')
}
Expand Down
11 changes: 5 additions & 6 deletions images/jenkins-plugins/files/init.groovy.d/03_PrepDSL.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@ import javaposse.jobdsl.plugin.JenkinsJobManagement
import jenkins.model.Jenkins

// Make the Job DSL integration optional. Fairly easy to come up with different ways to trigger it. Env vars are simple.
if (! System.getenv("DSL_REPO")) {
if (! System.getenv("GITOPS_DSL_REPO")) {
println "No DSL repo defined, skipping DSL prep"
return
}

// Create the seed job itself, so it can run and create the "real" DSL-managed jobs
// Create the utility seed job, so it can run and create various DSL-managed utility jobs from a target DSL repo
def jobDslScript = new File("/var/jenkins_home/init.groovy.d/SeedJobDSL.groovy.DSL")
def workspace = new File("/tmp/dsl")
def jobManagement = new JenkinsJobManagement(System.out, [:], workspace)
new DslScriptLoader(jobManagement).runScript(jobDslScript.text)

// Schedule the job to run so the jobs are created when Jenkins starts - or skip this for more control
Jenkins.instance.getItemByFullName('seed-job', FreeStyleProject.class).scheduleBuild(new UserIdCause())
Jenkins.instance.getItemByFullName('SeedJobUtilityDSL', FreeStyleProject.class).scheduleBuild(new UserIdCause())


// Secondary job seeder using a local dir
// Secondary seed job used to demonstrate loading a file directly instead of a repository
def jobDslScript2 = new File("/var/jenkins_home/init.groovy.d/SeedJobDSL2.groovy.DSL")
def workspace2 = new File("/tmp/dsl")
def jobManagement2 = new JenkinsJobManagement(System.out, [:], workspace2)
new DslScriptLoader(jobManagement2).runScript(jobDslScript2.text)

// Schedule the job to run so the jobs are created when Jenkins starts.
Jenkins.instance.getItemByFullName('seed-job2', FreeStyleProject.class).scheduleBuild(new UserIdCause())
Jenkins.instance.getItemByFullName('SeedJobInternal', FreeStyleProject.class).scheduleBuild(new UserIdCause())
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
job('seed-job') {
description('Seed Job to create other DSL-based jobs')
job('SeedJobUtilityDSL') {
description('Seed Job to create DSL repo-provided utility jobs')
label("master")
scm {
git("${System.getenv("DSL_REPO")}")
git("${System.getenv("GITOPS_DSL_REPO")}")
}
triggers {
// Every 15 minutes poll to see if there are new changes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
job('seed-job2') {
description('Seed Job to create other DSL-based jobs')
job('SeedJobInternal') {
description('Seed Job to showcase using DSL from scripts stored locally')
label("master")
customWorkspace('/var/jenkins_home/dslScripts')
concurrentBuild(false)
Expand Down
3 changes: 3 additions & 0 deletions images/jenkins-plugins/files/plugins.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ job-dsl
cloudbees-folder
gradle
email-ext
startup-trigger-plugin
workflow-cps-global-lib
matrix-auth