Skip to content

Commit 256912c

Browse files
jenkins
1 parent b67c5d4 commit 256912c

File tree

9 files changed

+367
-0
lines changed

9 files changed

+367
-0
lines changed

jenkins/JenkinsFile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
node('jenkins-slave') {
2+
3+
stage('test pipeline') {
4+
sh(script: """
5+
echo "hello"
6+
git clone https://github.com/marcel-dempers/docker-development-youtube-series.git
7+
cd ./docker-development-youtube-series/golang
8+
9+
docker build . -t test
10+
""")
11+
}
12+
}

jenkins/dockerfiles/dockerfile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
FROM openjdk:8-jdk
2+
3+
RUN apt-get update -y && apt-get install -y curl sudo
4+
5+
#Install docker
6+
RUN curl -sSL https://get.docker.com/ | sh
7+
8+
ARG user=jenkins
9+
ARG group=jenkins
10+
ARG uid=10000
11+
ARG gid=10000
12+
13+
ENV HOME /home/${user}
14+
RUN groupadd -g ${gid} ${group}
15+
RUN useradd -c "Jenkins user" -d $HOME -u ${uid} -g ${gid} -m ${user}
16+
RUN usermod -aG docker ${user}
17+
RUN usermod -aG sudo ${user}
18+
19+
ARG VERSION=3.20
20+
ARG AGENT_WORKDIR=/home/${user}/agent
21+
22+
RUN curl --create-dirs -sSLo /usr/share/jenkins/slave.jar https://repo.jenkins-ci.org/public/org/jenkins-ci/main/remoting/${VERSION}/remoting-${VERSION}.jar \
23+
&& chmod 755 /usr/share/jenkins \
24+
&& chmod 644 /usr/share/jenkins/slave.jar
25+
26+
#docker compose cli
27+
RUN curl -L https://github.com/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose \
28+
&& chmod +x /usr/local/bin/docker-compose
29+
30+
COPY jenkins-slave /usr/local/bin/jenkins-slave
31+
RUN chmod 777 /usr/local/bin/jenkins-slave
32+
33+
RUN echo 'jenkins ALL=(ALL) NOPASSWD:ALL'| sudo EDITOR='tee -a' visudo
34+
35+
USER ${user}
36+
ENV AGENT_WORKDIR=${AGENT_WORKDIR}
37+
RUN mkdir /home/${user}/.jenkins && mkdir -p ${AGENT_WORKDIR}
38+
39+
VOLUME /home/${user}/.jenkins
40+
VOLUME ${AGENT_WORKDIR}
41+
WORKDIR /home/${user}
42+
43+
ENTRYPOINT ["/usr/local/bin/jenkins-slave"]

jenkins/dockerfiles/jenkins-slave

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env sh
2+
3+
# The MIT License
4+
#
5+
# Copyright (c) 2015, CloudBees, Inc.
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
25+
# Usage jenkins-slave.sh [options] -url http://jenkins [SECRET] [AGENT_NAME]
26+
# Optional environment variables :
27+
# * JENKINS_TUNNEL : HOST:PORT for a tunnel to route TCP traffic to jenkins host, when jenkins can't be directly accessed over network
28+
# * JENKINS_URL : alternate jenkins URL
29+
# * JENKINS_SECRET : agent secret, if not set as an argument
30+
# * JENKINS_AGENT_NAME : agent name, if not set as an argument
31+
# * JENKINS_AGENT_WORKDIR : agent work directory, if not set by optional parameter -workDir
32+
33+
if [ $# -eq 1 ]; then
34+
35+
# if `docker run` only has one arguments, we assume user is running alternate command like `bash` to inspect the image
36+
exec "$@"
37+
38+
else
39+
40+
# if -tunnel is not provided try env vars
41+
case "$@" in
42+
*"-tunnel "*) ;;
43+
*)
44+
if [ ! -z "$JENKINS_TUNNEL" ]; then
45+
TUNNEL="-tunnel $JENKINS_TUNNEL"
46+
fi ;;
47+
esac
48+
49+
# if -workDir is not provided try env vars
50+
if [ ! -z "$JENKINS_AGENT_WORKDIR" ]; then
51+
case "$@" in
52+
*"-workDir"*) echo "Warning: Work directory is defined twice in command-line arguments and the environment variable" ;;
53+
*)
54+
WORKDIR="-workDir $JENKINS_AGENT_WORKDIR" ;;
55+
esac
56+
fi
57+
58+
if [ -n "$JENKINS_URL" ]; then
59+
URL="-url $JENKINS_URL"
60+
fi
61+
62+
if [ -n "$JENKINS_NAME" ]; then
63+
JENKINS_AGENT_NAME="$JENKINS_NAME"
64+
fi
65+
66+
if [ -z "$JNLP_PROTOCOL_OPTS" ]; then
67+
echo "Warning: JnlpProtocol3 is disabled by default, use JNLP_PROTOCOL_OPTS to alter the behavior"
68+
JNLP_PROTOCOL_OPTS="-Dorg.jenkinsci.remoting.engine.JnlpProtocol3.disabled=true"
69+
fi
70+
71+
# If both required options are defined, do not pass the parameters
72+
OPT_JENKINS_SECRET=""
73+
if [ -n "$JENKINS_SECRET" ]; then
74+
case "$@" in
75+
*"${JENKINS_SECRET}"*) echo "Warning: SECRET is defined twice in command-line arguments and the environment variable" ;;
76+
*)
77+
OPT_JENKINS_SECRET="${JENKINS_SECRET}" ;;
78+
esac
79+
fi
80+
81+
OPT_JENKINS_AGENT_NAME=""
82+
if [ -n "$JENKINS_AGENT_NAME" ]; then
83+
case "$@" in
84+
*"${JENKINS_AGENT_NAME}"*) echo "Warning: AGENT_NAME is defined twice in command-line arguments and the environment variable" ;;
85+
*)
86+
OPT_JENKINS_AGENT_NAME="${JENKINS_AGENT_NAME}" ;;
87+
esac
88+
fi
89+
90+
#TODO: Handle the case when the command-line and Environment variable contain different values.
91+
#It is fine it blows up for now since it should lead to an error anyway.
92+
93+
exec java $JAVA_OPTS $JNLP_PROTOCOL_OPTS -cp /usr/share/jenkins/slave.jar hudson.remoting.jnlp.Main -headless $TUNNEL $URL $WORKDIR $OPT_JENKINS_SECRET $OPT_JENKINS_AGENT_NAME "$@"
94+
fi

jenkins/jenkins.deployment.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
apiVersion: extensions/v1beta1
2+
kind: Deployment
3+
metadata:
4+
name: jenkins
5+
labels:
6+
name: jenkins
7+
app: jenkins
8+
spec:
9+
replicas: 1
10+
selector:
11+
matchLabels:
12+
name: jenkins
13+
template:
14+
metadata:
15+
labels:
16+
app: jenkins
17+
name: jenkins
18+
name: jenkins
19+
spec:
20+
serviceAccountName: jenkins
21+
containers:
22+
- env:
23+
- name: JAVA_OPTS
24+
value: -Xmx2048m -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85
25+
image: jenkins/jenkins #:lts-alpine
26+
imagePullPolicy: IfNotPresent
27+
name: jenkins
28+
ports:
29+
- containerPort: 8080
30+
protocol: TCP
31+
- containerPort: 50000
32+
protocol: TCP
33+
# resources:
34+
# limits:
35+
# cpu: "1"
36+
# memory: 1Gi
37+
# requests:
38+
# cpu: "1"
39+
# memory: 1Gi
40+
volumeMounts:
41+
- mountPath: /var/jenkins_home
42+
name: jenkins
43+
restartPolicy: Always
44+
securityContext:
45+
#fsGroup: 1000
46+
runAsUser: 0
47+
terminationGracePeriodSeconds: 30
48+
volumes:
49+
- name: jenkins
50+
persistentVolumeClaim:
51+
claimName: jenkins-claim

jenkins/jenkins.pv.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
apiVersion: v1
2+
kind: PersistentVolume
3+
metadata:
4+
name: jenkins
5+
labels:
6+
type: local
7+
spec:
8+
storageClassName: manual
9+
capacity:
10+
storage: 2Gi
11+
accessModes:
12+
- ReadWriteOnce
13+
hostPath:
14+
path: "/mnt/data"
15+
# apiVersion: v1
16+
# kind: PersistentVolume
17+
# metadata:
18+
# name: jenkins
19+
# #annotations:
20+
# #pv.beta.kubernetes.io/gid: "1000"
21+
# spec:
22+
# capacity:
23+
# storage: 5Gi
24+
# accessModes:
25+
# - ReadWriteOnce
26+
# azureFile:
27+
# secretName: storage-connection
28+
# secretNamespace: jenkins
29+
# shareName: {{ .Values.jenkins.azurefileshare }}
30+
# readOnly: false
31+
# claimRef:
32+
# name: jenkins-pvc
33+
# kind: PersistenVolumeClaim
34+
# namespace: jenkins
35+
# persistentVolumeReclaimPolicy: Retain

jenkins/jenkins.pvc.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apiVersion: v1
2+
kind: PersistentVolumeClaim
3+
metadata:
4+
name: jenkins-claim
5+
spec:
6+
storageClassName: manual
7+
accessModes:
8+
- ReadWriteOnce
9+
resources:
10+
requests:
11+
storage: 2Gi
12+
13+
# apiVersion: v1
14+
# kind: PersistentVolumeClaim
15+
# metadata:
16+
# name: jenkins-pvc
17+
# namespace: jenkins
18+
# spec:
19+
# accessModes:
20+
# - ReadWriteOnce
21+
# resources:
22+
# requests:
23+
# storage: 5Gi
24+
# storageClassName: default
25+
# volumeName: jenkins

jenkins/jenkins.rbac.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
apiVersion: v1
3+
kind: ServiceAccount
4+
metadata:
5+
name: jenkins
6+
---
7+
kind: Role
8+
apiVersion: rbac.authorization.k8s.io/v1
9+
metadata:
10+
name: jenkins
11+
rules:
12+
- apiGroups: [""]
13+
resources: ["pods"]
14+
verbs: ["create","delete","get","list","patch","update","watch"]
15+
- apiGroups: [""]
16+
resources: ["pods/exec"]
17+
verbs: ["create","delete","get","list","patch","update","watch"]
18+
- apiGroups: [""]
19+
resources: ["pods/log"]
20+
verbs: ["get","list","watch"]
21+
- apiGroups: [""]
22+
resources: ["secrets"]
23+
verbs: ["get"]
24+
25+
---
26+
apiVersion: rbac.authorization.k8s.io/v1
27+
kind: RoleBinding
28+
metadata:
29+
name: jenkins
30+
roleRef:
31+
apiGroup: rbac.authorization.k8s.io
32+
kind: Role
33+
name: jenkins
34+
subjects:
35+
- kind: ServiceAccount
36+
name: jenkins
37+
namespace: jenkins

jenkins/jenkins.service.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: jenkins
5+
labels:
6+
app: jenkins
7+
spec:
8+
type: ClusterIP
9+
ports:
10+
- name: ui
11+
port: 8080
12+
targetPort: 8080
13+
protocol: TCP
14+
- name: slave
15+
port: 50000
16+
protocol: TCP
17+
- name: http
18+
port: 80
19+
targetPort: 8080
20+
selector:
21+
app: jenkins

jenkins/readme.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Setting up Jenkins Agent
2+
3+
After installing `kubernetes-plugin` for Jenkins
4+
* Go to Manage Jenkins | Bottom of Page | Cloud | Kubernetes (Add kubenretes cloud)
5+
* Fill out plugin values
6+
* Name: kubernetes
7+
* Kubernetes URL: https://kubernetes.default:443
8+
* Kubernetes Namespace: jenkins
9+
* Credentials | Add | Jenkins (Choose Kubernetes service account option & Global + Save)
10+
* Test Connection | Should be successful! If not, check RBAC permissions and fix it!
11+
* Jenkins URL: http://jenkins
12+
* Tunnel : jenkins:50000
13+
* Apply cap only on alive pods : yes!
14+
* Add Kubernetes Pod Template
15+
* Name: jenkins-slave
16+
* Namespace: jenkins
17+
* Labels: jenkins-slave (you will need to use this label on all jobs)
18+
* Containers | Add Template
19+
* Name: jnlp
20+
* Docker Image: aimvector/jenkins-slave
21+
* Command to run : <Make this blank>
22+
* Arguments to pass to the command: <Make this blank>
23+
* Allocate pseudo-TTY: yes
24+
* Add Volume
25+
* HostPath type
26+
* HostPath: /var/run/docker.sock
27+
* Mount Path: /var/run/docker.sock
28+
* Timeout in seconds for Jenkins connection: 300
29+
* Save
30+
31+
# Test a build
32+
33+
To run docker commands inside a jenkins agent you will need a custom jenkins agent with docker-in-docker working.
34+
Take a look and build the docker file in `./dockerfiles/jenkins-agent`
35+
Push it to a registry and use it instead of above configured `* Docker Image: jenkins/jnlp-slave`
36+
If you do not use the custom image, the below pipeline will not work because default `* Docker Image: jenkins/jnlp-slave` public image does not have docker ability.
37+
38+
* Add a Jenkins Pipeline
39+
40+
```
41+
node('jenkins-slave') {
42+
43+
stage('unit-tests') {
44+
sh(script: """
45+
docker run --rm alpine /bin/sh -c "echo hello world"
46+
""")
47+
}
48+
}
49+
```

0 commit comments

Comments
 (0)