temp-action-1 #12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Save README on Branch Push | |
on: | |
push: | |
branches: | |
- '**' # 监听所有分支的推送事件 | |
jobs: | |
save-readme: | |
runs-on: self-hosted | |
steps: | |
- name: Checkout source branch (当前触发分支) | |
uses: actions/checkout@v2 | |
with: | |
path: source # 将代码检出到source目录 | |
- name: Get branch name | |
id: get-branch | |
run: | | |
BRANCH_NAME=$(echo "$GITHUB_REF" | sed 's/refs\/heads\///') | |
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV | |
- name: Prepare target branch (all-branch-info) | |
run: | | |
# 使用GitHub Token认证 | |
REPO_URL="https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY.git" | |
# 尝试克隆目标分支 | |
git clone -b all-branch-info --single-branch "$REPO_URL" target 2>/dev/null || CLONE_FAILED=true | |
if [ "$CLONE_FAILED" = "true" ]; then | |
echo "目标分支不存在,创建新分支" | |
mkdir -p target | |
cd target | |
git init | |
git remote add origin "$REPO_URL" | |
git checkout -b all-branch-info | |
touch .gitkeep # 创建空文件用于初始提交 | |
git add .gitkeep | |
git commit -m "初始化all-branch-info分支" | |
git push origin all-branch-info | |
fi | |
- name: Copy README to target | |
run: | | |
# 确保目标目录存在 | |
mkdir -p "target/all-branch-info/$BRANCH_NAME" | |
if [ -f "source/README.md" ]; then | |
# 复制README并保留目录结构 | |
cp "source/README.md" "target/all-branch-info/$BRANCH_NAME/README.md" | |
echo "成功复制README.md到目标分支" | |
else | |
echo "错误:源分支不存在README.md" | |
exit 1 | |
fi | |
- name: Commit and push changes | |
run: | | |
cd target | |
git config user.name "GitHub Actions" | |
git config user.email "[email protected]" | |
# 添加所有更改 | |
git add --all | |
# 检测是否有更改需要提交 | |
if git diff-index --quiet HEAD --; then | |
echo "没有更改需要提交" | |
else | |
git commit -m "更新分支 [$BRANCH_NAME] 的README.md" | |
git push origin all-branch-info | |
echo "更改已推送到all-branch-info分支" | |
fi |