action scripts 使用shell命令tr '[:upper:]' '[:lower:]'来转换仓库名为小写 #2
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
| name: 🐳 构建并发布Docker镜像 | |
| on: | |
| push: | |
| branches: [ main ] | |
| tags: [ 'v*.*.*' ] | |
| pull_request: | |
| branches: [ main ] | |
| workflow_dispatch: # 支持手动触发 | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| jobs: | |
| docker: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: 📦 检出代码 | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: 📝 获取版本信息 | |
| id: get_version | |
| run: | | |
| if [[ $GITHUB_REF == refs/tags/* ]]; then | |
| VERSION=${GITHUB_REF#refs/tags/v} | |
| else | |
| VERSION=$(node -p "require('./package.json').version") | |
| fi | |
| REPO_LC=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]') | |
| echo "VERSION=$VERSION" >> $GITHUB_ENV | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "is_release=${{ startsWith(github.ref, 'refs/tags/') }}" >> $GITHUB_OUTPUT | |
| echo "repo_name=$REPO_LC" >> $GITHUB_OUTPUT | |
| - name: 🔧 设置 QEMU | |
| uses: docker/setup-qemu-action@v3 | |
| - name: 🔧 设置 Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| with: | |
| driver-opts: | | |
| image=moby/buildkit:latest | |
| - name: 🔑 登录到容器仓库 | |
| # 只在main分支或发布时登录 | |
| if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/') | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: 📋 提取镜像元数据 | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ steps.get_version.outputs.repo_name }} | |
| tags: | | |
| # PR时使用pr-number作为标签 | |
| type=raw,value=pr-${{ github.event.pull_request.number }},enable=${{ github.event_name == 'pull_request' }} | |
| # main分支使用latest标签 | |
| type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }} | |
| # 发布时使用版本号标签 | |
| type=raw,value=${{ steps.get_version.outputs.version }},enable=${{ steps.get_version.outputs.is_release == 'true' }} | |
| - name: 🏗️ 构建和推送 | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| push: ${{ github.event_name != 'pull_request' }} # PR不推送 | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: | | |
| org.opencontainers.image.version=${{ steps.get_version.outputs.version }} | |
| org.opencontainers.image.revision=${{ github.sha }} | |
| org.opencontainers.image.title=${{ steps.get_version.outputs.repo_name }} | |
| platforms: ${{ github.event_name != 'pull_request' && 'linux/amd64,linux/arm64' || 'linux/amd64' }} # PR只构建amd64 | |
| cache-from: | | |
| type=gha,scope=${{ github.workflow }} | |
| type=registry,ref=${{ env.REGISTRY }}/${{ steps.get_version.outputs.repo_name }}:buildcache | |
| cache-to: | | |
| type=gha,mode=max,scope=${{ github.workflow }} | |
| type=registry,ref=${{ env.REGISTRY }}/${{ steps.get_version.outputs.repo_name }}:buildcache,mode=max | |
| build-args: | | |
| BUILDKIT_INLINE_CACHE=1 | |
| VERSION=${{ steps.get_version.outputs.version }} | |
| - name: 📢 输出版本信息 | |
| run: | | |
| echo "🏷️ 版本: ${{ steps.get_version.outputs.version }}" | |
| echo "📦 镜像标签:" | |
| echo "${{ steps.meta.outputs.tags }}" | tr '\n' '\n ' | |
| - name: 📝 创建发布说明 | |
| if: startsWith(github.ref, 'refs/tags/') | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| name: 🚀 Release ${{ steps.get_version.outputs.version }} | |
| draft: false | |
| prerelease: false | |
| generate_release_notes: true |