Clone a GitHub Repository

Create a pipeline that clones a private git repository hosted on GitHub.

GitHub is a popular Git repository hosting service. Sophos Factory pipelines can clone git repositories hosted on GitHub to the runner using the built-in “Git Clone” step module. This technique can be used to download existing automation content, such as scripts or configurations, which can be utilized by subsequent steps in a pipeline.

This tutorial will walk you through creating an example pipeline that clones a git repository from GitHub. We will then display the contents of the destination directory to verify that the files were downloaded successfully.

Prerequisites

Step 1: Choose a Repository and Configure a Public SSH Key

Log in to GitHub and choose a repository to use. In this tutorial, we will be using an example repository called “git-tutorial”.

Configure a public SSH key in your GitHub account

A public key from the RSA keypair needs to be added to GitHub. To do so:

  1. Log in to Github.
  2. Go to the properties of your GitHub account.
  3. Click “Settings”.
  4. On the left navigation pane, click “SSH and GPG keys”.
  5. Give the new SSH key a title (e.g. Sophos Factory Public Key).
  6. In the Key field, paste the public key.
  7. Click “Add SSH Key”.

Step 2: Add an SSH Private Key Credential

  1. Log in to Sophos Factory and navigate to the “Credentials” page.
  2. Click “New Credential”.
  3. Click “Add Credential”.
  4. In the Create Credential dialog box, give the credential a name (“GitHub SSH Key”).
  5. Define a Credential ID (e.g. “github_sshkey”).
  6. For Credential Type, choose “Built In: SSH Private Key” from the drop-down menu.
  7. In the SSH Private Key box, paste the private key of your key pair.
  8. Click “Create”.

The new SSH Private Key Credential will appear in the credential list:

Step 3: Create a New Pipeline

  1. Navigate to the “Pipelines” page.
  2. Click the “New Pipeline” button.
  3. Enter a friendly name for the tutorial pipeline.
  4. Click the “Create” button.
  5. You should now see the pipeline builder page.

Step 4: Add Git and Shell Steps to the Pipeline

This pipeline will consist of two step modules: Git and Shell Script. In the pipeline builder, drag the Git and Shell Script step modules into the design area. Connect the two step modules with an arrow.

Configure Git step properties

  1. Click on the Git step to view the step properties.
  2. Enter a Display Name for the Step ID (e.g. “Git”).
  3. Enter a Display Name (e.g. “Git Repository”).
  4. In the dropdown, choose the Credential that was created in Step 2. In this tutorial, it was “GitHub SSH Key”.
  5. Enter the GitHub Repository URL. The URL should be in SSH format, e.g. “git@github.com:refactr-demo/git-tutorial.git”.
  6. Enter the Destination Path where the cloned repository data will reside (e.g. “git-tutorials”)
  7. Click “Save”.

The GitHub repository location and link can be easily copied from your GitHub account. For instructions on how to do this, please visit: How to copy a GitHub repository location

Configure Shell Script step properties

Now we’ll configure the Shell Script step, which will display the contents of the clone directory.

  1. Click on the Shell Script step to view the step properties.
  2. Enter a short Step ID (e.g. “gitshell1”).
  3. Enter a Display Name (e.g. “Git ls Script”).
  4. Enter a Working Directory that matches the Destination Path entered in the previous section (e.g., “git-tutorials”).
  5. Click “Save”.

Save the pipeline

  1. Click “Save” in the upper left-hand corner.
  2. Add a short comment about the changes you made.
  3. Click the blue “Save” button below the comment box.

Step 5: Run the Pipeline and Observe Output

  1. In the pipeline builder, click “Run”.
  2. In the “Run Pipeline” dialog box, accept the defaults and click “Run”. You should be redirected to the run page.

Note that the Git and Shell Script Step Modules is complete and the content of the cloned GitHub repository is listed successfully.