Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zbrtsn/jenkins-kubernetes
Jetkins --> pipeline --> docker --> dockerhub --> kubernetes
https://github.com/zbrtsn/jenkins-kubernetes
Last synced: about 2 months ago
JSON representation
Jetkins --> pipeline --> docker --> dockerhub --> kubernetes
- Host: GitHub
- URL: https://github.com/zbrtsn/jenkins-kubernetes
- Owner: zbrtsn
- Created: 2024-07-31T11:47:31.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-08-21T10:44:26.000Z (5 months ago)
- Last Synced: 2024-08-22T00:36:43.977Z (5 months ago)
- Language: Dockerfile
- Size: 44.9 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Jenkins-Kubernetes
I will pull a simple Flask project from GitLab or GitHub via Jenkins. Then, I will build the project with Docker (the project includes a Dockerfile!), and push the built image to Docker Hub. Locally, I use the "kind" tool for Kubernetes. I will also activate my manifest files, i.e., .yaml files associated with the Docker Hub repository of the pushed project, through Jenkins in the same way. My goal is to automate all these processes.
Jetkins --> pipeline --> docker --> dockerhub --> kubernetes## Main Pipeline
[Jenkins Pipeline](https://github.com/zbrtsn/Jenkins-Kubernetes/blob/main/jenkins-pipeline)## Explanation
```
environment {
registryCredential = 'dockerhub-credentials' // all the docker hub username and password are in here
GIT_REPO_URL = 'https://mtgit.mediatriple.net/zubeyir.tosun/basic-to-do-flask2.git' // just classic repo url
DOCKERHUB_REPO = 'pyouck/basic_todo_flask'
KUBE_CONFIG = '/home/zub/.kube/config' // Adjust the path to your kubeconfig
}
```
Here, we define our global variables.
Additionally, if we have created credentials, we can also define them globally here.
```
stage('Clone Git Repository') {
steps {
git url: "${GIT_REPO_URL}", branch: 'latest'
}
}
```
In the variables section, it pulls the repository from the Git URL based on the branch name we provided.
```
stage('Build Docker Image') {
steps {
script {
dir('.') {
// Build the Docker image with --no-cache
sh 'docker build -t ${DOCKERHUB_REPO}:test --no-cache .'
}
}
}
}
```
It builds the repository that it pulls.
```
stage('Push Docker Image to DockerHub') {
steps {
script {
docker.withRegistry('', registryCredential) {
sh 'docker push ${DOCKERHUB_REPO}:test'
}
}
}
}
```
Here, registryCredential contains the Docker Hub account username and password under the credential named dockerhub-credentials. This allows the docker.withRegistry code to connect to Docker Hub, which is pre-installed with a Jenkins plugin. After logging in, it pushes the built image with the "test" tag to the name specified in DOCKERHUB_REPO.
```
stage('Update Kubernetes Deployment') {
steps {
sh 'kubectl delete -f /home/zub/ProgramFiles/b_flask/k8s/kind/kind-deployment.yaml --kubeconfig=${KUBE_CONFIG}' // first deleting it for not making any mistakes. You don't have to do that.
sh 'kubectl apply -f /home/zub/ProgramFiles/b_flask/k8s/kind/kind-deployment.yaml --kubeconfig=${KUBE_CONFIG}'}
}stage('Run Flask Application') {
steps {
sh 'kubectl delete -f /home/zub/ProgramFiles/b_flask/k8s/kind/kind-service.yaml --kubeconfig=${KUBE_CONFIG}'
sh 'kubectl apply -f /home/zub/ProgramFiles/b_flask/k8s/kind/kind-service.yaml --kubeconfig=${KUBE_CONFIG}
}
}
}
```
Since my Kubernetes is installed locally, I specified the path `.kube/config` in the variables section. It applies accordingly. I remove it because sometimes there can be conflicts, and removing it resolves this issue. A check could be added here to determine whether the YAML has been applied or not.------------
## If your repo is private?```pipeline
stages {
stage('Clone Git Repository') {
steps {
git branch: 'latest', credentialsId: 'gitlab-token', url: "${GIT_REPO_URL}"
// you can add "gitlab-token" in Manage Jenkins --> Credentials --> Global --> Add Credentials --> Username with password and id = gitlab-token
}
}
```
We can access the private repository using a credential named gitlab-token, which contains the GitLab account's username and password.
The repository URL is already obtained from the variable in the environment section above. You can modify the branch part according to your needs in the code. Alternatively, instead of adjusting the branch part within the code, you can create a variable in the environment section and direct it here to simplify the process.------------
## If you wanna get your image tag name from deployment.yaml```
stages {
stage('Clone Git Repository') { // Git projesinin çekilme aşaması
steps {
git branch: 'latest', credentialsId: 'gitlab-login', url: "${GIT_REPO_URL}"
}
}
stage('Get Image Tag from deployment.yaml') {
steps {
script {
def yamlFile = readFile('kind-deployment.yaml') // Here read the file
def tag = yamlFile.find(/image:.*:(\S+)/) { match -> match[1] } // Catch the tag name here
if (tag == null) {
error 'Could not find image tag in deployment.yaml'
}
env.IMAGE_TAG = tag
}
}
}
stage('Build Docker Image') { // repo'dan çekilen projeyi build etme aşaması
steps {
script {
def imageTag = "${env.IMAGE_TAG}"
dockerImage = docker.build("${DOCKERHUB_REPO}:${imageTag}", "--no-cache .")
}
}
}
stage('Push Docker Image to DockerHub') { // docker hub'a push
steps {
script {
def imageTag = "${env.IMAGE_TAG}"
docker.withRegistry('', registryCredential) {
dockerImage.push("${imageTag}")
}
}
}
}
}
```
The `deployment.yaml` is inside the repository. When you clone the repository, it reads the `deployment.yaml` inside it and retrieves the tag of the image section.
------------
## Credential
- registryCredential = 'dockerhub-credentials'
dockerhub-credentials is a credential created within Jenkins. It is of type "Username and password" and contains the Docker Hub account's username and password. It is passed into the pipeline using `registryCredential`.
------------
## Related ProjectsHere are some related projects;
[Basic Flask Project](https://github.com/zbrtsn/basic-to-do-flask.git)
[Kubernetes-Kind](https://github.com/zbrtsn/kurbernetes-kind)