Azure DevOps Git Repo 和 GitHub Repo 之间同步

问题描述 投票:0回答:1

我已将 GitHub 存储库导入到 Azure DevOps 中。但后来我对 Azure DevOps 代码进行了更改,还在 Azure DevOps 中创建了新的存储库和拉取请求,我希望使所有这些与 GitHub 同步。

此外,请提供输入以进行相同的同步,反之亦然。将最新代码从 github 获取到 Azure DevOps。

我尝试使用几个 YAML 代码构建管道来进行同步,即使代码成功运行后,我也无法迁移代码、新的存储库和拉取请求。请提供带有文档和示例代码的解决方案。

使用的Yaml代码1:

# Define the trigger for the pipeline, which specifies when it should run.
trigger:
  branches:
    include:
      - feature-*  # Trigger on changes in the develop branch

# Define the pool of agents that the pipeline should run on.
pool:
  vmImage: 'ubuntu-latest'  # Use an Ubuntu agent

# Define environment variables for Git credentials.
variables:
  GitHub_Repository_URL: 'https://username:[email protected]/githuburl'
  GIT_USERNAME: 'username'
  GIT_PASSWORD: 'token'

# Define the steps of the pipeline.
steps:
  # Step to checkout the code from the current repository.
- checkout: self  # Checkout the code from the current repository

  # Step to configure Git credentials.
- script: |
    git config --global credential.helper cache
    git config --global credential.helper 'cache --timeout=3600'
    git config --global user.name "$(GIT_USERNAME)"
    git config --global user.email "email"
    git remote add github $(GitHub_Repository_URL)  # Add GitHub repository as a remote
    git fetch github  # Fetch from GitHub
  displayName: 'Configure Git Credentials and Fetch Changes'  # Display name for this step

  # Step to synchronize code changes with GitHub for feature branches.
- script: |
    for branch in $(git branch -r --list "origin/feature-*"); do
      branch_name=$(echo $branch | cut -d'/' -f2)
      if ! git ls-remote --exit-code --heads github $branch_name; then
        git checkout -b $branch_name github/main  # Create a new local branch from GitHub main
        git push -u github $branch_name  # Push new branch to GitHub
      else
        git checkout -b $branch_name github/main  # Create a new local branch from GitHub main
        git pull origin $branch_name  # Pull changes from the branch in Azure DevOps
        git push github $branch_name  # Push changes to GitHub
      fi
    done
  displayName: 'Sync Changes with GitHub for Feature Branches'  # Display name for this step

  # Step to synchronize code changes with GitHub for new feature branches.
- script: |
    for branch in $(git branch -r --list "origin/feature-*"); do
      branch_name=$(echo $branch | cut -d'/' -f2)
      if ! git ls-remote --exit-code --heads github $branch_name; then
        git checkout -b $branch_name github/main  # Create a new local branch from GitHub main
        git push -u github $branch_name  # Push new branch to GitHub
      else
        git checkout -b $branch_name github/main  # Create a new local branch from GitHub main
        git pull origin $branch_name  # Pull changes from the branch in Azure DevOps
        git push github $branch_name  # Push changes to GitHub
      fi
    done
  displayName: 'Sync Changes with GitHub for New Feature Branches'  # Display name for this step

使用的Yaml代码2:

# Define the trigger (adjust branch patterns as needed)
trigger:
  branches:
    include:
      - feature-*

# Define the pool
pool:
  vmImage: 'ubuntu-latest'

# Securely access PAT using Azure DevOps secrets
variables:
  GitHub_Repository_URL: 'https://username:[email protected]/githuburl'
  GIT_USERNAME: 'username'
  GIT_PASSWORD: 'token'


# Define steps
steps:
- checkout: self

- script: |
    git config --global credential.helper cache
    git config --global credential.helper 'cache --timeout=3600'
    git config --global user.name "$(GIT_USERNAME)"  # Consider using a generic username (optional)
    git config --global user.email "email"  # Consider using a generic email (optional)
    git remote add github $(GitHub_Repository_URL)  # Add GitHub repository as a remote


- script: |
    for branch in $(git branch -r --list "origin/feature-*"); do
      branch_name=$(echo $branch | cut -d'/' -f2)

        # Check if branch exists on GitHub
        if ! git ls-remote --exit-code --heads github $branch_name; then
          git checkout -b $branch_name origin/$branch_name  # Checkout new branch from Azure DevOps
          git push -u github $branch_name  # Push new branch to GitHub
        else
          # Existing branch - existing logic for pulling/pushing
          git checkout $branch_name  # Directly checkout existing branch
          git pull origin $branch_name  # Pull changes from Azure DevOps
          git push github $branch_name  # Push changes to GitHub
        fi
      done
  displayName: 'Sync Code Changes with GitHub'

提前致谢。

github azure-devops version-control azure-pipelines azure-pipelines-yaml
1个回答
0
投票

要将 GitHub 存储库同步到 Azure 存储库,有两个建议。

  1. GitHub 存储库中的每次提交都会触发 DevOps 管道来同步存储库。您需要在 GitHub 存储库的每个分支下添加一个 yaml 文件。
pool:
  vmImage: ubuntu-latest
steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      git clone --mirror https://<GitHub PAT>@github.com/<GitHub org name>/<GitHub repo name>.git
      cd $(build.sourcesdirectory)/<GitHub repo name>.git
      git remote add origin1 https://$(System.AccessToken)@dev.azure.com/<DevOps org name>/<DevOps project name>/_git/<Azure repo name>
      git push -u origin1  --all
  env:
    AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)

  1. 使用管道的计划触发器来安排同步。您需要将以下 yaml 文件添加到 GitHub 存储库的主分支
schedules:
- cron: 0 0,6,12,18 * * *
  displayName: Trigger every 6 hours
  branches:
    include: 
    - main
 
  always: true 
 
trigger:
- none
 
pool:
  vmImage: ubuntu-latest
steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      git clone --mirror https://<GitHub PAT>@github.com/<GitHub org name>/<GitHub repo name>.git
      cd $(build.sourcesdirectory)/<GitHub repo name>.git
      git remote add origin1 https://$(System.AccessToken)@dev.azure.com/<DevOps org name>/<DevOps project name>/_git/<Azure repo name>
      git push -u origin1  --all
  env:
    AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)

DevOps 管道的源代码应该是您的 GitHub 存储库。

© www.soinside.com 2019 - 2024. All rights reserved.