Check Tag & Build #4284
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 Tag & Build | |
# This workflow uses actions that are not certified by GitHub. | |
# They are provided by a third-party and are governed by | |
# separate terms of service, privacy policy, and support | |
# documentation. | |
on: | |
schedule: | |
- cron: '0 * * * *' # 每小时检查一次 | |
push: | |
branches: | |
- main # 选择你想要的分支 | |
env: | |
# Use docker.io for Docker Hub if empty | |
REGISTRY: ghcr.io | |
# github.repository as <account>/<repo> | |
IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
check-tag: | |
runs-on: ubuntu-latest | |
outputs: | |
trigger: ${{ steps.if_new_tag.outputs.new_tag }} | |
steps: | |
- name: Checkout This Repository | |
uses: actions/checkout@v4 | |
- name: Set up Git | |
run: | | |
git config --global user.name 'github-actions[bot]' | |
git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
- name: Check Latest Tag from Upstream | |
id: check_tag | |
run: | | |
UPSTREAM_LATEST_TAG=$(npm show musicn version | head -n 1) | |
echo "Latest upstream tag: $UPSTREAM_LATEST_TAG" | |
echo "latest_tag=$UPSTREAM_LATEST_TAG" >> $GITHUB_ENV | |
- name: Check Last Processed Tag | |
id: check_last_tag | |
run: | | |
if [ -f .last_upstream_tag ]; then | |
LAST_PROCESSED_TAG=$(cat .last_upstream_tag) | |
echo "Last processed tag: $LAST_PROCESSED_TAG" | |
else | |
LAST_PROCESSED_TAG="none" | |
echo "No last processed tag found." | |
fi | |
echo "last_tag=$LAST_PROCESSED_TAG" >> $GITHUB_ENV | |
- name: Compare Tags | |
id: if_new_tag | |
if: ${{ env.latest_tag != env.last_tag }} | |
run: | | |
echo "New tag detected: ${latest_tag}" | |
echo "new_tag=true" >> "$GITHUB_OUTPUT" | |
- name: Save Last Processed Tag | |
if: ${{ env.latest_tag != env.last_tag }} | |
run: echo ${latest_tag} > .last_upstream_tag | |
- name: Commit and Push Last Processed Tag | |
if: ${{ env.latest_tag != env.last_tag }} | |
run: | | |
git add .last_upstream_tag | |
git commit -m "Update last processed upstream tag to ${latest_tag}" | |
git push | |
build: | |
needs: check-tag | |
runs-on: ubuntu-latest | |
if: needs.check-tag.outputs.trigger == 'true' | |
permissions: | |
contents: read | |
packages: write | |
# This is used to complete the identity challenge | |
# with sigstore/fulcio when running outside of PRs. | |
id-token: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Setup Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 'latest' | |
- name: Get Latest Tag from Upstream | |
id: get_tag | |
run: | | |
UPSTREAM_LATEST_TAG=$(npm show musicn version | head -n 1) | |
echo "Latest upstream tag: $UPSTREAM_LATEST_TAG" | |
echo "latest_tag=$UPSTREAM_LATEST_TAG" >> $GITHUB_ENV | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
with: | |
platforms: all | |
- name: Setup Docker buildx | |
uses: docker/setup-buildx-action@v3 | |
# Login against a Docker registry except on PR | |
# https://github.com/docker/login-action | |
- name: Log into registry ${{ env.REGISTRY }} | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
# Extract metadata (tags, labels) for Docker | |
# https://github.com/docker/metadata-action | |
- name: Extract Docker metadata | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
flavor: | | |
latest=true | |
tags: | | |
type=raw,value=${{ env.latest_tag }} | |
# Build and push Docker image with Buildx (don't push on PR) | |
# https://github.com/docker/build-push-action | |
- name: Build and push Docker image | |
id: build-and-push | |
uses: docker/build-push-action@v6 | |
with: | |
builder: ${{ steps.buildx.outputs.name }} | |
context: . | |
platforms: linux/amd64,linux/arm64 | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} |