Push docker image to digital ocean image registry and update image on Kubernetes using Jenkins.
Here we will be using Jenkins inside docker. We will be using jenkins:lts image from docker-hub.
docker run -p 8082:8080 \
-v /var/run/docker.sock:/var/run/docker.sock \
--name jenkins \
jenkins/jenkins:lts
Jenkins will run on port 8082 of our system. You can change the ports accordingly.
Now we need to install docker inside Jenkins container. We will be having 2 users inside our docker container one will be root and another will be jenkins.
Lets login as root user first to install docker
docker exec -it -u root jenkins bashapt-get updateapt install docker.iosystemctl start dockersystemctl enable docker
Now docker would have been installed inside jenkins container. So we will be able to do docker builds now.
To check docker version
docker --version
Now exit from root user and lets login as jenkins user . As jenkins will be doing docker builds and pushing to digital ocean image registery.
docker exec -it -u jenkins jenkins bash
Now we need to install doctl for jenkins user. Check alternate ways to install doctl and authenticatation here. We will be using installing through github for now.
cd ~
wget https://github.com/digitalocean/doctl/releases/download/v1.46.0/doctl-1.46.0-linux-amd64.tar.gztar xf ~/doctl-1.46.0-linux-amd64.tar.gzsudo mv ~/doctl /usr/local/bin
Now you need to create digital ocean API Token. For that check digital ocean link.
Once you have your API token, run
doctl auth init
and enter you API access token.
Check your account details by
doctl account get
Now login to doctl registry by
doctl registry login
Now we are all done with our registry login. All we need to do is place kube config file inside jenkins container. For that go to jenkins_home by
cd /var/jenkins_home/
Create a new directory .kube and there put your config.yaml file
mkdir .kube cd .kubenano config.yaml
Paste your config inside config.yaml
Now login to jenkins and create new pipeline authenticate it with your github project.
Now put this Jenkinsfile in your github project.
First we will get code latest changes from github by
checkout scm
then we will do docker build with
docker build -t registry.digitalocean.com/repo-name/image-name:latest .
then we will be pushing it to our image registry andupdating our image tag.
docker push registry.digitalocean.com/repo-name/image-name:latestkubectl set image deployment/deployment-name container-name=registry.digitalocean.com/supportgenie/react-apps-portal:$buildId --record
Here is the full jenkins file that we can put inside our project.
pipeline {
agent any
stages {
stage('Checkout') { steps {
checkout scm
}
}
stage("Docker Build") {
steps {
sh " docker build -t registry.digitalocean.com/repo-name/image-name:latest ."
}
}
stage("Docker Push") {
steps {
sh "docker push registry.digitalocean.com/repo-name/image-name:latest"sh "KUBECONFIG=/var/jenkins_home/.kube/config.yaml kubectl set image deployment/deployment-name container-name=registry.digitalocean.com/supportgenie/react-apps-portal:$buildId --record"
}
}
}
}