0

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
                '''
          }
        }
    }
1
  • 2
    Typically you would build a custom image based on node-alpine with git installed and then use that for the agent. Commented May 23 at 10:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.