sh 'alias' is not giving any output in Jenkinsfile

Viewed 1819

I have a docker container which runs Jenkins.

Inside Jenkinsfile, I try to define an alias and print this alias.

I tried it manually, connecting to Jenkins container, and I am able to do:

alias foo='bar'

and then, if I execute alias I can see the list of alias ( I have 7 preset alias, and the new one )

but when I execute the same thing in Jenkinsfile, I foo command won't respond...

Here is my pipeline code:

#!/bin/groovy

pipeline {
agent any
stages {
  stage("Use alias command"){
    steps { 
        sh 'alias foo="bar"'
        sh 'foo'
    }
  }
}}

Any idea why ?

1 Answers

Each sh command runs in it's own shell. It's in the same agent/workspace, but since it's a new shell, environment variables, aliases, etc, will be lost. You need to combine the lines into a single sh:

#!/bin/groovy

pipeline {
agent any
stages {
  stage("Use alias command"){
    steps { 
        sh '''
          alias foo="bar"
          foo
        '''
    }
  }
}}
Related