INDEX
########################################################### 2024-01-05 17:00 ########################################################### Devops Toolkit... Running Jenkins in Kubernetes https://www.youtube.com/watch?v=2Kc3fUJANAc Jenkins operator is (maybe still) in early stages - use helm instad helm repo add jenkinsci https://charts.jenkins.io # jenkins-values.yaml controller: ingress: enabled: true apiVersion: networking.k8s.io/v1 hostName: jenkins.3.67.0.140.nip.io installPlugins: - kubernetes:1.29.4 - workflow-aggregator:2.6 - git:4.7.1 - configuration-as-code:1.51 - blueocean:1.24.6 # Install and open jenkins (running in k8) helm upgrade --install jenkins jenkinsci/jenkins --namespace jenkins \ --create-namespace --values jenkins-values.yaml --wait echo http://jenkins.$INGRESS_HOST.nip.io # Get password for "admin" account kubectl --namespace jenkins get secret jenkins \ --output jsonpath="{.data.jenkins-admin-password}" | base64 --decode Now open "Blue Ocean" and add a github token - follow steps to add pipeline # Jenkinsfile pipeline { agent { kubernetes { defaultContainer "shell" yamlFile "jenkins-pod.yaml" } } environment {} # define environment variables here stages { stage("Build") { steps { # Build and push images } } stage("Test") { when { changeRequest target: "master" } # Only test master branch steps { # Create namespace # Build and run image # Rollout changes # Run tests on that image # Delete namespace } } stage("Deploy") { when { branch "master" } steps { # Point production to use new image # Apply new changes } } } } Now define what these actions are and the role doing these actions # jenkins-pod.yaml apiVersion: v1 kind: Pod spec: serviceAccount: jenkins containers: - name: shell # How to run shell commands image: alpine command: - cat tty: true - name: kaniko image: gcr.io/kaniko-project/executor:debug command: - cat tty: true voluemMounts: # Mount secrets - name: kaniko-secret mountPath: /kaniko/.docker/ - name: kustomize image: nekottyo/kustomize-kubeval command: - cat tty: true volumes: - name: kaniko-secret secret: secretName: regcred items: - key: .dockerconfigjson path: config.json kubectl --namespace jenkins create secret docker-registry regcred \ --docker-server ... # username, password, email # Last thing to do is define roles # roles.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: jenkins rules: - apiGroups: - "*" resources: - "*" verbs: - "*" --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: jenkins roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: jenkins subjects: - kind: ServiceAccount name: jenkins namespace: jenkins kubectl apply --filename roles.yaml Now apply the webhooks to link these together echo "http://jenkins.$INGRESS_HOST.nip.io/github-webhook/ # Get URL # Github repo - settings - webhooks - add (with jenkins URL, json, send all) # Jenkins should now be tracking changes git push --set-upstream origin BRANCH_NAME # Pushes to new branch gh pr create --title "New feature" --body "merging with master" # Makes a PR # Look into BlueOcean and can see Jenkins has tested and run this # Merge PR on github and Jenkins will test and run again # If all working fine, should be now deployed to production Problem is that Jenkins engine does not scale