How to use source command within Jenkins pipeline script

23,640

Solution 1

Replace source environment.sh with

. ./environment.sh

Please note there is a space after first dot.

Solution 2

source is a bash/ksh/etc extension, provided as a more "substantial" synonym for ..

In sh, you need to use . in case the underlying shell is one (such as dash) that does not support the command source.

sh '''
    ...
    . ./environment.sh
    //Build
    //Test
    ...
'''

Solution 3

If someone want to execute the script with source only solution is to change "Shell executable" to bash in ->Manage Jenkins->Configure System

Share:
23,640

Related videos on Youtube

Neil
Author by

Neil

• 3+ years of IT Experience in Analysis, Design, Development, Testing, Deployment and Implementation of Multi-Tier distributed applications using Java, J2EE Technologies. • 1 year Erlang Development, analysis and testing • Good experience on net setting and Cisco routers setting • Good experience on DevOps tools, like Jenkins, Ansible. etc • Good understanding of design patterns and n-tier architecture. • Excellent Java development skills using J2EE, Servlets, JSON, JMS, JDBC, and Java Beans. • Strong front-end UI development skills using scripting languages like JSTL, HTML5, JavaScript, JQuery and CSS. • Experience on XML and parsing methodologies like DOM and SAX. • Experience in Service Oriented Architecture (SOA) and developing Web services using SOAP, REST, XML, WSDL, JAXP XML Beans. • Experience on security design and development using Asymmetric cryptograph, Kerberos Protocol, Spring Security • Extensive experience in design, development and implementation of Model-View-Controller frame works using Sprint boot, Struts and Spring MVC. • Extensive experience in development and implementation of ORM framework Hibernate/ Hibernate with Spring Data Access. • Strong database skills in DB2 and MySQL. • Expertise in programming with SQL, PL/SQL and Stored Procedures. • Experienced with Java Multithreaded programming to develop multithreaded modules and applications. • Expertise in designing applications using various J2EE design patterns like Singleton, Value Object, Data Access Object, Factory, Session Façade and Business Delegate Service Locator etc. • Expertise in using and configuring various web & application servers like GlassFish, Apache Tomcat, Jetty and JBoss. • Proficiency in programming with different Java IDE's like IntellJ, Eclipse and Netbeans. • Extensive experience in Linux, UNIX, Shell scripting. • Used log4J for application logging and notification tracing mechanisms. • Worked with various Version Control Tools including GitHub, Subversion, Tortoise SVN and. • Experience on NoSQL DataBase including MangoDB, ElasticSearch

Updated on July 09, 2022

Comments

  • Neil
    Neil almost 2 years

    I recently rewrite bash execution command into Jenkins pipeline. The old code is like

    ...
    source environment.sh
    //Build
    //Test
    ...
    

    Now I use pipeline script to wrap the command, like this

    sh '''
        ...
        source environment.sh
        //Build
        //Test
        ...
    '''
    

    However, I got an error, as.../.jenkins/script.sh: line 9: source: environment.sh: file not found. When I try to less environment.sh, it display correctly. So I suspect something wrong with source command within sh wrap.

    Before using pipeline, source environment.sh command is working fine in shell execution. So source is install at Jenkins server, it seems pipeline script don't know what is the source command.

    How could I do to run source command within sh wrapped block?

    • walsht
      walsht almost 8 years
      To debug use echo $pwd; ls .
  • Neil
    Neil almost 8 years
    I suppose triple quotes is the syntax in pipeline script, which identify multiple lines command. And I tried use . instead of source, however, no luck on that. Similar error throws /.jenkins/script.sh: line 9: .: environment.sh: file not found
  • chepner
    chepner almost 8 years
    Ah, missed that this wasn't just a shell script. (Although that should have been obvious, sorry.) It looks like the real problem is that Jenkins has a different working directory than your original script. Using an absolute path (instead of just environment.sh) should work.
  • Neil
    Neil almost 8 years
    Thank you, with absolute path, it works. Still, it's weird that linux cannot find the file
  • Neil
    Neil almost 8 years
    This one works. I don't need use absolute path anymore. Thank you
  • A. Gille
    A. Gille almost 3 years
    @Neil You have to put ' ./' in first: . ./environment.sh. It worked for me.
  • chepner
    chepner almost 3 years
    Fixed; I always forget that . uses path lookup if there is no / in the file name.