Check Hugo Release & Publish to Docker Hub #50
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Check Hugo Release & Publish to Docker Hub | |
on: | |
schedule: | |
- cron: "0 0 * * *" # 每天檢查一次 | |
push: | |
branches: | |
- main | |
jobs: | |
check-and-publish: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Install jq | |
run: | | |
sudo apt-get install jq | |
echo "jq installed successfully." | |
- name: Fetch Hugo versions | |
id: fetch-versions | |
run: | | |
tags=$(curl --silent "https://api.github.com/repos/gohugoio/hugo/tags" | jq -r '.[].name' | sort -V | tr '\n' ';') | |
echo "Fetched Hugo versions: $tags" | |
echo "::set-output name=hugo_versions::$tags" | |
- name: Check if versions exist on Docker Hub | |
id: check-versions | |
run: | | |
existing_tags=$(curl -s "https://registry.hub.docker.com/v2/repositories/${{ secrets.DOCKERHUB_USERNAME }}/hugo/tags/" | jq -r '.results[].name') | |
echo "Existing tags on Docker Hub: $existing_tags" | |
versions_to_build="" | |
IFS=';' read -ra ADDR <<< "${{ steps.fetch-versions.outputs.hugo_versions }}" | |
for tag in "${ADDR[@]}"; do | |
if [[ "$tag" > "v0.115.4" && ! "$existing_tags" =~ "${tag#v}-ext-debian" ]]; then | |
versions_to_build="$versions_to_build${versions_to_build:+;}$tag" | |
fi | |
done | |
echo "Versions to build: $versions_to_build" | |
echo "VERSIONS_TO_BUILD=$versions_to_build" >> $GITHUB_ENV | |
- name: Checkout code | |
if: env.VERSIONS_TO_BUILD | |
uses: actions/checkout@v2 | |
- name: Login to Docker Hub | |
if: env.VERSIONS_TO_BUILD | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
- name: Build and push | |
if: env.VERSIONS_TO_BUILD | |
run: | | |
username=${{ secrets.DOCKERHUB_USERNAME }} | |
echo "Docker Hub username: $username" | |
builder=builder | |
if ! docker buildx ls | grep -q $builder; then | |
docker buildx create --name $builder | |
echo "Builder $builder created." | |
else | |
echo "Builder $builder already exists." | |
fi | |
docker buildx use $builder | |
docker buildx inspect --bootstrap | |
IFS=';' read -ra ADDR <<< "$VERSIONS_TO_BUILD" | |
for tag in "${ADDR[@]}"; do | |
ver=${tag#v} | |
echo "Building for version: $ver" | |
docker buildx build --build-arg HUGO_VERSION=$tag --platform=linux/amd64,linux/arm64,linux/arm/v6,linux/arm/v7 --push -t $username/hugo:$ver-ext-debian -t $username/hugo:latest . | |
done | |
echo "Docker images built and pushed successfully." |