diff --git a/Days/CICD/Jenkins/Pipeline/Dockerfile b/Days/CICD/Jenkins/Pipeline/Dockerfile new file mode 100644 index 0000000..a72b87f --- /dev/null +++ b/Days/CICD/Jenkins/Pipeline/Dockerfile @@ -0,0 +1,11 @@ +FROM busybox:latest +ENV PORT=8000 + +ADD index.html /www/index.html + +# EXPOSE $PORT + +HEALTHCHECK CMD nc -z localhost $PORT + +# Create a basic webserver and run it until the container is stopped +CMD echo "httpd started" && trap "exit 0;" TERM INT; httpd -v -p $PORT -h /www -f & wait \ No newline at end of file diff --git a/Days/CICD/Jenkins/Pipeline/Jenkinsfile b/Days/CICD/Jenkins/Pipeline/Jenkinsfile new file mode 100644 index 0000000..100961d --- /dev/null +++ b/Days/CICD/Jenkins/Pipeline/Jenkinsfile @@ -0,0 +1,53 @@ +podTemplate(yaml: ''' + apiVersion: v1 + kind: Pod + spec: + containers: + - name: maven + image: maven:3.8.1-jdk-8 + command: + - sleep + args: + - 99d + - name: kaniko + image: gcr.io/kaniko-project/executor:debug + command: + - sleep + args: + - 9999999 + volumeMounts: + - name: kaniko-secret + mountPath: /kaniko/.docker + restartPolicy: Never + volumes: + - name: kaniko-secret + secret: + secretName: dockercred + items: + - key: .dockerconfigjson + path: config.json +''') { + node(POD_LABEL) { + stage('Clone Repository') { + git url: 'https://github.com/MichaelCade/Jenkins-HelloWorld.git', branch: 'main' + container('maven') { + stage('Build Image') { + sh ''' + echo "Tests passed" + ''' + } + } + } + + stage('Test Image') { + container('kaniko') { + stage('Build Hello World App') { + sh ''' + /kaniko/executor --context `pwd` --destination michaelcade1/helloworld:1.0 + ''' + } + } + } + + } +} \ No newline at end of file diff --git a/Days/CICD/Jenkins/Pipeline/index.html b/Days/CICD/Jenkins/Pipeline/index.html new file mode 100644 index 0000000..56ecc69 --- /dev/null +++ b/Days/CICD/Jenkins/Pipeline/index.html @@ -0,0 +1,3 @@ +
+ Hello World! +\ No newline at end of file diff --git a/Days/Images/Day73_CICD1.png b/Days/Images/Day73_CICD1.png new file mode 100644 index 0000000..0729522 Binary files /dev/null and b/Days/Images/Day73_CICD1.png differ diff --git a/Days/Images/Day73_CICD10.png b/Days/Images/Day73_CICD10.png new file mode 100644 index 0000000..51b4a92 Binary files /dev/null and b/Days/Images/Day73_CICD10.png differ diff --git a/Days/Images/Day73_CICD11.png b/Days/Images/Day73_CICD11.png new file mode 100644 index 0000000..53791b9 Binary files /dev/null and b/Days/Images/Day73_CICD11.png differ diff --git a/Days/Images/Day73_CICD12.png b/Days/Images/Day73_CICD12.png new file mode 100644 index 0000000..ccd6b32 Binary files /dev/null and b/Days/Images/Day73_CICD12.png differ diff --git a/Days/Images/Day73_CICD13.png b/Days/Images/Day73_CICD13.png new file mode 100644 index 0000000..8fdf1f8 Binary files /dev/null and b/Days/Images/Day73_CICD13.png differ diff --git a/Days/Images/Day73_CICD14.png b/Days/Images/Day73_CICD14.png new file mode 100644 index 0000000..c608d33 Binary files /dev/null and b/Days/Images/Day73_CICD14.png differ diff --git a/Days/Images/Day73_CICD15.png b/Days/Images/Day73_CICD15.png new file mode 100644 index 0000000..ccc9199 Binary files /dev/null and b/Days/Images/Day73_CICD15.png differ diff --git a/Days/Images/Day73_CICD2.png b/Days/Images/Day73_CICD2.png new file mode 100644 index 0000000..a4bc678 Binary files /dev/null and b/Days/Images/Day73_CICD2.png differ diff --git a/Days/Images/Day73_CICD3.png b/Days/Images/Day73_CICD3.png new file mode 100644 index 0000000..2e60da9 Binary files /dev/null and b/Days/Images/Day73_CICD3.png differ diff --git a/Days/Images/Day73_CICD4.png b/Days/Images/Day73_CICD4.png new file mode 100644 index 0000000..7b85813 Binary files /dev/null and b/Days/Images/Day73_CICD4.png differ diff --git a/Days/Images/Day73_CICD5.png b/Days/Images/Day73_CICD5.png new file mode 100644 index 0000000..14fdf06 Binary files /dev/null and b/Days/Images/Day73_CICD5.png differ diff --git a/Days/Images/Day73_CICD6.png b/Days/Images/Day73_CICD6.png new file mode 100644 index 0000000..0ccf65d Binary files /dev/null and b/Days/Images/Day73_CICD6.png differ diff --git a/Days/Images/Day73_CICD7.png b/Days/Images/Day73_CICD7.png new file mode 100644 index 0000000..86b93bf Binary files /dev/null and b/Days/Images/Day73_CICD7.png differ diff --git a/Days/Images/Day73_CICD8.png b/Days/Images/Day73_CICD8.png new file mode 100644 index 0000000..2aa2995 Binary files /dev/null and b/Days/Images/Day73_CICD8.png differ diff --git a/Days/Images/Day73_CICD9.png b/Days/Images/Day73_CICD9.png new file mode 100644 index 0000000..d6ab2ad Binary files /dev/null and b/Days/Images/Day73_CICD9.png differ diff --git a/Days/day73.md b/Days/day73.md index 29ddd01..867a36c 100644 --- a/Days/day73.md +++ b/Days/day73.md @@ -1,10 +1,215 @@ -## Building a Jenkins pipeline -Job creation -Building the pipeline +## Building a Jenkins Pipeline +In the last section we got Jenkins deployed to our Minikube cluster and we set up a very basic Jenkins Pipeline, that didn't do much at all other than echo out the stages of a Pipeline. +You might have also seen that there are some example scripts available for us to run in the Jenkins Pipeline creation. + -https://github.com/MichaelCade/jenkins-example +The first demo script is "Declartive (Kubernetes)" and you can see the stages below. +``` +// Uses Declarative syntax to run commands inside a container. +pipeline { + agent { + kubernetes { + // Rather than inline YAML, in a multibranch Pipeline you could use: yamlFile 'jenkins-pod.yaml' + // Or, to avoid YAML: + // containerTemplate { + // name 'shell' + // image 'ubuntu' + // command 'sleep' + // args 'infinity' + // } + yaml ''' +apiVersion: v1 +kind: Pod +spec: + containers: + - name: shell + image: ubuntu + command: + - sleep + args: + - infinity +''' + // Can also wrap individual steps: + // container('shell') { + // sh 'hostname' + // } + defaultContainer 'shell' + } + } + stages { + stage('Main') { + steps { + sh 'hostname' + } + } + } +} +``` +You can see below the outcome of what happens when this Pipeline is ran. + + +### Job creation + +**Goals** + +- Create a simple app and store in GitHub public repository (https://github.com/scriptcamp/kubernetes-kaniko.git) + +- Use Jenkins to build our docker Container image and push to docker hub. (for this we will use a private repository) + +To achieve this in our Kubernetes cluster running in or using Minikube we need to use something called [Kaniko](https://github.com/GoogleContainerTools/kaniko#running-kaniko-in-a-kubernetes-cluster) It general though if you are using Jenkins in a real Kubernetes cluster or you are running it on a server then you can specify an agent which will give you the ability to perform the docker build commands and upload that to DockerHub. + +With the above in mind we are also going to deploy a secret into Kubernetes with our GitHub credentials. + +``` +kubectl create secret docker-registry dockercred \ + --docker-server=https://index.docker.io/v1/ \ + --docker-username=