创建三端同步
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
name: Sync to Gitee and GitCode
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- '**' # 所有分支推送时触发
|
||||
workflow_dispatch: # 允许手动执行
|
||||
|
||||
jobs:
|
||||
sync:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout source code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Setup Git user
|
||||
run: |
|
||||
git config --global user.name "GitHub Action"
|
||||
git config --global user.email "action@github.com"
|
||||
|
||||
- name: Extract repo and owner names
|
||||
run: |
|
||||
# 从 github.repository (格式: owner/repo) 中提取
|
||||
REPO_NAME="${GITHUB_REPOSITORY##*/}"
|
||||
OWNER_NAME="${GITHUB_REPOSITORY%%/*}"
|
||||
echo "REPO_NAME=$REPO_NAME" >> $GITHUB_ENV
|
||||
echo "OWNER_NAME=$OWNER_NAME" >> $GITHUB_ENV
|
||||
echo "当前仓库: $OWNER_NAME/$REPO_NAME"
|
||||
|
||||
# ==================== Gitee(自动创建公开仓库 + 推送)====================
|
||||
- name: Create Gitee public repository if not exists
|
||||
run: |
|
||||
echo "检查 Gitee 仓库是否存在..."
|
||||
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
|
||||
-H "Authorization: token ${{ secrets.GITEE_TOKEN }}" \
|
||||
"https://gitee.com/api/v5/repos/${{ env.OWNER_NAME }}/${{ env.REPO_NAME }}")
|
||||
|
||||
if [ "$HTTP_CODE" = "404" ]; then
|
||||
echo "仓库不存在,正在自动创建公开仓库..."
|
||||
curl -X POST "https://gitee.com/api/v5/user/repos" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: token ${{ secrets.GITEE_TOKEN }}" \
|
||||
-d "{
|
||||
\"name\": \"${{ env.REPO_NAME }}\",
|
||||
\"private\": false,
|
||||
\"auto_init\": false
|
||||
}"
|
||||
echo "✅ Gitee 公开仓库创建完成"
|
||||
elif [ "$HTTP_CODE" = "200" ]; then
|
||||
echo "✅ Gitee 仓库已存在,跳过创建"
|
||||
else
|
||||
echo "⚠️ Gitee API 返回状态码: $HTTP_CODE,继续尝试推送"
|
||||
fi
|
||||
|
||||
- name: Push to Gitee
|
||||
run: |
|
||||
git remote add gitee https://${{ env.OWNER_NAME }}:${{ secrets.GITEE_TOKEN }}@gitee.com/${{ env.OWNER_NAME }}/${{ env.REPO_NAME }}.git
|
||||
git push gitee --all --force
|
||||
git push gitee --tags --force
|
||||
|
||||
# ==================== GitCode(自动创建公开仓库 + 推送)====================
|
||||
- name: Create GitCode public repository if not exists
|
||||
run: |
|
||||
echo "检查 GitCode 仓库是否存在..."
|
||||
# 列出用户所有仓库,查找同名仓库
|
||||
REPOS_JSON=$(curl -s "https://api.gitcode.com/api/v5/user/repos?access_token=${{ secrets.GITCODE_TOKEN }}&per_page=100")
|
||||
EXISTS=$(echo "$REPOS_JSON" | grep -o "\"name\":\"${{ env.REPO_NAME }}\"" || true)
|
||||
|
||||
if [ -z "$EXISTS" ]; then
|
||||
echo "仓库不存在,正在自动创建公开仓库..."
|
||||
RESPONSE=$(curl -s -X POST "https://api.gitcode.com/api/v5/user/repos?access_token=${{ secrets.GITCODE_TOKEN }}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{
|
||||
\"name\": \"${{ env.REPO_NAME }}\",
|
||||
\"private\": false,
|
||||
\"auto_init\": false,
|
||||
\"repository_type\": \"code\"
|
||||
}")
|
||||
echo "API 响应: $RESPONSE"
|
||||
if echo "$RESPONSE" | grep -q '"id":'; then
|
||||
echo "✅ GitCode 公开仓库创建成功"
|
||||
else
|
||||
echo "❌ GitCode 仓库创建失败,请检查 Token 权限或手动创建"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo "✅ GitCode 仓库已存在,跳过创建"
|
||||
fi
|
||||
|
||||
- name: Push to GitCode
|
||||
run: |
|
||||
# 推送认证:用户名 git,密码为 Token
|
||||
git remote add gitcode https://git:${{ secrets.GITCODE_TOKEN }}@gitcode.com/${{ env.OWNER_NAME }}/${{ env.REPO_NAME }}.git
|
||||
git push gitcode --all --force
|
||||
git push gitcode --tags --force
|
||||
Reference in New Issue
Block a user