Update deploy.yml #65
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: Auto-Deploy | ||
| on: | ||
| workflow_dispatch: | ||
| push: | ||
| branches: [ "main" ] | ||
| jobs: | ||
| build-push-and-release: | ||
| runs-on: ubuntu-latest | ||
| # 修复条件判断:允许手动触发 | ||
| if: | | ||
| github.event_name == 'workflow_dispatch' || | ||
| contains(join(github.event.commits.*.message, ' '), 'autorelease') | ||
| - name: Set up Python 3.13 | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: '3.13' | ||
| - name: Lowercase Repository Owner | ||
| id: lowercase-owner | ||
| run: | | ||
| REPO_OWNER_LOWER=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]') | ||
| echo "REPO_OWNER_LOWER=$REPO_OWNER_LOWER" >> $GITHUB_OUTPUT | ||
| - name: Generate Metadata | ||
| id: metadata | ||
| run: | | ||
| # 精确获取 7 位短 SHA | ||
| SHORT_SHA=$(git rev-parse --short=7 HEAD) | ||
| TIMESTAMP=$(date +%Y%m%d%H%M%S) | ||
| BUILD_DATE=$(date -u +'%Y-%m-%d %H:%M:%S UTC') | ||
| echo "VERSION=auto-$TIMESTAMP-$SHORT_SHA" >> $GITHUB_OUTPUT | ||
| echo "TAG_NAME=auto-$TIMESTAMP-$SHORT_SHA" >> $GITHUB_OUTPUT | ||
| echo "BUILD_DATE=$BUILD_DATE" >> $GITHUB_OUTPUT | ||
| echo "SHORT_SHA=$SHORT_SHA" >> $GITHUB_OUTPUT # 新增输出 | ||
| - name: Create Tag | ||
| run: | | ||
| git config --global user.name "GitHub Actions" | ||
| git config --global user.email "actions@github.com" | ||
| git tag -a "${{ steps.metadata.outputs.TAG_NAME }}" -m "Auto release" | ||
| git push origin "${{ steps.metadata.outputs.TAG_NAME }}" | ||
| - name: Build Docker Image | ||
| run: | | ||
| docker build \ | ||
| -t random-images-api:latest \ | ||
| -t random-images-api:${{ steps.metadata.outputs.VERSION }} . | ||
| - name: Export Docker Image | ||
| run: | | ||
| mkdir -p ./docker-images | ||
| docker save random-images-api:${{ steps.metadata.outputs.VERSION }} \ | ||
| -o ./docker-images/image-${{ steps.metadata.outputs.VERSION }}.tar | ||
| - name: Login to GHCR | ||
| uses: docker/login-action@v3 | ||
| with: | ||
| registry: ghcr.io | ||
| username: ${{ github.actor }} | ||
| password: ${{ secrets.GITHUB_TOKEN }} | ||
| - name: Push to GHCR | ||
| run: | | ||
| GHCR_IMAGE="ghcr.io/${{ steps.lowercase-owner.outputs.REPO_OWNER_LOWER }}/random-images-api" | ||
| docker tag random-images-api:latest $GHCR_IMAGE:latest | ||
| docker tag random-images-api:${{ steps.metadata.outputs.VERSION }} $GHCR_IMAGE:${{ steps.metadata.outputs.VERSION }} | ||
| docker push $GHCR_IMAGE:latest | ||
| docker push $GHCR_IMAGE:${{ steps.metadata.outputs.VERSION }} | ||
| - name: Create Release | ||
| uses: softprops/action-gh-release@v1 | ||
| with: | ||
| tag_name: ${{ steps.metadata.outputs.TAG_NAME }} | ||
| name: "Release ${{ steps.metadata.outputs.VERSION }}" | ||
| body: | | ||
| ### 构建信息 | ||
| - **版本**: ${{ steps.metadata.outputs.VERSION }} | ||
| - **时间**: ${{ steps.metadata.outputs.BUILD_DATE }} | ||
| - **提交**: [${{ steps.metadata.outputs.SHORT_SHA }}](https://github.com/${{ github.repository }}/commit/${{ github.sha }}) | ||
| ### 镜像使用 | ||
| #### 从附件加载 | ||
| ```bash | ||
| docker load -i image-${{ steps.metadata.outputs.VERSION }}.tar | ||
| ``` | ||
| #### 从 GHCR 拉取 | ||
| ```bash | ||
| # 最新稳定版 | ||
| docker pull ghcr.nju.edu.cn/${{ steps.lowercase-owner.outputs.REPO_OWNER_LOWER }}/random-images-api:latest | ||
| # 本次特定版本 | ||
| docker pull ghcr.nju.edu.cn/${{ steps.lowercase-owner.outputs.REPO_OWNER_LOWER }}/random-images-api:${{ steps.metadata.outputs.VERSION }} | ||
| ``` | ||
| files: | | ||
| docker-images/image-${{ steps.metadata.outputs.VERSION }}.tar | ||