How do i checkout a single folder / file from a remote repo using github actions

2.2k Views Asked by At

I'm using github actions runner on windows.

I tried the below to checkout only the scripts folder from remote repo called betech from branch mm365 inside the current-folder\betech; but it checks out the entire repo contents.

  - name: Checkout just the scripts folder
    uses: actions/checkout@v3
    with:
      repository: mytech/betech
      ref: mm365
      path: betech
      token: ${{ secrets.MYGITHUBTOKEN }}

  - name: Configure Sparse Checkout   
    run: |
      echo "scripts/*" >> .git/info.sparse-checkout
      git read-tree -mu HEAD
    working-directory: betech

    env:
      token: ${{ secrets.MYGITHUBTOKEN }}
      secrets: inherit 

here is the sparse file

cat betech\.git\info.sparse-checkout

scripts/*

Can you please suggest how I can checkout only the scripts folder or any single file [If the logic is comparatively different]

*** Update: *** I tied the below answer and it works.

  - name: Checkout just the scripts folder
    uses: actions/checkout@v3
    with:
      repository: mytech/betech
      ref: mm365
      path: betech
      sparse-checkout: |
        scripts
      sparse-checkout-cone-mode: false
      token: ${{ secrets.MYGITHUBTOKEN }}
1

There are 1 best solutions below

1
user459872 On BEST ANSWER

The checkout action documentation have few examples demonstrating how to fetch specific files.

Fetch only the root files

- uses: actions/checkout@v3   
  with:
    sparse-checkout: . 

Fetch only the root files and .github and src folder

- uses: actions/checkout@v3
  with:
    sparse-checkout: |
      .github
      src

Fetch only a single file

- uses: actions/checkout@v3
  with:
    sparse-checkout: |
      README.md
    sparse-checkout-cone-mode: false

You can also use checkout-files action to checkout only certain files and/or folders. This action uses Github REST API to download the repository content.

- name: Checkout files
  uses: Bhacaz/checkout-files@v2
  with:
   files: package.json

Here the files is a list of files with the path separated by a space, relative to root of your repository. This can also be a folder and the action will recursively pull all the files.