|
1 | | -name: Docker Image CI |
| 1 | +name: Build and Publish Docker Image |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
5 | | - branches: [ "main" ] |
6 | | - pull_request: |
7 | | - branches: [ "main" ] |
| 5 | + branches: [main] |
| 6 | + tags: ['v*'] |
| 7 | + workflow_dispatch: |
8 | 8 |
|
9 | | -jobs: |
10 | | - |
11 | | - build: |
| 9 | +env: |
| 10 | + REGISTRY: ghcr.io |
| 11 | + IMAGE_NAME: ${{ github.repository }} |
12 | 12 |
|
| 13 | +jobs: |
| 14 | + build-and-push: |
13 | 15 | runs-on: ubuntu-latest |
| 16 | + # 仅在主仓库运行,fork 仓库跳过 |
| 17 | + if: github.repository == 'dext7r/KiroGate' |
| 18 | + permissions: |
| 19 | + contents: read |
| 20 | + packages: write |
14 | 21 |
|
15 | 22 | steps: |
16 | | - - uses: actions/checkout@v4 |
17 | | - - name: Build the Docker image |
18 | | - run: docker build . --file Dockerfile --tag my-image-name:$(date +%s) |
| 23 | + - name: Checkout repository |
| 24 | + uses: actions/checkout@v4 |
| 25 | + |
| 26 | + - name: Set up Docker Buildx |
| 27 | + uses: docker/setup-buildx-action@v3 |
| 28 | + |
| 29 | + - name: Log in to Container Registry |
| 30 | + uses: docker/login-action@v3 |
| 31 | + with: |
| 32 | + registry: ${{ env.REGISTRY }} |
| 33 | + username: ${{ github.actor }} |
| 34 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 35 | + |
| 36 | + - name: Extract metadata (tags, labels) |
| 37 | + id: meta |
| 38 | + uses: docker/metadata-action@v5 |
| 39 | + with: |
| 40 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 41 | + tags: | |
| 42 | + type=ref,event=branch |
| 43 | + type=semver,pattern={{version}} |
| 44 | + type=semver,pattern={{major}}.{{minor}} |
| 45 | + type=sha,prefix= |
| 46 | +
|
| 47 | + - name: Build and push Docker image |
| 48 | + uses: docker/build-push-action@v6 |
| 49 | + with: |
| 50 | + context: . |
| 51 | + push: true |
| 52 | + tags: ${{ steps.meta.outputs.tags }} |
| 53 | + labels: ${{ steps.meta.outputs.labels }} |
| 54 | + cache-from: type=gha |
| 55 | + cache-to: type=gha,mode=max |
| 56 | + platforms: linux/amd64,linux/arm64 |
0 commit comments