Clone a GitHub Repository
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
- A GitHub repository is required for this tutorial. Click here to learn how to create a GitHub repository.
- A previously generated RSA public/private keypair is required for this tutorial. This key pair will be used to authenticate with GitHub. Click here to learn how to create an SSH keypair.
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”.
![](/docs/tutorials/github/git-repository_hu5586a4e47386edb7b23d31ee2244c75c_161418_0x726_resize_catmullrom_3.png)
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:
- Log in to Github.
- Go to the properties of your GitHub account.
- Click “Settings”.
- On the left navigation pane, click “SSH and GPG keys”.
- Give the new SSH key a title (e.g. Sophos Factory Public Key).
- In the Key field, paste the public key.
- Click “Add SSH Key”.
![](/docs/tutorials/github/github-settings_hub169c76d050e46581c9265ae3ebb2306_272891_0x726_resize_catmullrom_3.png)
Step 2: Add an SSH Private Key Credential
- Log in to Sophos Factory and navigate to the “Credentials” page.
- Click “New Credential”.
- Click “Add Credential”.
- In the Create Credential dialog box, give the credential a name (“GitHub SSH Key”).
- Define a Credential ID (e.g. “github_sshkey”).
- For Credential Type, choose “Built In: SSH Private Key” from the drop-down menu.
- In the SSH Private Key box, paste the private key of your key pair.
- Click “Create”.
The new SSH Private Key Credential will appear in the credential list:
![](/docs/tutorials/github/new-credential_hu6b3c31869c83564d237b67f180f71b45_67018_0x300_resize_catmullrom_3.png)
Step 3: Create a New Pipeline
- Navigate to the “Pipelines” page.
- Click the “New Pipeline” button.
- Enter a friendly name for the tutorial pipeline.
- Click the “Create” button.
- You should now see the pipeline builder page.
![](/docs/tutorials/github/new-pipeline_hud2be5be2d4f6c96a24bdb8d3d06ff884_20229_0x450_resize_catmullrom_3.png)
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.
![](/docs/tutorials/github/pipeline-builder_hu4c550aff1a68d799a61afb13b7dd4e26_116351_0x450_resize_catmullrom_3.png)
Configure Git step properties
- Click on the Git step to view the step properties.
- Enter a Display Name for the Step ID (e.g. “Git”).
- Enter a Display Name (e.g. “Git Repository”).
- In the dropdown, choose the Credential that was created in Step 2. In this tutorial, it was “GitHub SSH Key”.
- Enter the GitHub Repository URL. The URL should be in SSH format, e.g. “git@github.com:refactr-demo/git-tutorial.git”.
- Enter the Destination Path where the cloned repository data will reside (e.g. “git-tutorials”)
- 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
![](/docs/tutorials/github/git-module-steps_hua2d0f0af2b3e2c74c6d6a0602f70d16a_213138_0x728_resize_catmullrom_3.png)
Configure Shell Script step properties
Now we’ll configure the Shell Script step, which will display the contents of the clone directory.
- Click on the Shell Script step to view the step properties.
- Enter a short Step ID (e.g. “gitshell1”).
- Enter a Display Name (e.g. “Git ls Script”).
- Enter a Working Directory that matches the Destination Path entered in the previous section (e.g., “git-tutorials”).
- Click “Save”.
![](/docs/tutorials/github/shell-properties_hu673b2140da39cd4f1a5766013803f47c_46466_0x728_resize_catmullrom_3.png)
Save the pipeline
- Click “Save” in the upper left-hand corner.
- Add a short comment about the changes you made.
- Click the blue “Save” button below the comment box.
![](/docs/tutorials/github/save-pipeline_hubc99480bdc5b50a2f35b6ebcd3f9d709_812085_0x728_resize_catmullrom_3.png)
Step 5: Run the Pipeline and Observe Output
- In the pipeline builder, click “Run”.
- In the “Run Pipeline” dialog box, accept the defaults and click “Run”. You should be redirected to the run page.
![](/docs/tutorials/github/run-pipeline_hu401b1662208889bd8cbcb5b941b464a0_169091_0x728_resize_catmullrom_3.png)
![](/docs/tutorials/github/repo-results_hud0130bc709dc5819dee925bd32f7140c_425145_0x728_resize_catmullrom_3.png)
Note that the Git and Shell Script Step Modules is complete and the content of the cloned GitHub repository is listed successfully.