Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub - jobs : what is : use actions/checkout

I saw a lot of uses of :

jobs:
  myjob:
    steps:
      - name: checkout
        uses: "actions/checkout@something"
      - ...

But i can not find what is the purpose of the line :

uses : "actions/checkout@something"

Is it similar to this ?

 run: git checkout something   
like image 253
Foxhunt Avatar asked Apr 16 '21 19:04

Foxhunt


People also ask

What does GitHub Actions checkout do?

This action checks-out your repository under $GITHUB_WORKSPACE , so your workflow can access it. 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.

What does GitHub checkout mean?

Checkout is the command used to switch between the different branches of a GitHub repository. When a branch is checked out, all files in the working directory are updated to match the versions stored in that branch.

What are actions in GitHub Actions?

Actions are individual tasks that you can combine to create jobs and customize your workflow. You can create your own actions, or use and customize actions shared by the GitHub community.


2 Answers

For this line: uses : "actions/checkout@something", it will use the actions/checkout github action (source here) with the ref something. This ref only refers to the github action version (nothing to do with your repo)

The uses statement refers to a github action that is being used in this step. From github documentation for jobs.<job_id>.steps[*].uses:

Selects an action to run as part of a step in your job. An action is a reusable unit of code. You can use an action defined in the same repository as the workflow, a public repository, or in a published Docker container image.

From actions/checkout readme :

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

By default it checks out only one commit. My understanding is that it's doing something similar to:

git fetch --depth 1 origin $GITHUB_REF

This action also persists an auth token in git config. This way, your workflow can run authenticated git commands

By default, it clones your current repository ({{ github.repository }}) but you can also use this action to clone a different repository, and specify additionnal parameters like token, branch, path etc...

An example with additionnal input parameters: check out all git history by setting fetch-depth to 0 (default is 1), see usage doc:

- uses: actions/checkout@v2
  with:
    fetch-depth: 0
like image 139
Bertrand Martel Avatar answered Oct 17 '22 06:10

Bertrand Martel


Understanding terminologies made things clearer

  • Remote repo - It can also be referred to as the origin
  • Origin - the default name of the remote repo or the source repo being cloned
  • Head - a reference to human-friendly names for branches
  • git checkout - switch to a particular branch and displaying the changes currently on that branch
  • origin/name_of_branch - branch name created when fetching changes from a particular branch on the remote repo

Side Note: When git fetch is used, a custom branch is created locally in the form "origin/name_of_branch", changes on this branch can be viewed locally. These changes are the updated version of the files, not the specific change in that file as seen when commits are being inspected on GitHub.

Back to the question When the action is executed

jobs:
  myjob:
    steps:
      - name: checkout
        uses: "actions/checkout@something"
      - ...

The default steps being executed are:

  1. The current repo in which the workflow is being triggered gets cloned.

  2. Depending on the defined events such as a push or pull request:

  • For a push event, it runs the command below, where $GITHUB_REF points to the latest commit on the specified branch for the push event in the workflow.
git fetch --depth 1 $GITHUB_REF
  • For pull requests, it checks $GITHUB_REF points to the latest commit on the pull request source branch. This means it points to the would-be code/result from merging the pull request. This is the code/result other steps within the job are executed on such as running builds or tests. (Not completely sure of the command which runs under the hood)

Environment variables being referenced in the commands are explained here. Additional options can be added to implement specific processes or scenarios such as checking out a different branch. This can be found in the official repo readme.

like image 7
Abubakar Avatar answered Oct 17 '22 05:10

Abubakar