-
Notifications
You must be signed in to change notification settings - Fork 46
142 lines (121 loc) · 4.31 KB
/
container-build-publishing.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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 }}