riko1520
riko1520

Reputation: 91

How to synchronize Github and Azure DevOps repository?

I have two repository, first on Azure DevOps, second on GitHub. I'd like to update GitHub repository to have the same code on both repositories. How can I do it without manually copying source code?

Upvotes: 7

Views: 23614

Answers (5)

If your gitHub and azure devOps repositories are already created then,

step 1: (For credentials security)

Login GitHub -> Settings -> Code and automation -> Environments -> New Environment -> Give a meaningful name (I prefer 'develop') -> Click on configure environment -> Environment secrets -> Add secret ->

  1. Name (AZORG) and value (provide_organization_name_In_azure_devops)
  2. Name (AZUREPAT) and value (provide_personal_access_token_In_azure_devops)
  3. Name (AZUSERNAME) and value (provide_user_name_In_azure_devops)
  4. Name (AZUSER_EMAIL) and value (provide_your_organization_email_In_azure_devops)

step 2: (create workflow which is a trigger event in github pipeline)

Goto Actions tab -> new workflow -> on top right, choose 'set up a workflow yourself' -> in 'main.yml' ->

name: Push github directory to another azure devops repository
on:
  push:
    branches:
      - '*' # applies for all branches in github.
      - '!master' # This line is used to restrict this pipeline which won't triggers for master branch. remove this if you need to trigger for code push in master branch (Create PR if needed in devops repos)
jobs:
  check-bats-version:
    runs-on: ubuntu-latest
    environment: develop # mention that environment name that what we have created in step 1.
    steps:
    - uses: actions/checkout@v2
    - name: Run script file
      env:
        AZUREPAT: ${{secrets.AZUREPAT}} # calling secrets from environment variables
        AZUSERNAME: ${{secrets.AZUSERNAME}}
        AZUSER_EMAIL: ${{secrets.AZUSER_EMAIL}}
        AZORG: ${{secrets.AZORG}}
      run: |
         chmod +x ./commit.sh
         ./commit.sh # it will calls 'commit.sh' file. Thats where our logic is in.
      shell: bash
  

Step 3: (Main logic) Create 'commit.sh' file outside the .github directory ->

#!/bin/bash

AZUREPAT=$AZUREPAT
AZUSERNAME=$AZUSERNAME
AZUSER_EMAIL=$AZUSER_EMAIL
AZORG=$AZORG

# Remove Git information (for fresh git start)
rm -rf Brain-Squeezes/.git

# Fetch the changes from Azure DevOps to ensure we have latest
git fetch --unshallow

# Pull changes from Azure DevOps if its exiting branch and have commits on it
git pull https://$AZUSERNAME:[email protected]/$AZORG/Brain%20Squeezes/_git/Brain-Squeezes.git

#git checkout -b $github_to_azure_sync

# Set Git user identity
git config --global user.email "$AZUSER_EMAIL"
git config --global user.name "$AZUSERNAME"

# Add all changes into stage, commit, and push to Azure DevOps
git add .
git commit -m "Sync from GitHub to Azure DevOps"
git push --force https://$AZUSERNAME:[email protected]/$AZORG/Brain%20Squeezes/_git/Brain-Squeezes.git

Step 4: (Check the result) Add any file or modify the existing file in github repo, that changes will reflects in same branch in azure devops repository. If that branch doesn't exists in azure devops then that branch is automatically created with same branch name in github.

Upvotes: 0

Joshua Anop
Joshua Anop

Reputation: 21

<b>Create a Github Actions yaml file and add this</b>

Also add the variables to your github secretes

name: Sync From Git To Azure

on: 
  push:
    branches: [master]
    
jobs:
  sync_from_git_to_azure:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Setup environment variables
      run: |
        echo ::set-env name=AZUREPAT::${{ secrets.AZUREPAT }}
        echo ::set-env name=AZUSERNAME::${{ secrets.AZUSERNAME }}
        echo ::set-env name=AZUSER_EMAIL::${{ secrets.AZUSER_EMAIL }}
        echo ::set-env name=AZORG::${{ secrets.AZORG }}
    - name: Git clone repo
      run: |
        git clone https://link-to-your-repo
        cd reponame
        rm -rf .git
    - name: Clone to Azure DevOps Repo
      run: |
        cd ..
        GIT_CMD_REPOSITORY="https://azure-repo-url" 
        git clone $GIT_CMD_REPOSITORY
    - name: Copy files from git repro to azure repro
      run: |
        cp -r testAzureDevops/* TestGitSync/
    - name: Configure user in azure repro
      run: |
        cd TestGitSync
        git config --global user.email "$AZUSER_EMAIL"
        git config --global user.name "$AZUSERNAME"
    - name: Commit and Push to azure
      run: |
        git add .
        git commit -m "sync from git to azure"
        git push

Upvotes: 2

Harish Lalwani
Harish Lalwani

Reputation: 774

Since there was no solution to sync code from github to azure devops. Adding this piece to complete the answer. For pushing code from git hub repo to azure devops on every push, we can use github actions for this. Check the following code here.

https://github.com/harishlalwani/testAzureDevops.

Basically on push event github actions trigger commit.sh. And following code pushes code to Azure devops

AZUREPAT=$AZUREPAT
AZUSERNAME=$AZUSERNAME
AZUSER_EMAIL=$AZUSER_EMAIL
AZORG=$AZORG
git clone https://github.com/harishlalwani/testAzureDevops
cd testAzureDevops
rm -rf .git

cd ..
GIT_CMD_REPOSITORY="https://$AZUSERNAME:[email protected]/$AZORG/TestGitSync/_git/TestGitSync" 
 
git clone $GIT_CMD_REPOSITORY

cp -r testAzureDevops/* TestGitSync/

cd TestGitSync

git config --global user.email "$AZUSER_EMAIL"
git config --global user.name "$AZUSERNAME"

git add .
git commit -m "sync from git to azure"

git push

Upvotes: 2

LoLance
LoLance

Reputation: 28086

How can I do it without manually copying source code?

As I know Azure Devops Repos itself doesn't have such feature to monitor git repos and automatically keep these two repos in sync.

However I think there's some workarounds for your requirements. You can check this blog for the details about how to use git commands in pipeline to do what you expect above. Also, the free Git Tools for Azure Devops extension designed for synchronise one Git Repository with another may help for your scenario.

In addition: If you do like this sync feature, feel free to submit a feature request here in Azure Devops's User Voice forum. The product team would consider about your idea and gives reply if there's any update.

Upvotes: 5

Hong Ooi
Hong Ooi

Reputation: 57686

You can include a Pipeline step that runs git, to copy the repo over. Something like this:

steps:
- bash: |
    git push --prune https://$(GITHUB_PAT)@github.com/$REPO_NAME \
        +refs/remotes/origin/*:refs/heads/* +refs/tags/*:refs/tags/*
  displayName: 'Copy to Github'
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')

(Copied from https://github.com/Azure/AzureStor/blob/master/azure-pipelines.yml, slightly modified)

Upvotes: 9

Related Questions