2018-08-07

Jenkins pipeline calls cmd command

When you need to call cmd command from Jenkins pipeline script you can do it in following way. I am just storing that piece of code here.
steps
  {  
   script
    {
     env.GitCurrentHash=bat(returnStdout: true, script: "@echo off | git --git-dir=${WORKSPACE}\\.git rev-parse origin/${branch}").trim()
    }
  
echo "Current Git Hash: ${GitCurrentHash}"
   echo "Current Git Hash: ${GIT_COMMIT}"
  }
 Sub-command "@echo off |" is there to eliminate repetition of command to system output console, so we see only command result.

If you want to collect git hash from file level use construction:
env.gitcurrent=bat(returnStdout: true, script: "@echo off | git --git-dir=${WORKSPACE}\\.git rev-parse HEAD 2> nul || echo githash").trim()

If you want to know branch name from file level use construction:
env.gitbranch=bat(returnStdout: true, script: "@echo off | git --git-dir=${WORKSPACE}\\.git name-rev --name-only HEAD 2> nul || echo gitbranch").trim()

 That's all... 

Žádné komentáře :

Okomentovat

Dotaz, připomínka, oprava?
(pokud máte problém s vložením příspěvku, vyzkoušejte to v prohlížeči Chrome)