songtianlun
0f9b4efc80
- Include checkout code step using actions/checkout@v3 - Set fetch-depth to 0 to retrieve the full git history This change ensures that the workflow has access to the complete commit history, which may be necessary for certain operations within the Docker build process.
51 lines
1.6 KiB
YAML
51 lines
1.6 KiB
YAML
name: Build and Publish Docker Image
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
|
|
env:
|
|
# Use docker.io for Docker Hub if empty
|
|
REGISTRY: docker.io
|
|
IMAGE_NAME: ${{ github.event.repository.name }}
|
|
|
|
jobs:
|
|
docker:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v3
|
|
with:
|
|
fetch-depth: 0 # 获取完整的 git 历史
|
|
- name: Get version
|
|
id: get_version
|
|
run: |
|
|
GIT_VERSION=$(git describe --dirty --always --long --abbrev=7 --tags)
|
|
VERSION="v${GIT_VERSION}"
|
|
echo "VERSION=${VERSION}" >> $GITHUB_ENV
|
|
echo "GIT_VERSION=${GIT_VERSION}" >> $GITHUB_ENV
|
|
echo "LATEST_TAG=${{ env.REGISTRY }}/${{ secrets.DOCKERHUB_USER }}/${{ env.IMAGE_NAME }}:latest" >> $GITHUB_OUTPUT"
|
|
echo "VERSION_TAG=${{ env.REGISTRY }}/${{ secrets.DOCKERHUB_USER }}/${{ env.IMAGE_NAME }}:${VERSION}" >> $GITHUB_OUTPUT"
|
|
-
|
|
name: Login to ${{ env.REGISTRY }}
|
|
if: github.event_name != 'pull_request'
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ secrets.DOCKERHUB_USER }}
|
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
|
-
|
|
name: Set up QEMU
|
|
uses: docker/setup-qemu-action@v3
|
|
-
|
|
name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
-
|
|
name: Build and push
|
|
uses: docker/build-push-action@v6
|
|
with:
|
|
context: .
|
|
push: ${{ github.event_name != 'pull_request' }}
|
|
tags: ${{ env.LATEST_TAG }},${{ env.VERSION_TAG }}
|