Skip to content

Commit

Permalink
Merge pull request #196 from DontShaveTheYak/develop
Browse files Browse the repository at this point in the history
Release v0.12.0
  • Loading branch information
shadycuz authored Mar 29, 2022
2 parents dc51deb + 8124a74 commit c268577
Show file tree
Hide file tree
Showing 33 changed files with 667 additions and 78 deletions.
37 changes: 0 additions & 37 deletions .devcontainer/Dockerfile

This file was deleted.

6 changes: 4 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@
"5050:80"
],
// Uncomment the next line to run commands after the container is created - for example installing curl.
"postStartCommand": "./gradlew downloadJars",
"postStartCommand": "nohup /usr/local/bin/jenkins.sh & ./gradlew downloadJars",
// "postCreateCommand": "/sbin/tini -- /usr/local/bin/jenkins.sh &",
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
"mounts": [
"source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind"
],
// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
"remoteUser": "jenkins",
"runArgs": ["--init"]
}
52 changes: 44 additions & 8 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,48 @@ on:
push:
tags:
- '*'
paths:
- 'docker/prod/*'

jobs:
prod:
name: Build and Push

develop:
name: Build and Push Develop Images
runs-on: ubuntu-latest
if: contains(github.ref_name, 'SNAPSHOT')
steps:

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Prod image
id: prod_image
uses: docker/build-push-action@v2
with:
push: true
tags: "dsty/jenkins:${{ github.ref_name }}"
file: docker/prod/Dockerfile
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Demo image
id: docker_build
uses: docker/build-push-action@v2
with:
push: true
tags: "dsty/jenkins:demo-${{ github.ref_name }}"
file: docker/dev/Dockerfile
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
"baseImage=${{ steps.prod_image.outputs.digest }}"
release:
name: Build and Push Release Images
runs-on: ubuntu-latest
steps:

Expand All @@ -22,22 +58,22 @@ jobs:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push
- name: Prod image
id: prod_image
uses: docker/build-push-action@v2
with:
push: true
tags: "dsty/jenkins:${{ contains(github.ref_name, 'SNAPSHOT') && github.ref_name || 'latest' }}"
tags: "dsty/jenkins:${{ github.ref_name }},dsty/jenkins:latest"
file: docker/prod/Dockerfile
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Build and push
- name: Demo image
id: docker_build
uses: docker/build-push-action@v2
with:
push: true
tags: dsty/jenkins:demo
tags: "dsty/jenkins:demo,dsty/jenkins:demo-${{ github.ref_name }}"
file: docker/dev/Dockerfile
cache-from: type=gha
cache-to: type=gha,mode=max
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
uses: actions/setup-python@v3

- name: Python Cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('tests/requirements.txt') }}
Expand All @@ -58,7 +58,7 @@ jobs:
uses: actions/setup-python@v3

- name: Python Cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('tests/requirements.txt') }}
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,6 @@ dmypy.json

# profiling data
.prof

# Ignore Jenkins Server output
nohup.out
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ Why use our library?

[![Jenkins](https://cdn.freebiesupply.com/logos/large/2x/jenkins-logo-png-transparent.png)](https://www.jenkins.io/)

## Demo

Do you want to see the features first? We have a docker image that has Jenkins already setup, configured with our library and some sample jobs.

```sh
docker run -d --rm -p 4000:80 dsty/jenkins:demo
```

You can then see the jenkins at [127.0.0.1:4000](http://127.0.0.1:4000). If the seed job hasn't run, you can run it manually.

## Installation

First you need to configure your Jenkins with our Shared Library. In the Jenkins configure page scroll down
Expand Down
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ java {
withSourcesJar()
}

task removeDuplicateFiles(type: Delete) {
delete "${buildDir}/tmp/groovydoc"
}

groovydoc {
use = true
overviewText = resources.text.fromString('Bringing the Zen of Python to Jenkins.')
Expand All @@ -43,6 +47,8 @@ task groovydocJar(type: Jar, dependsOn: groovydoc ) {
from groovydoc.destinationDir
}

groovydoc.finalizedBy removeDuplicateFiles

publishing {
publications {
mavenJava(MavenPublication) {
Expand Down Expand Up @@ -131,7 +137,7 @@ configurations {
dependencies {
// Use the latest Groovy version for building this library
compileOnly('org.codehaus.groovy:groovy-all:3.0.10')
compileOnly('org.jenkins-ci.main:jenkins-core:2.338')
compileOnly('org.jenkins-ci.main:jenkins-core:2.339')
compileOnly('com.cloudbees:groovy-cps:1.32')
}

Expand Down
7 changes: 7 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Docker
This folder contains the various containers used by the Jenkins Standard Library.

* [prod](./prod/) - This container is based on [jenkins/jenkins:latest-jdk11](https://hub.docker.com/r/jenkins/jenkins) and has the Jenkins Standard Library already configured.
* [dev](./dev/) - Based on the [prod](./prod/) image but has additional configuration for local development using vscode dev containers.
* [jfr](./jfr/) - Based on the [prod](./prod/) image but is setup as a [Jenkinsfile-Runner](https://github.com/jenkinsci/jenkinsfile-runner) to run tests.
* [demo](./demo/) - Based on the [prod](./prod/) image and setup with examples of
40 changes: 40 additions & 0 deletions docker/demo/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
ARG baseImage=dsty/jenkins

FROM ${baseImage}

ARG NONROOT_USER=jenkins
ARG DEBIAN_FRONTEND=noninteractive

USER root

RUN apt-get update && \
apt-get install -y \
sudo && \
rm -rf /var/lib/apt/lists/*

RUN echo "#!/bin/sh\n\
sudoIf() { if [ \"\$(id -u)\" -ne 0 ]; then sudo \"\$@\"; else \"\$@\"; fi }\n\
SOCKET_GID=\$(stat -c '%g' /var/run/docker.sock) \n\
if [ \"${SOCKET_GID}\" != '0' ]; then\n\
if [ \"\$(cat /etc/group | grep :\${SOCKET_GID}:)\" = '' ]; then sudoIf groupadd --gid \${SOCKET_GID} docker-host; fi \n\
if [ \"\$(id ${NONROOT_USER} | grep -E \"groups=.*(=|,)\${SOCKET_GID}\(\")\" = '' ]; then sudoIf usermod -aG \${SOCKET_GID} ${NONROOT_USER}; fi\n\
fi\n\
exec \"\$@\"" > /usr/local/share/docker-init.sh \
&& chmod +x /usr/local/share/docker-init.sh \
&& echo $NONROOT_USER ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$NONROOT_USER \
&& chmod 0440 /etc/sudoers.d/$NONROOT_USER

USER jenkins
WORKDIR $JENKINS_HOME

## Plugins
RUN jenkins-plugin-cli --plugins job-dsl simple-theme-plugin ansicolor

## Setup init scripts
COPY docker/demo/init_scripts/* /usr/share/jenkins/ref/init.groovy.d/
COPY jobs seed-jobs

ENV JAVA_OPTS -Djenkins.install.runSetupWizard=false
ENV JENKINS_SLAVE_AGENT_PORT=
ENV JENKINS_OPTS="--httpPort=80"
ENV PATH="/$JENKINS_HOME/.local/bin:$PATH"
23 changes: 23 additions & 0 deletions docker/demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# DSTY demo

This image was created to allow people to easily run the Jenkinst Standard Library locally.

It contains the example jobs and the recommended plugins from the main [README.md](../../README.md).

This image is avaliable publicly from [dsty/jenkins:demo](https://hub.docker.com/repository/docker/dsty/jenkins).

## Building locally

From the root of the repo run:
```sh
docker build -t dsty/jenkins:demo -f docker/demo/Dockerfile .
```

## Using

To run this container:
```sh
docker run -d --rm -p 4000:80 dsty/jenkins:demo
```

You can then see the jenkins at [127.0.0.1:4000](http://127.0.0.1:4000). If the seed job hasn't run, you can run it manually.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ import hudson.plugins.filesystem_scm.FSSCM

println('==== Creating seed job')
final String jobName = 'seed-job'
final File jobsDir = new File('/var/jenkins_home/pipeline-library/jobs')
final File jobsDir = new File('/var/jenkins_home/seed-jobs')

final String dslScript = '''\
import java.nio.file.Path
import java.nio.file.Files
import java.nio.file.Paths
Files.walk(Paths.get('/var/jenkins_home/pipeline-library/jobs')).findAll { Path item ->
item.toString().contains('.groovy')
Files.walk(Paths.get('/var/jenkins_home/seed-jobs')).findAll { Path item ->
final String jobName = item.toString()
jobName.contains('.groovy') && jobName.contains('example')
}.forEach { Path item ->
final String fileName = item.getFileName()
final String jobName = fileName.replace('_', '-').replace('.groovy', '')
Expand Down Expand Up @@ -59,7 +62,7 @@ final FSSCM fileScm = new FSSCM(jobsDir.getPath(), false, false, false, false, n

final Jenkins jenkins = Jenkins.getInstance()

def existingJob = jenkins.items.find { def job ->
FreeStyleProject existingJob = jenkins.items.find { def job ->
job.name == jobName
}

Expand Down Expand Up @@ -92,7 +95,11 @@ dslProject.getPublishersList().add(dslBuilder)
println('== Adding Seed Job to Jenkins')
jenkins.add(dslProject, jobName)

println('== Triggering Seed Job polling')
scmTrigger.start(dslProject, true)
println('== Triggering Seed Job')
existingJob = jenkins.items.find { def job ->
job.name == jobName
}

existingJob.scheduleBuild2(0)

println('== Seed Job setup complete')
21 changes: 21 additions & 0 deletions docker/demo/init_scripts/simple-theme.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.simpletheme.CssTextThemeElement;

Jenkins jenkins = Jenkins.get()

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

final String cssOverides = '''\
.pipeline-annotated {
display: none;
}
.pipeline-new-node {
display: none;
}
'''

themeDecorator.setElements([
new CssTextThemeElement(cssOverides)
])

jenkins.save()
24 changes: 23 additions & 1 deletion docker/dev/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,34 @@ ARG baseImage=dsty/jenkins

FROM ${baseImage}

ARG NONROOT_USER=jenkins
ARG DEBIAN_FRONTEND=noninteractive

USER root

RUN apt-get update && \
apt-get install -y \
sudo \
python3 \
python3-pip && \
python3 -m pip install --upgrade pip && \
rm -rf /var/lib/apt/lists/*

RUN echo "#!/bin/sh\n\
sudoIf() { if [ \"\$(id -u)\" -ne 0 ]; then sudo \"\$@\"; else \"\$@\"; fi }\n\
SOCKET_GID=\$(stat -c '%g' /var/run/docker.sock) \n\
if [ \"${SOCKET_GID}\" != '0' ]; then\n\
if [ \"\$(cat /etc/group | grep :\${SOCKET_GID}:)\" = '' ]; then sudoIf groupadd --gid \${SOCKET_GID} docker-host; fi \n\
if [ \"\$(id ${NONROOT_USER} | grep -E \"groups=.*(=|,)\${SOCKET_GID}\(\")\" = '' ]; then sudoIf usermod -aG \${SOCKET_GID} ${NONROOT_USER}; fi\n\
fi\n\
exec \"\$@\"" > /usr/local/share/docker-init.sh \
&& chmod +x /usr/local/share/docker-init.sh \
&& echo $NONROOT_USER ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$NONROOT_USER \
&& chmod 0440 /etc/sudoers.d/$NONROOT_USER

USER jenkins
WORKDIR $JENKINS_HOME

# Install pre-commit and dependencies
COPY tests/requirements.txt /tmp
RUN pip3 install --no-cache-dir -r /tmp/requirements.txt
Expand All @@ -24,4 +43,7 @@ COPY docker/dev/init_scripts/* /usr/share/jenkins/ref/init.groovy.d/
ENV JAVA_OPTS -Djenkins.install.runSetupWizard=false
ENV JENKINS_SLAVE_AGENT_PORT=
ENV JENKINS_OPTS="--httpPort=80"
ENV PATH="/root/.local/bin:$PATH"
ENV PATH="/$JENKINS_HOME/.local/bin:$PATH"

ENTRYPOINT [ "/usr/local/share/docker-init.sh" ]
CMD [ "sleep", "infinity" ]
Loading

0 comments on commit c268577

Please sign in to comment.