Skip to content

Update Dockerfile

Update Dockerfile #35

name: Build and Push Docker Image
on:
push:
branches:
- main
tags:
- 'v*'
pull_request:
branches:
- main
workflow_dispatch:
env:
REGISTRY: docker.io
IMAGE_NAME: ${{ secrets.DOCKER_USERNAME }}/fixit
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch # 分支名 -> branch-main
type=ref,event=pr # PR 编号 -> pr-123
type=semver,pattern={{version}} # 标签 v1.2.3 -> 1.2.3
type=semver,pattern={{major}}.{{minor}} # 1.2
type=semver,pattern={{major}} # 1
type=sha,format=short # git 短 commit sha
type=raw,value=latest,enable={{is_default_branch}} # main 分支推送时也打 latest 标签
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile # 确保文件名正确
push: ${{ github.event_name != 'pull_request' }} # PR 时不推送
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64 # 可选:支持多架构
cache-from: type=gha
cache-to: type=gha,mode=max