Answers: <p>You can check out specific files or folders from a repo using <code>git archive</code>.  I've done this from a Jenkinsfile using a shell command like so:</p>
  
  <pre><code>sh("git archive --remote=${repo_url} ${ref} ${path} | tar x")
  </code></pre>
  
  <p>You will, of course, need to set the variables <code>repo_url</code> (the url of the remote repository), <code>ref</code> (the ref/branch/commit/etc. to check out), and <code>path</code> (the path of the file or folder within the repo to check out).</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-02-19T17:43:00.647Z
Answers: 2020-02-19T17:43:00.647Z
Answers: jayhendren
Body: <p>Hey folks I need some help on the jenkinsfile. Below is my usecase</p>
  
  <p>This is the strcuture of my GIT repo:</p>
  
  <pre><code>root
    |-&gt;app1
    |   |-&gt;jenkinsfile
    |   |-&gt;dockerfile1
    |-&gt;app2
        |-&gt;jenkinsfile
        |-&gt;dockerfile2
  </code></pre>
  
  <p>I am having a monorepo, app1 and app2 in the root folder and when there is a change in app1 folder, only app1 should build and same for app2. I have defined the jenkinsfile in jenkins but when its build. its looking for dockerfile1 in root folder not inside app1.</p>
  
  <p>P.S: I can see some thread where people asking the same question but the answer is in detail. </p>
  
  <p>jenkinsfile:</p>
  
  <pre><code>pipeline {
      agent any
  
      environment {
          PIPENV_VENV_IN_PROJECT = true
          DEVPI_USER = '\'jenkins_user\''
          DEVPI_PASSWORD = '\'V$5_Z%Bf-:mJ\''
          WORKSPACE="${WORKSPACE}/app1"
  
      }
      stages {
          stage('Notify Bitbucket') {
              steps {
                  bitbucketStatusNotify(buildState: 'INPROGRESS')
              }
          }
  
          stage('Build Environment') {
              steps {
                  sh 'docker build -t app-builder .'
              }
          }
  
          stage('Test') {
  
              steps{
                  sh 'docker run --rm app-builder pytest'
              }
          }
  </code></pre>
  
Comments: CC BY-SA 4.0
Comments: 2020-02-18T15:03:24.317Z
Comments: It's hard to assist if we don't know what your Jenkinsfile is currently doing. Can you at least post the code block relevant to your question?
Comments: Argyle
Comments: CC BY-SA 4.0
Comments: 2020-02-18T15:09:58.653Z
Comments: @Argyle: I know this jenkinsfile will not work. but this is something I need to do. whenever there is a change is sub-folder. it should build only that subfolder's app.
Comments: gamechanger17
Tags: <jenkins><jenkins-pipeline><jenkinsfile>
Title: how to checkout only specific folder from git repo((monorepo)) and build
UserDisplayName: gamechanger17
ThreadText: # how to checkout only specific folder from git repo((monorepo)) and build
  
  (Asked by: username_0 on 2020-02-18)
  
  Hey folks I need some help on the jenkinsfile. Below is my usecase
  
  This is the strcuture of my GIT repo:
  
  ```
  root
    |->app1
    |   |->jenkinsfile
    |   |->dockerfile1
    |->app2
        |->jenkinsfile
        |->dockerfile2
  
  ```
  
  I am having a monorepo, app1 and app2 in the root folder and when there is a change in app1 folder, only app1 should build and same for app2. I have defined the jenkinsfile in jenkins but when its build. its looking for dockerfile1 in root folder not inside app1.
  
  P.S: I can see some thread where people asking the same question but the answer is in detail.
  
  jenkinsfile:
  
  ```
  pipeline {
      agent any
  
      environment {
          PIPENV_VENV_IN_PROJECT = true
          DEVPI_USER = '\'jenkins_user\''
          DEVPI_PASSWORD = '\'V$5_Z%Bf-:mJ\''
          WORKSPACE="${WORKSPACE}/app1"
  
      }
      stages {
          stage('Notify Bitbucket') {
              steps {
                  bitbucketStatusNotify(buildState: 'INPROGRESS')
              }
          }
  
          stage('Build Environment') {
              steps {
                  sh 'docker build -t app-builder .'
              }
          }
  
          stage('Test') {
  
              steps{
                  sh 'docker run --rm app-builder pytest'
              }
          }
  
  ```
  
  **Tags:** jenkins, jenkins-pipeline, jenkinsfile
  
  > **Comments:**
  > **username_1**: It's hard to assist if we don't know what your Jenkinsfile is currently doing. Can you at least post the code block relevant to your question?  
  > **username_0**: @username_1: I know this jenkinsfile will not work. but this is something I need to do. whenever there is a change is sub-folder. it should build only that subfolder's app.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2020-02-19. Score: 2
  
  You can check out specific files or folders from a repo using `git archive`. I've done this from a Jenkinsfile using a shell command like so:
  
  ```
  sh("git archive --remote=${repo_url} ${ref} ${path} | tar x")
  
  ```
  
  You will, of course, need to set the variables `repo_url` (the url of the remote repository), `ref` (the ref/branch/commit/etc. to check out), and `path` (the path of the file or folder within the repo to check out).
  
  
  ---
  
  