84 lines
3.0 KiB
YAML
84 lines
3.0 KiB
YAML
name: Docker Build (Gitea)
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
- 'ci/**'
|
|
tags:
|
|
- 'v*'
|
|
workflow_dispatch:
|
|
|
|
env:
|
|
REGISTRY: git.viaeon.com
|
|
IMAGE: git.viaeon.com/admin/bepusdt
|
|
|
|
jobs:
|
|
build-and-push:
|
|
name: Build and Push Docker Image
|
|
runs-on: ubuntu
|
|
env:
|
|
RUNNER_TOOL_CACHE: /toolcache
|
|
|
|
steps:
|
|
- name: Install Docker CLI
|
|
run: |
|
|
if ! command -v docker &> /dev/null; then
|
|
if command -v apk &> /dev/null; then
|
|
apk add --no-cache docker-cli git
|
|
elif command -v apt-get &> /dev/null; then
|
|
apt-get update && apt-get install -y docker.io git
|
|
else
|
|
curl -fsSL https://download.docker.com/linux/static/stable/x86_64/docker-24.0.7.tgz | tar xz -C /tmp
|
|
mv /tmp/docker/docker /usr/local/bin/
|
|
chmod +x /usr/local/bin/docker
|
|
fi
|
|
fi
|
|
docker --version
|
|
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
fetch-tags: true
|
|
|
|
- name: Prepare version
|
|
id: ver
|
|
run: |
|
|
SHORT_SHA=$(git rev-parse --short=7 HEAD)
|
|
BASE=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
|
|
# 分支名可能带斜杠(ci/xxx),docker tag 不允许,统一换成短横线
|
|
REF_SLUG=$(echo "${GITHUB_REF_NAME}" | tr '/' '-')
|
|
echo "short_sha=${SHORT_SHA}" >> "$GITHUB_OUTPUT"
|
|
echo "ref_slug=${REF_SLUG}" >> "$GITHUB_OUTPUT"
|
|
echo "version=${BASE}-${SHORT_SHA}" >> "$GITHUB_OUTPUT"
|
|
echo "VERSION=${BASE}-${SHORT_SHA}"
|
|
|
|
- name: Login to Gitea Container Registry
|
|
run: |
|
|
echo "${{ secrets.TOKEN }}" | docker login "${{ env.REGISTRY }}" -u "${{ github.actor }}" --password-stdin 2>&1
|
|
|
|
- name: Build Docker image
|
|
run: |
|
|
# latest 只给 main,特性分支别把 latest 顶掉
|
|
TAGS="-t ${{ env.IMAGE }}:${{ steps.ver.outputs.short_sha }} -t ${{ env.IMAGE }}:${{ steps.ver.outputs.ref_slug }}"
|
|
if [ "${GITHUB_REF_NAME}" = "main" ]; then TAGS="$TAGS -t ${{ env.IMAGE }}:latest"; fi
|
|
docker build \
|
|
--label "org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}" \
|
|
--label "org.opencontainers.image.revision=${{ github.sha }}" \
|
|
--build-arg "VERSION=${{ steps.ver.outputs.version }}" \
|
|
$TAGS .
|
|
|
|
- name: Push Docker image
|
|
run: |
|
|
docker push "${{ env.IMAGE }}:${{ steps.ver.outputs.short_sha }}" 2>&1
|
|
docker push "${{ env.IMAGE }}:${{ steps.ver.outputs.ref_slug }}" 2>&1
|
|
if [ "${GITHUB_REF_NAME}" = "main" ]; then docker push "${{ env.IMAGE }}:latest" 2>&1; fi
|
|
|
|
- name: Summary
|
|
run: |
|
|
echo "镜像已推送:"
|
|
echo " ${{ env.IMAGE }}:${{ steps.ver.outputs.short_sha }}"
|
|
echo " ${{ env.IMAGE }}:${{ steps.ver.outputs.ref_slug }}"
|
|
echo " ${{ env.IMAGE }}:latest"
|