简体   繁体   中英

Change directory in Jenkins using shell script in YML pipeline

I have written a YML Pipeline in Jenkins. I cloned code using ssh from BitBucket. The code directory is inside the jenkins_pipeline directory. I have to run a few sh commands using the YML script. I want to change the directory at the start of the script to the code directory so that I no longer need to change it in every sh command. I am not getting a way to do this. Here is my YML script. I have tried using this method but it always says script.sh file not found which is there in the directory.
YML script

    pipeline:
  environment:
  agent:
    any:
  stages:
    - stage: Pull Code and Merge master in UAT
      steps:
        script:
          - sh 'git clone ssh://url-to-repo'            
          - sh 'source script.sh'     #it gives the error
          - sh 'pwd'
          - sh 'git checkout release/uat'
          - sh "git pull "
          - sh 'git status'
          - sh 'git merge -m "merging changes from main to UAT" origin/master'
          - sh 'git push'
    - stage: Content Replace
      steps:
        script:
          - withEnv: "['FIND_SER_NAME=dev','REPLACE_SER_NAME=uat']"
            script:
              - "contentReplace(configs: [fileContentReplaceConfig(configs: [fileContentReplaceItemConfig(matchCount: 0, replace: env.REPLACE_SER_NAME, search: env.FIND_SER_NAME)], fileEncoding: 'UTF-8', filePath: '/var/jenkins_home/workspace/UAT_YamlPipeline_Ceye_Service/deploy.sh')])"   
    - stage: Publish Changes to uat branch
      steps:
        script:
          - sh 'git status'
          - sh 'git add --all'
          - sh 'git commit -m "[CYB-128]changed the IPs for UAT branch for build"'
          - sh "git push  --all"
 

this is my currently working script

    pipeline:
  environment:
  agent:
    any:
  stages:
    - stage: Pull Code and Merge master in UAT
      steps:
        script:
          - sh ' cd ceye-service && git checkout release/uat'
          - sh " cd ceye-service && git pull "
          - sh ' cd ceye-service && git status'
          - sh ' cd ceye-service && git merge -m "merging changes from main to UAT" origin/master'
          - sh ' cd ceye-service && git push'
    - stage: Content Replace
      steps:
        script:
          - withEnv: "['FIND_SER_NAME=dev','REPLACE_SER_NAME=uat']"
            script:
              - "contentReplace(configs: [fileContentReplaceConfig(configs: [fileContentReplaceItemConfig(matchCount: 0, replace: env.REPLACE_SER_NAME, search: env.FIND_SER_NAME)], fileEncoding: 'UTF-8', filePath: '/var/jenkins_home/workspace/uat-ceye_service_code_change/ceye-service/deploy.sh')])"   
    - stage: Publish Changes to uat branch
      steps:
        script:
          - sh ' cd ceye-service && git status'
          - sh 'cd ceye-service && git add --all'
          - sh 'cd ceye-service && git commit -m "[CYB-128]changed the IPs for UAT branch for build"'
          - sh "cd ceye-service && git push --all"
 

I don't want to us cd in every command. Is there a way I could switch directory in start and it stay until the script ends? Thanks a lot

Markup, as comment doesn't have proper indentation: can you try this?

pipeline:
  environment:
  agent:
    any:
  stages:
    - stage: Pull Code and Merge master in UAT
      steps:
        script:
          - dir: "'ceye-service'"
            script:
              - sh 'git checkout release/uat'
              - sh "git pull"
              - sh 'git status'
              - sh 'git merge -m "merging changes from main to UAT" origin/master'
              - sh 'git push'

Documentation https://plugins.jenkins.io/pipeline-as-yaml/ specific the part of Special Steps With Code Blocks describes this.

Some steps has their own code blocks. For example: 'withAnt, withEnv, withCredentials, dir' or any other custom step definition which has it's own code block. This kind of steps also can be defined as YAML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM