Certified Jenkins Engineer
Kubernetes and GitOps
Demo Kubernetes Deploy Update Image Tag
In this tutorial, we’ll automate updating the Docker image tag in Kubernetes manifests and commit changes back to a GitOps repository using a Jenkins pipeline, triggered by Git webhooks.
Prerequisites
Component | Description | Reference |
---|---|---|
GitOps Repo | solar-system-gitops-argo-cd in Gitea under dasher-org | |
Argo CD App | solar-system-argo-app tracking the kubernetes directory | Argo CD Application |
Jenkins Instance | Controller with credentials to push to Gitea | Jenkins Credentials |
1. Inspect the Manifest Repository
Open the Gitea repo solar-system-gitops-argo-cd
and navigate to the kubernetes
folder:
Examine deployment.yml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: solar-system
namespace: solar-system
spec:
replicas: 2
template:
spec:
containers:
- name: solar-system
image: siddharth67/solar-system:3e9063be059342b1916f020e034344fb267d1
imagePullPolicy: Always
ports:
- containerPort: 3000
2. Check Argo CD Application Status
Argo CD will show the application as OutOfSync before resources are applied:
Verify no resources exist in the solar-system
namespace:
kubectl get all -n solar-system
3. Jenkins Pipeline: Add “K8S Update Image Tag” Stage
Add a new declarative stage to your Jenkinsfile
to:
- Run only on pull request branches (
PR*
). - Clone the GitOps repo.
- Update the Docker image tag in
deployment.yml
. - Commit & push to
feature-$BUILD_ID
.
Note
Ensure the Jenkins agent has git
and sed
installed for cloning and file editing.
stage('K8S Update Image Tag') {
when { branch 'PR*' }
steps {
script {
if (fileExists('solar-system-gitops-argo-cd')) {
sh 'rm -rf solar-system-gitops-argo-cd'
}
}
sh 'git clone -b main http://64.227.187.25:5555/dasher-org/solar-system-gitops-argo-cd'
dir('solar-system-gitops-argo-cd/kubernetes') {
sh '''
git checkout main
git checkout -b feature-$BUILD_ID
sed -i "s#image: .*#image: siddharth67/solar-system:$GIT_COMMIT#g" deployment.yml
git config user.email "[email protected]"
git config user.name "Jenkins"
git remote set-url origin http://[email protected]:5555/dasher-org/solar-system-gitops-argo-cd
git add deployment.yml
git commit -m "Update Docker image to $GIT_COMMIT"
git push -u origin feature-$BUILD_ID
'''
}
}
post {
always {
script {
if (fileExists('solar-system-gitops-argo-cd')) {
sh 'rm -rf solar-system-gitops-argo-cd'
}
}
}
}
}
4. Configure Gitea API Token
4.1 Generate Token in Gitea
In Gitea user settings, create a new access token named jenkins-token
with read/write scope:
4.2 Add Token to Jenkins Credentials
Go to Credentials > System > Global credentials in Jenkins and add a Secret text credential with ID gitea-api-token
:
Reference it:
environment {
GITEA_TOKEN = credentials('gitea-api-token')
}
Warning
Keep your API tokens secure. Do not hardcode secrets in your Jenkinsfile
.
5. Webhook Trigger on Pull Requests
Configure a Gitea webhook to trigger Jenkins on pull request events:
Enable Pull request events:
5.1 Create a Pull Request
Open a new PR against main
:
5.2 Observe Jenkins Pipeline Runs
Jenkins will build the PR branch and run the image update:
View the pipeline activity dashboard:
6. Confirm Image Tag Update
Check console logs:
git clone -b main http://64.227.187.25:5555/dasher-org/solar-system-gitops-argo-cd
git checkout -b feature-1
sed -i "... deployment.yml"
git commit -am "Update Docker image to f5c47d71240f57467b284288f1c452f81341b"
git push -u origin feature-1
Inspect the feature-1
branch in Gitea:
7. Sync with Argo CD
Since Argo CD tracks main
, it remains OutOfSync until you merge feature-1
:
Next, automate merging feature-1
into main
so Argo CD can deploy the updated manifest.
Links and References
Thank you for following this GitOps workflow!
Watch Video
Watch video content