我有两个仓库,一个在Azure DevOps上,另一个在GitHub上。我想更新GitHub仓库,使其具有两个仓库中相同的代码。如何才能在不手动复制源代码的情况下完成此操作?
我有两个仓库,一个在Azure DevOps上,另一个在GitHub上。我想更新GitHub仓库,使其具有两个仓库中相同的代码。如何才能在不手动复制源代码的情况下完成此操作?
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')
(从 https://github.com/Azure/AzureStor/blob/master/azure-pipelines.yml 复制,稍作修改)
我该如何在不手动复制源代码的情况下完成它?
据我所知,Azure Devops Repos本身没有这样的功能来监视git存储库并自动保持这两个存储库同步。
但是,我认为有一些解决方法可以满足您的要求。您可以查看this blog了解有关如何在管道中使用git命令执行上述操作的详细信息。此外,专为将一个Git存储库与另一个存储库同步
而设计的免费Git Tools for Azure Devops扩展程序可能有助于您的情况。
此外:如果您喜欢这个同步功能,请随时在Azure Devops的User Voice forum中提交功能请求。产品团队会考虑您的想法,并在有任何更新时给出回复。
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:$AZUREPAT@dev.azure.com/$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
<b>Create a Github Actions yaml file and add this</b>
同时,将这些变量添加到你的 Github secrets 中
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
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
#!/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:$AZUREPAT@dev.azure.com/$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:$AZUREPAT@dev.azure.com/$AZORG/Brain%20Squeezes/_git/Brain-Squeezes.git