Skip to content

ci: 修复 tags 时不触发的问题 #14

ci: 修复 tags 时不触发的问题

ci: 修复 tags 时不触发的问题 #14

Workflow file for this run

name: Build and Push
on:
push:
branches:
- main
- dev*
tags:
- "*"
paths:
- "Containerfile"
- ".github/workflows/release.yml"
- "scripts/*"
jobs:
docker:
name: Docker Build and Push
runs-on: ubuntu-latest
steps:
- name: Checkout Source Code
uses: actions/checkout@v4
- name: Login to GitHub Container Registry (ghcr.io)
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Check Docker Hub Credentials
id: check_docker_creds
run: |
if [[ -n "${{ secrets.DOCKERHUB_USERNAME }}" && -n "${{ secrets.DOCKERHUB_TOKEN }}" ]]; then
echo "docker_creds_exist=true" >> $GITHUB_OUTPUT
else
echo "docker_creds_exist=false" >> $GITHUB_OUTPUT
fi
- name: Login to Docker Hub (if credentials are provided)
if: steps.check_docker_creds.outputs.docker_creds_exist == 'true'
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Generate Tags
id: generate_tags
env:
IMAGE_REPO: ${{ github.repository_owner }}/cfdns
run: |
# GHCR Repository
GHCR_REPO="ghcr.io/$IMAGE_REPO"
# Check if the current ref is a tag
if [[ "${{ github.ref_type }}" == "tag" ]]; then
# delete tag as v0.1.0 to 0.1.0
ref_tag="${{ github.ref_name }}"
ref_tag="${ref_tag#v}"
echo "Detected tag: $ref_tag"
echo "ghcr_image_tag=$GHCR_REPO:$ref_tag" >> $GITHUB_OUTPUT
echo "ghcr_image_latest=$GHCR_REPO:latest" >> $GITHUB_OUTPUT
elif [[ "${{ github.ref_name }}" == "main" ]]; then
echo "ghcr_image_tag=$GHCR_REPO:main" >> $GITHUB_OUTPUT
else
echo "ghcr_image_tag=$GHCR_REPO:dev" >> $GITHUB_OUTPUT
fi
# Docker Repository
DOCKER_REPO="docker.io/$IMAGE_REPO"
# Docker Hub tags (if credentials provided)
if [[ "${{ steps.check_docker_creds.outputs.docker_creds_exist }}" == "true" ]]; then
if [[ "${{ github.ref_type }}" == "tag" ]]; then
ref_tag="${{ github.ref_name }}"
ref_tag="${ref_tag#v}"
echo "Detected tag: $ref_tag"
echo "docker_image_tag=$DOCKER_REPO:$ref_tag" >> $GITHUB_OUTPUT
echo "docker_image_latest=$DOCKER_REPO:latest" >> $GITHUB_OUTPUT
elif [[ "${{ github.ref_name }}" == "main" ]]; then
echo "docker_image_tag=$DOCKER_REPO:main" >> $GITHUB_OUTPUT
else
echo "docker_image_tag=$DOCKER_REPO:dev" >> $GITHUB_OUTPUT
fi
fi
- 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 Docker Images
uses: docker/build-push-action@v6
with:
push: true
platforms: linux/amd64, linux/arm64
file: Containerfile
tags: |
${{ steps.generate_tags.outputs.ghcr_image_tag }}
${{ steps.generate_tags.outputs.ghcr_image_latest || '' }}
${{ steps.generate_tags.outputs.docker_image_tag || '' }}
${{ steps.generate_tags.outputs.docker_image_latest || '' }}