使用 GitHub Actions 进行可重现的构建

SOURCE_DATE_EPOCH是一个标准化的环境变量,用于指示构建工具生成可重现的输出。 为构建设置环境变量会使 image index、config 和 file 元数据反映指定的 Unix 时间。

要在 GitHub Actions 中设置环境变量, 在 build 步骤中使用 built-in 属性。env

Unix 纪元时间戳

以下示例将变量设置为 0,即 Unix 纪元。SOURCE_DATE_EPOCH


name: ci

on:
  push:

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Build
        uses: docker/build-push-action@v6
        with:
          tags: user/app:latest
        env:
          SOURCE_DATE_EPOCH: 0
name: ci

on:
  push:

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Build
        uses: docker/bake-action@v5
        env:
          SOURCE_DATE_EPOCH: 0

Git 提交时间戳

以下示例设置为 Git 提交时间戳。SOURCE_DATE_EPOCH


name: ci

on:
  push:

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Get Git commit timestamps
        run: echo "TIMESTAMP=$(git log -1 --pretty=%ct)" >> $GITHUB_ENV

      - name: Build
        uses: docker/build-push-action@v6
        with:
          tags: user/app:latest
        env:
          SOURCE_DATE_EPOCH: ${{ env.TIMESTAMP }}
name: ci

on:
  push:

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Get Git commit timestamps
        run: echo "TIMESTAMP=$(git log -1 --pretty=%ct)" >> $GITHUB_ENV

      - name: Build
        uses: docker/bake-action@v5
        env:
          SOURCE_DATE_EPOCH: ${{ env.TIMESTAMP }}

其他信息:

有关 BuildKit 中支持的更多信息, 请参阅 BuildKit 文档SOURCE_DATE_EPOCH