Pedro Delfino
Pedro Delfino

Reputation: 2681

How to create a GitHub action to checkout a specific commit in a private repository?

I have been using this documentation called Checkout Actions to build a Continuous Integration workflow using GitHub Actions. In general, it works when dealing with public and private repositories.

This is the template:

      - name: Checkout my_organization/my_private_repository
        uses: actions/checkout@v3
        with:
          repository: my_organization/my_private_repository
          ref: main
          path: my_private_repository
          token: ${{ secrets.MY_PRIVATE_REPOSITORY_SECRET_ACTIONS }}

      - name: lein install my_private_repository
        run:
          cd my_private_repository && git checkout 60cfa20 && lein install && cd ..

I need almost the snippet above. The only thing missing is that I would like to check out a specific commit on main branch. The commit ID is 60cfa20.

I tried inserting as code to-be run after the cd to the repository. Unfortunately, it did not work. See below:

      - name: Checkout my_organization/my_private_repository
        uses: actions/checkout@v3
        with:
          repository: my_organization/my_private_repository
          ref: main
          path: my_private_repository
          token: ${{ secrets.MY_PRIVATE_REPOSITORY_SECRET_ACTIONS }}

      - name: lein install my_private_repository
        run:
          cd my_private_repository && git checkout 60cfa20 && lein install && cd ..

I also tried inserting the commit ID on ref:

      - name: Checkout my_organization/my_private_repository
        uses: actions/checkout@v3
        with:
          repository: my_organization/my_private_repository
          ref: main/60cfa20
          path: my_private_repository
          token: ${{ secrets. MY_REPOSITORY_SECRET_ACTIONS }}

      - name: lein install my_private_repository
        run:
          cd my_private_repository && lein install && cd ..

But, it did not work out.

How to fix this? How to check out a particular commit ID?

Upvotes: 3

Views: 7252

Answers (2)

VonC
VonC

Reputation: 1324148

In addition of fetch depth:0, I would recommend a combination of git clean + git restore (instead of git checkout, which is obsolete since Git 2.23)

- name: lein install my_private_repository
    run: |
      cd my_private_repository
      git clean -xdf
      git restore -SW 60cfa20
      lein install
      cd ..

Upvotes: 1

Pedro Delfino
Pedro Delfino

Reputation: 2681

Apparently, the problem was that I was fetching only the latest commit id.

This is the default behavior as indicated by the documentation:

Only a single commit is fetched by default, for the ref/SHA that triggered the workflow. Set fetch-depth: 0 to fetch all history for all branches and tags. Refer here to learn which commit $GITHUB_SHA points to for different events.

To fix it, I inserted fetch-depth: 0 and git checkout 60cfa20:

      - name: Checkout my_organization/my_private_repository
            uses: actions/checkout@v3
            with:
              repository: my_organization/my_private_repository
              ref: main/60cfa20
              path: my_private_repository
              token: ${{ secrets.MY_REPOSITORY_SECRET_ACTIONS }} 
              fetch-depth: 0
    
          - name: lein install my_private_repository
            run:
              cd my_private_repository && git checkout 60cfa20 && lein install && cd ..

Upvotes: 2

Related Questions