Delete .github/workflows/docker-image.yml #5
This file contains 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 | |
# 定义了触发此workflow的条件 | |
on: | |
# schedule: | |
# - cron: '34 22 * * *' | |
push: | |
branches: [ "main" ] | |
tags: [ 'v*.*.*' ] | |
pull_request: | |
branches: [ "main" ] | |
# 定义全局变量 | |
env: | |
# 针对 GitHub Container Registry (GHCR) 使用带 '-' 的名称 | |
IMAGE_NAME_GHCR: Johnserf-Seed/TikTokWeb | |
# 针对 Docker Hub 使用不带 '-' 的名称 | |
IMAGE_NAME_DOCKERHUB: johnserfseed/TikTokWeb | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
id-token: write | |
steps: | |
# 从GitHub仓库中检出代码 | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
# 安装cosign工具 | |
- name: Install cosign | |
if: github.event_name != 'pull_request' | |
uses: sigstore/cosign-installer@6e04d228eb30da1757ee4e1dd75a0ec73a653e06 | |
with: | |
cosign-release: 'v2.1.1' | |
# 设置Docker的buildx环境,它允许多平台的镜像构建 | |
- name: Setup Docker buildx | |
uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf | |
# 登录到GitHub的容器注册中心 | |
- name: Login to GitHub Container Registry | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
# 登录到Docker Hub | |
- name: Login to Docker Hub | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c | |
with: | |
registry: docker.io | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
# 提取镜像的元数据 | |
- name: Extract Docker metadata for GHCR | |
id: meta-ghcr | |
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 | |
with: | |
images: ghcr.io/${{ env.IMAGE_NAME_GHCR }} | |
- name: Extract Docker metadata for Docker Hub | |
id: meta-dockerhub | |
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 | |
with: | |
images: ${{ env.IMAGE_NAME_DOCKERHUB }} | |
# 构建并推送到GitHub的容器注册中心 | |
- name: Build and push to GitHub Container Registry | |
id: build-push-ghcr | |
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a | |
with: | |
context: . | |
push: ${{ github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta-ghcr.outputs.tags }} | |
labels: ${{ steps.meta-ghcr.outputs.labels }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
# 构建并推送到Docker Hub | |
- name: Build and push to Docker Hub | |
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a | |
with: | |
context: . | |
push: ${{ github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta-dockerhub.outputs.tags }} | |
labels: ${{ steps.meta-dockerhub.outputs.labels }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
# 使用cosign对GitHub的容器注册中心中的镜像进行签名 | |
- name: Sign the published Docker image on GHCR | |
if: ${{ github.event_name != 'pull_request' }} | |
env: | |
TAGS: ${{ steps.meta-ghcr.outputs.tags }} | |
DIGEST: ${{ steps.build-push-ghcr.outputs.digest }} | |
run: | | |
if [ -z "$DIGEST" ]; then | |
echo "DIGEST is empty, can't sign the image!" | |
exit 1 | |
fi | |
echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST} |