Skip to main content

Command Palette

Search for a command to run...

Creating an Azure DevOps Pipeline to Clone a GitHub Repository to an Azure DevOps Repository

Published
3 min read
Creating an Azure DevOps Pipeline to Clone a GitHub Repository to an Azure DevOps Repository
S

Microsoft MVP | Solution Architect Expert | Techno-Functional Consultant | MLSA Gold | Power Platform & Dynamics 365 Enthusiast

If you’re looking to clone a GitHub repository into an Azure DevOps repository and keep the two in sync, an Azure DevOps pipeline can handle this for you automatically. This pipeline will clone the code from a GitHub repository, set up the Azure DevOps repository as a remote, and push the code there. Here’s how to set up the pipeline.

Prerequisites

  1. Azure DevOps Organization: You’ll need an active Azure DevOps organization.

  2. GitHub Repository: The source repository you want to clone.

  3. Azure DevOps Repository: The destination repository where the code will be cloned.

  4. Permissions: Ensure you have push permissions on the Azure DevOps repository.

Step 1: Create and Configure the Pipeline YAML File

To set up the pipeline, add a new YAML file named azure-pipelines.yml in your Azure DevOps repository with the following content:

trigger:
  branches:
    include:
      - main

jobs:
- job: CloneAndPush
  pool:
    vmImage: 'ubuntu-latest'

  steps:
  - checkout: none  # Skip the default checkout

  - script: |
      echo "Cloning the GitHub repository..."
      git clone https://github.com/microsoft/coe-starter-kit.git  # Replace with your GitHub repo URL
      cd coe-starter-kit
      echo "Setting up remote for Azure DevOps repository..."
      git remote add devops https://$(System.AccessToken)@dev.azure.com/YourOrg/_git/YourRepo  # Replace with your DevOps repo URL

      echo "Configuring Git user..."
      git config user.email "your-email@example.com"  # Replace with your email
      git config user.name "Your Name"  # Replace with your name

      echo "Pushing to Azure DevOps repository..."
      git push devops main
    displayName: 'Clone and Push'
    env:
      GIT_ASKPASS: /bin/echo
      GIT_USERNAME: $(System.AccessToken)  # Use the system access token for authentication

Pipeline Breakdown

  • trigger: Runs the pipeline on changes to the main branch.

  • pool: Sets up the pipeline to run on an Ubuntu virtual machine.

  • checkout: none: Skips the default repository checkout, since we’ll be cloning the GitHub repo manually.

  • git clone: Clones the GitHub repository into the pipeline workspace.

  • git remote add: Adds the Azure DevOps repository as a remote named devops.

  • git config: Configures the Git user for committing to the Azure DevOps repository.

  • git push: Pushes the cloned GitHub code to the Azure DevOps repository.

Step 2: Add the Pipeline File to Your Repository

  1. Save the above YAML as azure-pipelines.yml in the root of your Azure DevOps repository.

  2. Commit and push the YAML file to your Azure DevOps repository.

Step 3: Set Up the Pipeline in Azure DevOps

  1. In Azure DevOps, go to Pipelines > New Pipeline.

  2. Select Azure Repos Git as your source and choose the repository where you committed azure-pipelines.yml.

  3. Azure DevOps will automatically detect the YAML file and prompt you to create the pipeline. Click Save and Run to start the process.

Step 4: Run and Monitor the Pipeline

  • Once the pipeline runs, it will clone the GitHub repository and push it to the Azure DevOps repository.

  • Check the Pipelines section in Azure DevOps for logs and status updates to ensure everything completed successfully.

This pipeline provides a simple way to clone a GitHub repository to Azure DevOps, making it easy to migrate or mirror repositories.

More from this blog

S

SHAHEER365 | Shaheer Ahmad | Solution Architect | Microsoft MVP

36 posts

Microsoft MVP | Solution Architect Expert | Techno-Functional Consultant | MLSA Beta | Power Platform & Dynamics 365 Enthusiast