|
| 1 | +# .github/workflows/docker-publish-ghcr.yml |
| 2 | + |
| 3 | +name: Docker Image CI (GHCR - CET_BOT) |
| 4 | + |
| 5 | +on: |
| 6 | + push: |
| 7 | + branches: [master] |
| 8 | + paths-ignore: |
| 9 | + - "**.md" |
| 10 | + - "docs/**" |
| 11 | + workflow_dispatch: |
| 12 | + |
| 13 | +jobs: |
| 14 | + check_commit: |
| 15 | + name: Check Build Settings |
| 16 | + runs-on: ubuntu-latest |
| 17 | + outputs: |
| 18 | + should_build: ${{ steps.check.outputs.should_build }} |
| 19 | + apply_tags: ${{ steps.check.outputs.apply_tags }} |
| 20 | + steps: |
| 21 | + - name: Checkout Repository |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + fetch-depth: 2 |
| 25 | + |
| 26 | + - name: Get commit message |
| 27 | + id: get_commit_message |
| 28 | + run: | |
| 29 | + COMMIT_MSG=$(git log -1 --pretty=format:"%s") |
| 30 | + echo "commit_message=$COMMIT_MSG" >> $GITHUB_OUTPUT |
| 31 | + echo "Commit message: $COMMIT_MSG" |
| 32 | +
|
| 33 | + - name: Set build settings |
| 34 | + id: check |
| 35 | + run: | |
| 36 | + COMMIT_MSG="${{ steps.get_commit_message.outputs.commit_message }}" |
| 37 | + if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then |
| 38 | + echo "should_build=true" >> $GITHUB_OUTPUT |
| 39 | + echo "apply_tags=true" >> $GITHUB_OUTPUT |
| 40 | + echo "Workflow dispatched manually. Building with tags..." |
| 41 | + elif [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/master" ]]; then |
| 42 | + echo "should_build=true" >> $GITHUB_OUTPUT |
| 43 | + # Only apply tags if commit message contains 'build' |
| 44 | + if [[ "$COMMIT_MSG" == *"build"* ]]; then |
| 45 | + echo "apply_tags=true" >> $GITHUB_OUTPUT |
| 46 | + echo "Push to master branch with 'build' in commit message. Building with tags..." |
| 47 | + else |
| 48 | + echo "apply_tags=false" >> $GITHUB_OUTPUT |
| 49 | + echo "Push to master branch without 'build' in commit message. Building without tags..." |
| 50 | + fi |
| 51 | + else |
| 52 | + echo "should_build=false" >> $GITHUB_OUTPUT |
| 53 | + echo "apply_tags=false" >> $GITHUB_OUTPUT |
| 54 | + echo "Not a master branch push or manual dispatch. Skipping build." |
| 55 | + fi |
| 56 | +
|
| 57 | + read_version: |
| 58 | + name: Read Version |
| 59 | + runs-on: ubuntu-latest |
| 60 | + outputs: |
| 61 | + version: ${{ steps.get_version.outputs.version }} |
| 62 | + steps: |
| 63 | + - name: Checkout Repository |
| 64 | + uses: actions/checkout@v4 |
| 65 | + |
| 66 | + - name: Read version from file |
| 67 | + id: get_version |
| 68 | + run: | |
| 69 | + VERSION=$(grep -oP 'version=\K[0-9]+\.[0-9]+\.[0-9]+' version.txt) |
| 70 | + echo "Version read from file: $VERSION" |
| 71 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 72 | +
|
| 73 | + build_and_push: |
| 74 | + name: Build and Push CET_BOT Image to GHCR |
| 75 | + runs-on: ubuntu-latest |
| 76 | + needs: [check_commit, read_version] |
| 77 | + if: ${{ needs.check_commit.outputs.should_build == 'true' }} |
| 78 | + permissions: |
| 79 | + contents: write |
| 80 | + packages: write |
| 81 | + |
| 82 | + steps: |
| 83 | + - name: Checkout Repository |
| 84 | + uses: actions/checkout@v4 |
| 85 | + with: |
| 86 | + fetch-depth: 0 |
| 87 | + |
| 88 | + - name: Set up QEMU |
| 89 | + uses: docker/setup-qemu-action@v3 |
| 90 | + |
| 91 | + - name: Set up Docker Buildx |
| 92 | + uses: docker/setup-buildx-action@v3 |
| 93 | + |
| 94 | + - name: Login to GitHub Container Registry |
| 95 | + uses: docker/login-action@v3 |
| 96 | + with: |
| 97 | + registry: ghcr.io |
| 98 | + username: ${{ github.actor }} |
| 99 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 100 | + |
| 101 | + # --- 新增步骤:转换所有者名称为小写 --- |
| 102 | + - name: Convert owner name to lowercase |
| 103 | + id: owner_lc |
| 104 | + run: echo "OWNER_LC=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV |
| 105 | + # ------------------------------------ |
| 106 | + |
| 107 | + # Test image deletion section removed as per requirement |
| 108 | + |
| 109 | + # Removed GitHub repository tagging |
| 110 | + |
| 111 | + - name: Create Version Tag |
| 112 | + if: ${{ needs.check_commit.outputs.apply_tags == 'true' }} |
| 113 | + run: | |
| 114 | + VERSION=${{ needs.read_version.outputs.version }} |
| 115 | + echo "Creating tag v$VERSION" |
| 116 | + git config user.name "GitHub Actions" |
| 117 | + git config user.email "[email protected]" |
| 118 | + git tag -a "v$VERSION" -m "Release version $VERSION" |
| 119 | + git push origin "v$VERSION" |
| 120 | +
|
| 121 | + - name: Build and push Docker image to GHCR with tags |
| 122 | + id: build-and-push-with-tags |
| 123 | + if: ${{ needs.check_commit.outputs.apply_tags == 'true' }} |
| 124 | + uses: docker/build-push-action@v5 |
| 125 | + with: |
| 126 | + context: . |
| 127 | + file: ./Dockerfile |
| 128 | + push: true |
| 129 | + tags: | # --- 使用转换后的小写所有者名称和版本号 --- |
| 130 | + ghcr.io/${{ env.OWNER_LC }}/cet_bot:latest |
| 131 | + ghcr.io/${{ env.OWNER_LC }}/cet_bot:sha-${{ github.sha }} |
| 132 | + ghcr.io/${{ env.OWNER_LC }}/cet_bot:v${{ needs.read_version.outputs.version }} |
| 133 | + labels: | |
| 134 | + org.opencontainers.image.title=CET_BOT |
| 135 | + org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} |
| 136 | + org.opencontainers.image.revision=${{ github.sha }} |
| 137 | + org.opencontainers.image.created=${{ steps.build-and-push-with-tags.outputs.metadata.image.created }} |
| 138 | +
|
| 139 | + # Test build section removed as per requirement |
| 140 | + |
| 141 | + - name: Build and push Docker image to GHCR without version tags |
| 142 | + id: build-and-push-without-tags |
| 143 | + if: ${{ needs.check_commit.outputs.apply_tags == 'false' && needs.check_commit.outputs.should_build == 'true' }} |
| 144 | + uses: docker/build-push-action@v5 |
| 145 | + with: |
| 146 | + context: . |
| 147 | + file: ./Dockerfile |
| 148 | + push: true |
| 149 | + tags: | # --- 使用唯一标识符和主分支标签 --- |
| 150 | + ghcr.io/${{ env.OWNER_LC }}/cet_bot:commit-${{ github.sha }} |
| 151 | + ghcr.io/${{ env.OWNER_LC }}/cet_bot:master |
| 152 | + # platforms: linux/amd64,linux/arm64 |
| 153 | + labels: | |
| 154 | + org.opencontainers.image.title=CET_BOT |
| 155 | + org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} |
| 156 | + org.opencontainers.image.revision=${{ github.sha }} |
| 157 | + org.opencontainers.image.created=${{ steps.build-and-push-without-tags.outputs.metadata.image.created }} |
| 158 | + # (可选) 输出构建的镜像 Digest 和元数据 |
| 159 | + # - name: Print image digest |
| 160 | + # run: echo ${{ steps.build-and-push.outputs.digest }} |
| 161 | + # - name: Print image metadata |
| 162 | + # run: echo '${{ steps.build-and-push.outputs.metadata }}' |
| 163 | + |
| 164 | + create_release: |
| 165 | + name: Create GitHub Release |
| 166 | + runs-on: ubuntu-latest |
| 167 | + needs: [read_version, build_and_push, check_commit] |
| 168 | + if: ${{ needs.check_commit.outputs.apply_tags == 'true' }} |
| 169 | + permissions: |
| 170 | + contents: write |
| 171 | + steps: |
| 172 | + - name: Checkout Repository |
| 173 | + uses: actions/checkout@v4 |
| 174 | + |
| 175 | + - name: Create Release |
| 176 | + uses: actions/create-release@v1 |
| 177 | + env: |
| 178 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 179 | + with: |
| 180 | + tag_name: v${{ needs.read_version.outputs.version }} |
| 181 | + release_name: Release v${{ needs.read_version.outputs.version }} |
| 182 | + draft: false |
| 183 | + prerelease: false |
| 184 | + body: | |
| 185 | + Release version ${{ needs.read_version.outputs.version }} |
| 186 | +
|
| 187 | + Docker image available at: |
| 188 | + - ghcr.io/${{ github.repository_owner }}/cet_bot:v${{ needs.read_version.outputs.version }} |
| 189 | + - ghcr.io/${{ github.repository_owner }}/cet_bot:latest |
0 commit comments