|
| 1 | +# GitHub-to-ADO Sync Pipeline |
| 2 | +# Syncs main branch from public GitHub to internal Azure DevOps daily at 5pm IST |
| 3 | + |
| 4 | +name: GitHub-Sync-$(Date:yyyyMMdd)$(Rev:.r) |
| 5 | + |
| 6 | +schedules: |
| 7 | + - cron: "30 11 * * *" |
| 8 | + displayName: "Daily sync at 5pm IST" |
| 9 | + branches: |
| 10 | + include: |
| 11 | + - main |
| 12 | + always: true |
| 13 | + |
| 14 | +trigger: none |
| 15 | +pr: none |
| 16 | + |
| 17 | +jobs: |
| 18 | +- job: SyncFromGitHub |
| 19 | + displayName: 'Sync main branch from GitHub' |
| 20 | + pool: |
| 21 | + vmImage: 'windows-latest' |
| 22 | + |
| 23 | + steps: |
| 24 | + - checkout: none |
| 25 | + |
| 26 | + - task: CmdLine@2 |
| 27 | + displayName: 'Clone GitHub repo' |
| 28 | + inputs: |
| 29 | + script: git clone https://github.com/microsoft/mssql-python.git repo-dir -b main |
| 30 | + workingDirectory: $(Agent.TempDirectory) |
| 31 | + |
| 32 | + - task: CmdLine@2 |
| 33 | + displayName: 'Add Azure DevOps remote' |
| 34 | + inputs: |
| 35 | + script: git remote add azdo-mirror https://$(System.AccessToken)@sqlclientdrivers.visualstudio.com/mssql-python/_git/mssql-python |
| 36 | + workingDirectory: $(Agent.TempDirectory)/repo-dir |
| 37 | + |
| 38 | + - task: CmdLine@2 |
| 39 | + displayName: 'Fetch ADO repo' |
| 40 | + inputs: |
| 41 | + script: git fetch azdo-mirror |
| 42 | + workingDirectory: $(Agent.TempDirectory)/repo-dir |
| 43 | + |
| 44 | + - task: CmdLine@2 |
| 45 | + displayName: 'Create timestamped sync branch' |
| 46 | + inputs: |
| 47 | + script: | |
| 48 | + echo Getting current timestamp... |
| 49 | + powershell -Command "Get-Date -Format 'yyyyMMdd-HHmmss'" > timestamp.txt |
| 50 | + set /p TIMESTAMP=<timestamp.txt |
| 51 | + set SYNC_BRANCH=github-sync-%TIMESTAMP% |
| 52 | + echo %SYNC_BRANCH% > branchname.txt |
| 53 | + echo Creating sync branch: %SYNC_BRANCH% |
| 54 | + git fetch azdo-mirror |
| 55 | + git show-ref --verify --quiet refs/remotes/azdo-mirror/main |
| 56 | + if %ERRORLEVEL% EQU 0 ( |
| 57 | + git checkout -b %SYNC_BRANCH% -t azdo-mirror/main |
| 58 | + ) else ( |
| 59 | + echo azdo-mirror/main does not exist. Exiting. |
| 60 | + exit /b 1 |
| 61 | + ) |
| 62 | + echo ##vso[task.setvariable variable=SYNC_BRANCH;isOutput=true]%SYNC_BRANCH% |
| 63 | + workingDirectory: $(Agent.TempDirectory)/repo-dir |
| 64 | + |
| 65 | + - task: CmdLine@2 |
| 66 | + displayName: 'Reset branch to match GitHub main exactly' |
| 67 | + inputs: |
| 68 | + script: | |
| 69 | + git -c user.email="sync@microsoft.com" -c user.name="ADO Sync Bot" reset --hard origin/main |
| 70 | + workingDirectory: $(Agent.TempDirectory)/repo-dir |
| 71 | + |
| 72 | + - task: CmdLine@2 |
| 73 | + displayName: 'Push branch to Azure DevOps' |
| 74 | + inputs: |
| 75 | + script: | |
| 76 | + set /p SYNC_BRANCH=<branchname.txt |
| 77 | + echo Pushing branch: %SYNC_BRANCH% |
| 78 | + |
| 79 | + git config user.email "sync@microsoft.com" |
| 80 | + git config user.name "ADO Sync Bot" |
| 81 | + |
| 82 | + git push azdo-mirror %SYNC_BRANCH% --set-upstream |
| 83 | + |
| 84 | + if %ERRORLEVEL% EQU 0 ( |
| 85 | + echo Branch pushed successfully! |
| 86 | + ) else ( |
| 87 | + echo ERROR: Push failed! |
| 88 | + exit /b 1 |
| 89 | + ) |
| 90 | + workingDirectory: $(Agent.TempDirectory)/repo-dir |
| 91 | + |
| 92 | + - task: CmdLine@2 |
| 93 | + displayName: 'Create pull request' |
| 94 | + inputs: |
| 95 | + script: | |
| 96 | + echo Installing Azure DevOps extension... |
| 97 | + call az extension add --name azure-devops --only-show-errors |
| 98 | + |
| 99 | + echo Setting up authentication... |
| 100 | + set AZURE_DEVOPS_EXT_PAT=$(System.AccessToken) |
| 101 | + |
| 102 | + echo Configuring Azure DevOps defaults... |
| 103 | + call az devops configure --defaults organization=https://sqlclientdrivers.visualstudio.com project=mssql-python |
| 104 | + |
| 105 | + set /p SYNC_BRANCH=<branchname.txt |
| 106 | + echo Creating PR from branch: %SYNC_BRANCH% to main |
| 107 | + |
| 108 | + call az repos pr create --source-branch %SYNC_BRANCH% --target-branch main --title "ADO-GitHub Sync - %date%" --description "Automated sync from GitHub main branch" --auto-complete true --squash true --delete-source-branch true --output table |
| 109 | + |
| 110 | + if %ERRORLEVEL% EQU 0 ( |
| 111 | + echo PR created successfully! |
| 112 | + echo Adding reviewers... |
| 113 | + call az repos pr list --source-branch %SYNC_BRANCH% --target-branch main --status active --output tsv --query "[0].pullRequestId" > pr_id.txt |
| 114 | + set /p PR_ID=<pr_id.txt |
| 115 | + call az repos pr reviewer add --id %PR_ID% --reviewers gargsaumya@microsoft.com sharmag@microsoft.com sumit.sarabhai@microsoft.com jathakkar@microsoft.com spaitandi@microsoft.com --output table |
| 116 | + if %ERRORLEVEL% EQU 0 ( |
| 117 | + echo Reviewers added successfully! |
| 118 | + ) |
| 119 | + del pr_id.txt |
| 120 | + ) else ( |
| 121 | + echo ERROR: Failed to create PR |
| 122 | + exit /b 1 |
| 123 | + ) |
| 124 | + |
| 125 | + if exist timestamp.txt del timestamp.txt |
| 126 | + if exist branchname.txt del branchname.txt |
| 127 | + workingDirectory: $(Agent.TempDirectory)/repo-dir |
0 commit comments