Hi I am new to Devops and I am trying to create a simple Jenkins Pipleine. In which I need to push the docker image to dockerhub. Entire pipeline including Jenkins is hosted on Kubernetes. Below is the Jenkinsfile. I am giving the credentials inline which is bad. I have stored my dockerhub password in Jenkins as a secret text, but not sure how to reference it here.
pipeline {
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: docker
image: docker:latest
command:
- cat
tty: true
volumeMounts:
- mountPath: /var/run/docker.sock
name: docker-sock
volumes:
- name: docker-sock
hostPath:
path: /var/run/docker.sock
'''
}
}
stages {
stage('Clone') {
steps {
container('docker') {
git branch: 'main', changelog: false, poll: false, url: '<my github URL>'
}
}
}
stage('Build-Docker-Image') {
steps {
container('docker') {
sh 'docker build -t <my-dockerhub-username>/testing-image:latest .'
}
}
}
stage('Login-Into-Docker') {
steps {
container('docker') {
sh 'docker login -u <my-dockerhub-username> -p <my-dockerhub-password>'
}
}
}
stage('Push-Images-Docker-to-DockerHub') {
steps {
container('docker') {
sh 'docker push <my-dockerhub-username>/testing-image:latest'
}
}
}
stage('Remove docker Images') {
steps {
container('docker') {
sh 'docker image prune -af'
}
}
}
}
post {
always {
container('docker') {
sh 'docker logout'
}
}
}
}
I tried
stage('Push-Images-Docker-to-DockerHub') {
steps {
withCredentials([string(credentialsId: 'Docker_PW', variable: 'Docker_PW')])
container('docker') {
sh 'docker push <my-dockerhub-username>/testing-image:latest'
}
}
}
But my pipeline fails after coming to this step. Please assist if possible.