Skip to content

Instantly share code, notes, and snippets.

@hayderimran7
Forked from vfarcic/80-jenkins.sh
Created May 20, 2021 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hayderimran7/45951703b225812976e42a7d36cc1ff4 to your computer and use it in GitHub Desktop.
Save hayderimran7/45951703b225812976e42a7d36cc1ff4 to your computer and use it in GitHub Desktop.
#################################
# Running Jenkins In Kubernetes #
# Tutorial And Review #
# https://youtu.be/2Kc3fUJANAc #
#################################
# Referenced videos:
# - GitHub CLI - How to manage repositories more efficiently: https://youtu.be/BII6ZY2Rnlc
# - Kaniko - Building Container Images In Kubernetes Without Docker: https://youtu.be/EgwVQN6GNJg
# - Kustomize - How to Simplify Kubernetes Configuration Management: https://youtu.be/Twtbg6LFnAg
#########
# Setup #
#########
# Create a Kubernetes cluster with NGINX Ingress
gh repo fork vfarcic/jenkins-demo \
--clone
cd jenkins-demo
# If NOT EKS
export INGRESS_HOST=$(kubectl \
--namespace ingress-nginx \
get svc ingress-nginx-controller \
--output jsonpath="{.status.loadBalancer.ingress[0].ip}")
# If EKS
export INGRESS_HOSTNAME=$(kubectl \
--namespace ingress-nginx \
get svc ingress-nginx-controller \
--output jsonpath="{.status.loadBalancer.ingress[0].hostname}")
# If EKS
export INGRESS_HOST=$(\
dig +short $INGRESS_HOSTNAME)
echo $INGRESS_HOST
# Repeat the `export` commands if the output is empty
# If the output contains more than one IP, wait for a while longer, and repeat the `export` commands.
# If the output continues having more than one IP, choose one of them and execute `export INGRESS_HOST=[...]` with `[...]` being the selected IP.
export REGISTRY_SERVER=https://index.docker.io/v1/
# Replace `[...]` with the registry username
export REGISTRY_USER=[...]
# Replace `[...]` with the registry password
export REGISTRY_PASS=[...]
# Replace `[...]` with the registry email
export REGISTRY_EMAIL=[...]
3840 X 2160
cat kustomize/base/ingress.yaml \
| sed -e "s@acme.com@jenkins-demo.$INGRESS_HOST.nip.io@g" \
| tee kustomize/overlays/production/ingress.yaml
cat kustomize/base/ingress.yaml \
| sed -e "s@acme.com@jenkins-demo.$INGRESS_HOST.nip.io@g" \
| tee kustomize/overlays/preview/ingress.yaml
cat Jenkinsfile \
| sed -e "s@vfarcic@$REGISTRY_USER@g" \
| sed -e "s@acme.com@$INGRESS_HOST.nip.io@g" \
| tee Jenkinsfile
cat jenkins-values.yaml \
| sed -e "s@hostName: .*@hostName: jenkins.$INGRESS_HOST.nip.io@g" \
| tee jenkins-values.yaml
kubectl create namespace production
kubectl create namespace previews
######################
# Installing Jenkins #
######################
pwd
helm repo add jenkinsci https://charts.jenkins.io
helm repo update
gh repo view jenkinsci/helm-charts --web
cat jenkins-values.yaml
helm upgrade --install \
jenkins jenkinsci/jenkins \
--namespace jenkins \
--create-namespace \
--values jenkins-values.yaml \
--wait
#########################
# Creating Jenkins jobs #
#########################
echo http://jenkins.$INGRESS_HOST.nip.io
# Open it in browser
kubectl --namespace jenkins \
get secret jenkins \
--output jsonpath="{.data.jenkins-admin-password}" \
| base64 --decode && echo
#########################
# Exploring Jenkinsfile #
#########################
cat Jenkinsfile
#########################
# Exploring PodTemplate #
#########################
cat jenkins-pod.yaml
kubectl --namespace jenkins \
create secret \
docker-registry regcred \
--docker-server $REGISTRY_SERVER \
--docker-username $REGISTRY_USER \
--docker-password $REGISTRY_PASS \
--docker-email $REGISTRY_EMAIL
##################################
# Defining roles and permissions #
##################################
cat roles.yaml
kubectl apply --filename roles.yaml
############################
# Creating GitHub webhooks #
############################
gh repo view --web
echo http://jenkins.$INGRESS_HOST.nip.io/github-webhook/
# Copy and paste the address into the webhook *Payload URL* field
###########################
# Creating a pull request #
###########################
git checkout -b my-feature
# Open config.toml and change something
git add .
git commit -m "Is this a new feature?"
git push --set-upstream origin my-feature
gh pr create \
--title "My feature" \
--body "I'm too lazy to write descriptions"
#####################
# Building mainline #
#####################
gh repo view --web
# Navigate to the PR and merge it.
git checkout master
git pull
echo http://jenkins-demo.$INGRESS_HOST.nip.io
# Open it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment