I have created a Jenkins Job Free Style, through script i am preparing build and uploading that build as an image on docker hub using docker commands as following
docker --version
docker login -u 'docker_USer' -p 'docker_password'
echo "Login done"
echo "Building image..."
docker build -f python-jenkins/Dockerfile -t py-sample-image .
echo "Docker image is built"
echo "Running docker image..."
docker run py-sample-image
echo "images"
docker images
echo "Tagging local image..."
docker tag py-sample-image:latest dockerrepo/jenkins-poc:${BUILD_NUMBER}
echo "Publishing image to docker hub..."
docker push dockerrepo/jenkins-poc:${BUILD_NUMBER}
echo "Docker image is published"
Additionally i have created a declarative pipeline where i am doing same using sh command, declarative pipeline is as follows
pipeline {
agent any
tools {
// Reference the Docker installation configured in Jenkins Global Tool Configuration
dockerTool 'dockerx'
}
stages {
stage ('Git checkout') {
steps {
script {
git branch: 'main', credentialsId: 'github_pass', url: 'https://github.com/levelops/GHA-Test'
echo 'checkout jenkins-rnd repo'
}
}
}
stage ('Login to Docker Image') {
steps {
sh "docker login -u ${DOCKER_USER} -p ${DOCKER_PASS}"
echo 'Logged into docker successfully'
}
}
stage ('Build Docker Image') {
steps {
echo 'Building docker image...'
sh "docker build -f python-jenkins/Dockerfile -t py-sample-image-dp ."
echo 'Docker image is built'
}
}
stage ('Run Docker Image') {
steps {
echo 'Running docker image...'
sh "docker run py-sample-image-dp"
}
}
stage ('Tag Docker Image') {
steps {
echo 'Tagging docker image...'
sh "docker tag py-sample-image-dp:latest ${DOCKER_USER}/jenkins-poc-declarative-pipeline:${BUILD_NUMBER}"
echo 'Provided tag to docker image'
}
}
stage ('Publish Docker image') {
steps {
echo 'Publishing docker image to hub...'
sh "docker push ${DOCKER_USER}/jenkins-poc-declarative-pipeline:${BUILD_NUMBER}"
echo 'Docker image is successfully published to docker hub'
}
}
}
}
Please note:
here build procedure can be different ie. artifact can be war, zip, xml, txt, image etc. anything
Similarly Artifactory can be jfrog, dockerhub, nexus, some s3 etc.
Question
Is there any way i can get generated Artifact details like Generated Artifact Name, Location etc through any plugin/jenkins API/SDK?