# yaml-language-server: $schema=https://json.schemastore.org/github-workflow name: Build Docker Image # 当 push 到 main 分支,或者创建以 v 开头的 tag 时触发,可根据需求修改 on: push: tags: - v* env: REGISTRY: ghcr.io IMAGE: iwumingz/sycgram # prinsss/ga-hit-counter jobs: build-and-push: runs-on: ubuntu-latest # 这里用于定义 GITHUB_TOKEN 的权限 permissions: packages: write contents: read steps: - name: Checkout uses: actions/checkout@v2 # 缓存 Docker 镜像以加速构建 - name: Cache Docker layers uses: actions/cache@v2 with: path: /tmp/.buildx-cache key: ${{ runner.os }}-buildx-${{ github.sha }} restore-keys: ${{ runner.os }}-buildx- # 配置 QEMU 和 buildx 用于多架构镜像的构建 - name: Set up QEMU uses: docker/setup-qemu-action@v1 - name: Set up Docker Buildx id: buildx uses: docker/setup-buildx-action@v1 - name: Inspect builder run: | echo "Name: ${{ steps.buildx.outputs.name }}" echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}" echo "Status: ${{ steps.buildx.outputs.status }}" echo "Flags: ${{ steps.buildx.outputs.flags }}" echo "Platforms: ${{ steps.buildx.outputs.platforms }}" # 登录到 GitHub Packages 容器仓库 # 注意 secrets.GITHUB_TOKEN 不需要手动添加,直接就可以用 - name: Log in to the Container registry uses: docker/login-action@v1 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} # 根据输入自动生成 tag 和 label 等数据,说明见下 - name: Extract metadata for Docker id: meta uses: docker/metadata-action@v3 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE }} # 构建并上传 - name: Build and push uses: docker/build-push-action@v2 with: context: . file: ./Dockerfile builder: ${{ steps.buildx.outputs.name }} platforms: linux/amd64,linux/arm64 push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=local,src=/tmp/.buildx-cache cache-to: type=local,dest=/tmp/.buildx-cache - name: Inspect image run: docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.IMAGE }}:${{ steps.meta.outputs.version }}