-
-
Notifications
You must be signed in to change notification settings - Fork 182
chore: 支持所有非 main 分支的 CI/CD 构建 #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,10 @@ | ||||||||||||||||||||||||||||||||||
| name: Dev Branch CI/CD | ||||||||||||||||||||||||||||||||||
| name: Non-Main Branch CI/CD | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| on: | ||||||||||||||||||||||||||||||||||
| push: | ||||||||||||||||||||||||||||||||||
| branches: | ||||||||||||||||||||||||||||||||||
| - dev | ||||||||||||||||||||||||||||||||||
| - '**' # 匹配所有分支 | ||||||||||||||||||||||||||||||||||
| - '!main' # 排除 main 分支 | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| permissions: | ||||||||||||||||||||||||||||||||||
| contents: write | ||||||||||||||||||||||||||||||||||
|
|
@@ -21,12 +22,37 @@ jobs: | |||||||||||||||||||||||||||||||||
| fetch-depth: 0 | ||||||||||||||||||||||||||||||||||
| token: ${{ secrets.GITHUB_TOKEN }} | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| - name: Get commit hash | ||||||||||||||||||||||||||||||||||
| id: commit | ||||||||||||||||||||||||||||||||||
| - name: Prepare branch info and tags | ||||||||||||||||||||||||||||||||||
| id: branch_info | ||||||||||||||||||||||||||||||||||
| run: | | ||||||||||||||||||||||||||||||||||
| # 获取分支名(去掉 refs/heads/ 前缀) | ||||||||||||||||||||||||||||||||||
| BRANCH_NAME="${GITHUB_REF#refs/heads/}" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # 处理分支名:转小写,替换 / 为 -,只保留字母数字和连字符 | ||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 建议添加边界情况处理 当前分支名处理逻辑缺少边界情况检查:
Suggested change
防止特殊分支名导致空 tag。 |
||||||||||||||||||||||||||||||||||
| SAFE_BRANCH_NAME=$(echo "$BRANCH_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g' | sed 's/--*/-/g' | sed 's/^-//;s/-$//') | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # 获取短 commit hash | ||||||||||||||||||||||||||||||||||
| SHORT_SHA=$(git rev-parse --short=7 HEAD) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # 根据是否是 dev 分支决定 tag 策略 | ||||||||||||||||||||||||||||||||||
| if [ "$BRANCH_NAME" = "dev" ]; then | ||||||||||||||||||||||||||||||||||
| BASE_TAG="dev" | ||||||||||||||||||||||||||||||||||
| VERSIONED_TAG="dev-$SHORT_SHA" | ||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||
| BASE_TAG="$SAFE_BRANCH_NAME" | ||||||||||||||||||||||||||||||||||
| VERSIONED_TAG="$SAFE_BRANCH_NAME-$SHORT_SHA" | ||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT | ||||||||||||||||||||||||||||||||||
| echo "safe_branch_name=$SAFE_BRANCH_NAME" >> $GITHUB_OUTPUT | ||||||||||||||||||||||||||||||||||
| echo "short_sha=$SHORT_SHA" >> $GITHUB_OUTPUT | ||||||||||||||||||||||||||||||||||
| echo "Dev build version: dev-$SHORT_SHA" | ||||||||||||||||||||||||||||||||||
| echo "base_tag=$BASE_TAG" >> $GITHUB_OUTPUT | ||||||||||||||||||||||||||||||||||
| echo "versioned_tag=$VERSIONED_TAG" >> $GITHUB_OUTPUT | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| echo "Building from branch: $BRANCH_NAME" | ||||||||||||||||||||||||||||||||||
| echo "Safe branch name: $SAFE_BRANCH_NAME" | ||||||||||||||||||||||||||||||||||
| echo "Base tag: $BASE_TAG" | ||||||||||||||||||||||||||||||||||
| echo "Versioned tag: $VERSIONED_TAG" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| - name: Setup Node.js for formatting | ||||||||||||||||||||||||||||||||||
| uses: actions/setup-node@v4 | ||||||||||||||||||||||||||||||||||
|
|
@@ -60,8 +86,8 @@ jobs: | |||||||||||||||||||||||||||||||||
| echo "No changes to commit" | ||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||
| # 提交格式化后的代码 | ||||||||||||||||||||||||||||||||||
| git commit -m "chore: format code (dev-${{ steps.commit.outputs.short_sha }})" | ||||||||||||||||||||||||||||||||||
| git push origin dev | ||||||||||||||||||||||||||||||||||
| git commit -m "chore: format code (${{ steps.branch_info.outputs.versioned_tag }})" | ||||||||||||||||||||||||||||||||||
| git push origin ${{ steps.branch_info.outputs.branch_name }} | ||||||||||||||||||||||||||||||||||
|
Comment on lines
86
to
+90
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 潜在的自动提交问题 在所有非 main 分支都自动提交格式化修改,可能导致意外的 commit 历史污染。 建议:
Suggested change
|
||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| - name: Prepare image names | ||||||||||||||||||||||||||||||||||
|
|
@@ -92,25 +118,26 @@ jobs: | |||||||||||||||||||||||||||||||||
| platforms: linux/amd64,linux/arm64 | ||||||||||||||||||||||||||||||||||
| push: true | ||||||||||||||||||||||||||||||||||
| build-args: | | ||||||||||||||||||||||||||||||||||
| APP_VERSION=dev-${{ steps.commit.outputs.short_sha }} | ||||||||||||||||||||||||||||||||||
| APP_VERSION=${{ steps.branch_info.outputs.versioned_tag }} | ||||||||||||||||||||||||||||||||||
| tags: | | ||||||||||||||||||||||||||||||||||
| ${{ steps.image_names.outputs.ghcr_image }}:dev-${{ steps.commit.outputs.short_sha }} | ||||||||||||||||||||||||||||||||||
| ${{ steps.image_names.outputs.ghcr_image }}:dev | ||||||||||||||||||||||||||||||||||
| ${{ steps.image_names.outputs.ghcr_image }}:${{ steps.branch_info.outputs.versioned_tag }} | ||||||||||||||||||||||||||||||||||
| ${{ steps.image_names.outputs.ghcr_image }}:${{ steps.branch_info.outputs.base_tag }} | ||||||||||||||||||||||||||||||||||
| labels: | | ||||||||||||||||||||||||||||||||||
| org.opencontainers.image.version=dev-${{ steps.commit.outputs.short_sha }} | ||||||||||||||||||||||||||||||||||
| org.opencontainers.image.version=${{ steps.branch_info.outputs.versioned_tag }} | ||||||||||||||||||||||||||||||||||
| org.opencontainers.image.revision=${{ github.sha }} | ||||||||||||||||||||||||||||||||||
| org.opencontainers.image.source=https://github.com/${{ github.repository }} | ||||||||||||||||||||||||||||||||||
| cache-from: type=gha | ||||||||||||||||||||||||||||||||||
| cache-to: type=gha,mode=max | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| - name: Build summary | ||||||||||||||||||||||||||||||||||
| run: | | ||||||||||||||||||||||||||||||||||
| echo "✅ Dev build completed successfully!" | ||||||||||||||||||||||||||||||||||
| echo "✅ Build completed successfully!" | ||||||||||||||||||||||||||||||||||
| echo "" | ||||||||||||||||||||||||||||||||||
| echo "📦 Branch: ${{ steps.branch_info.outputs.branch_name }}" | ||||||||||||||||||||||||||||||||||
| echo "📦 Docker Images:" | ||||||||||||||||||||||||||||||||||
| echo " - ${{ steps.image_names.outputs.ghcr_image }}:dev-${{ steps.commit.outputs.short_sha }}" | ||||||||||||||||||||||||||||||||||
| echo " - ${{ steps.image_names.outputs.ghcr_image }}:dev" | ||||||||||||||||||||||||||||||||||
| echo " - ${{ steps.image_names.outputs.ghcr_image }}:${{ steps.branch_info.outputs.versioned_tag }}" | ||||||||||||||||||||||||||||||||||
| echo " - ${{ steps.image_names.outputs.ghcr_image }}:${{ steps.branch_info.outputs.base_tag }}" | ||||||||||||||||||||||||||||||||||
| echo "" | ||||||||||||||||||||||||||||||||||
| echo "🚀 Usage:" | ||||||||||||||||||||||||||||||||||
| echo " docker pull ${{ steps.image_names.outputs.ghcr_image }}:dev" | ||||||||||||||||||||||||||||||||||
| echo " docker pull ${{ steps.image_names.outputs.ghcr_image }}:dev-${{ steps.commit.outputs.short_sha }}" | ||||||||||||||||||||||||||||||||||
| echo " docker pull ${{ steps.image_names.outputs.ghcr_image }}:${{ steps.branch_info.outputs.base_tag }}" | ||||||||||||||||||||||||||||||||||
| echo " docker pull ${{ steps.image_names.outputs.ghcr_image }}:${{ steps.branch_info.outputs.versioned_tag }}" | ||||||||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这种写法在某些 GitHub Actions 版本中可能不生效(
'**'和'!main'的组合)。建议改为更可靠的语法:
这样更简洁且被广泛支持。