I have a jenkins pipeline and I am running a Nodejs application which uses the docker image node:22.14.0-alpine3.21. I need to run git on the build and deploy stage but git is not available in the docker image so i installed it on each stage that needs it and also authenticate to the git repo on every stage. Is there a way to make this reusable and maintainable so that i install git one time and do the authenticate for all stages without repeating the same steps in each stage i need it . Is there a global stage for git authentication that can be used for all ?
pipeline {
agent {
docker {
image 'node:22.14.0-alpine3.21'
}
}
environment {
GIT_LOGIN = credentials('git-user')
}
stages {
stage('Install') {
steps {
sh 'npm install'
}
}
stage('Run Unit Test') {
steps {
sh 'npm run test'
}
}
stage('Run Build') {
sh '''
apk add --no-cache git
git config --global credential.helper store
echo "http://${GIT_LOGIN_USR}:${GIT_LOGIN_PSW}@test.saubertest.com/healthapp.git" > ~/.git-credentials
git fetch --tags --force
npm run build
'''
}
}
stage('Deploy ZIP') {
steps {
sh '''
apk add --no-cache git
git config --global credential.helper store
echo "http://${GIT_LOGIN_USR}:${GIT_LOGIN_PSW}@test.saubertest.com/healthapp.git" > ~/.git-credentials
echo "Uploading file with node.js"
git fetch --tags --force
node execute.js
'''
}
}
}
node-alpinewithgitinstalled and then use that for the agent.