-1

I've previously used GitLab, where I didn't checkout the repository explicitly. Instead it was done automatically by the pipeline.

Thus, I was surprised that almost all GitHub Actions workflows use e.g. actions/checkout@v3.

But what's the purpose besides checking out a different repository?

As seen from the below screenshot, my repository is already checked out, when the job starts:

enter image description here

Shuzheng
  • 11,288
  • 20
  • 88
  • 186

1 Answers1

1

By default, this action will check-out to the SHA for that workflow’s event (such as push and pull_request). Otherwise, uses the default branch (usually main or master in a standard repository).

You should use the checkout action any time your workflow will use the repository's code.

https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions#understanding-the-workflow-file

This action checks-out your repository under $GITHUB_WORKSPACE, so your workflow can access it.

https://github.com/actions/checkout

So you can specify the repo, branch, path, submodule and so on to which you can checkout. Also you can use fetch-depth to include git history as necessary.

Also note, that it's cloning the files to $GITHUB_WORKSPACE.

Robin Thomas
  • 1,317
  • 8
  • 15
  • Thank you @RobinThomas. By **Otherwise, uses the default branch (usually main or master in a standard repository).** - you mean that GitHub's default behaviour is to clone the default branch? So, what I'm seeing in the screen is actually the tip of the `master` branch? Could you elaborate on `$GITHUB_WORKSPACE`? Is this different from where GitHub normally clones the repository (in my case: `/__w/lddprobff/lddprobff`) – Shuzheng Aug 20 '23 at 10:08
  • There is no defined location for `$GITHUB_WORKSPACE`, since thats set by the GitHub runner. But looks like `/__w/lddprobff/lddprobff` is the `GITHUB_WORKSPACE` in this case. – Robin Thomas Aug 20 '23 at 10:20
  • If you have a PR that triggers a workflow, then the default branch will be the branch of that PR, and not the default branch. – Robin Thomas Aug 20 '23 at 10:22
  • I think you mean the PR source branch. But, what you're saying is that GitHub Actions by default clones the default branch, except when triggered by a PR? – Shuzheng Aug 20 '23 at 10:25
  • Yes, PR source branch. – Robin Thomas Aug 20 '23 at 10:27
  • No, `actions/checkout` is an official action from GitHub to clone the repo to the workspace of the Actions runner. That action will use the PR source branch /commit (if available) or use the default branch. – Robin Thomas Aug 20 '23 at 10:28
  • @Shuzheng Does that clarify everything? If yes, can you accept my answer? Thanks :) – Robin Thomas Aug 20 '23 at 14:18
  • I will accept it, but what's the default checkout behavior, when not using the `actions/checkout`? I observe my repository already checked out, when the workflow is executed. When can I avoid using the action? – Shuzheng Aug 21 '23 at 07:10
  • Why does `actions/checout` checkout the default branch, if running a workflow for a different branch without an associated PR? – Shuzheng Aug 21 '23 at 07:12
  • It wont. It will use the branch/tag that triggered the workflow. – Robin Thomas Aug 21 '23 at 07:16
  • Nice, but you don't know the clone behavior, when not using `actions/checkout`? – Shuzheng Aug 21 '23 at 12:44
  • No Github recommends to use `actions/checkout` if we need to use our repo in our workflow. So not exactly sure about this clone behaviour. Maybe github could change this behaviour in the future though. – Robin Thomas Aug 21 '23 at 12:46