name: Build and Push Docker Image on: push: branches: - "**" # 匹配所有分支 paths-ignore: # 忽略一些不必要的文件 - ".gitignore" - "README.md" - ".vscode/**" pull_request: branches: ["main"] env: REGISTRY: 113.44.68.213:3000 IMAGE_NAME: ${{ gitea.repository }} jobs: build-and-push: runs-on: ubuntu-latest permissions: contents: read packages: write steps: - name: 获取 Gitea 配置 run: | echo ${{ env.REGISTRY }} echo ${{ gitea.actor }} echo ${{ secrets.GHCR_USER }} echo ${{ secrets.GHCR_TOKEN }} echo ${{ env.IMGAE_NAME }} - name: 检出代码 uses: actions/checkout@v4 - name: 获取分支名称 id: branch_name run: | # 从 GITHUB_REF 提取分支名 branch=${GITHUB_REF#refs/heads/} echo "branch=$branch" >> $GITHUB_OUTPUT # 如果是 main 分支,使用 prod 标签,否则使用 dev-分支名 if [ "$branch" = "main" ]; then echo "env_suffix=prod" >> $GITHUB_OUTPUT else echo "env_suffix=dev-${branch}" >> $GITHUB_OUTPUT fi - name: 登录到 Github Container Registry uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ secrets.GHCR_USER }} password: ${{ secrets.GHCR_TOKEN }} - name: 提取 Docker 元数据 id: meta uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | # main 分支使用标准标签 type=raw,value=latest,enable=${{ steps.branch_name.outputs.branch == 'main' }} type=raw,value=${{ steps.branch_name.outputs.env_suffix }} type=sha,format=short,prefix=${{ steps.branch_name.outputs.env_suffix }}- type=ref,event=tag,prefix=${{ steps.branch_name.outputs.env_suffix }}- - name: 构建并推送 Docker 镜像 uses: docker/build-push-action@v5 with: context: . push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} build-args: | BUILDKIT_INLINE_CACHE=0 # 添加清理缓存的命令 outputs: type=docker,cleancache=true # - name: Trigger Portainer Webhook # run: | # curl -k -X POST ${{ secrets.BACKEND_WEBHOOK_URL }}