自动更新依赖并发布版本
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
name: Update dependencies and release
|
||||
|
||||
on:
|
||||
schedule:
|
||||
# Every Sunday at 03:17 UTC (11:17 Asia/Shanghai).
|
||||
- cron: '17 3 * * 0'
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
|
||||
GITEA_SERVER_URL: ${{ github.server_url }}
|
||||
GITEA_REPOSITORY: ${{ github.repository }}
|
||||
|
||||
jobs:
|
||||
update-and-release:
|
||||
# The act_runner on the Raspberry Pi must expose the self-hosted label.
|
||||
runs-on: self-hosted
|
||||
timeout-minutes: 60
|
||||
|
||||
steps:
|
||||
- name: Check out the default branch
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
token: ${{ secrets.GITEA_TOKEN }}
|
||||
ref: ${{ env.DEFAULT_BRANCH }}
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
cache: true
|
||||
|
||||
- name: Update modules
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
go get -u ./...
|
||||
go mod tidy
|
||||
|
||||
- name: Run tests
|
||||
shell: bash
|
||||
run: go test -timeout=20m ./...
|
||||
|
||||
- name: Detect dependency changes
|
||||
id: changes
|
||||
shell: bash
|
||||
run: |
|
||||
if git diff --quiet -- go.mod go.sum; then
|
||||
echo "changed=false" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
echo "changed=true" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
|
||||
- name: Commit and push dependency update
|
||||
if: steps.changes.outputs.changed == 'true'
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
git config user.name "gitea-actions[bot]"
|
||||
git config user.email "gitea-actions[bot]@users.noreply.gitea.local"
|
||||
git add go.mod go.sum
|
||||
git commit -m "chore: update Go dependencies"
|
||||
git push origin "HEAD:$DEFAULT_BRANCH"
|
||||
|
||||
- name: Calculate next patch version
|
||||
if: steps.changes.outputs.changed == 'true'
|
||||
id: version
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
latest="$(git tag --list 'v[0-9]*' --sort=-version:refname | head -n 1)"
|
||||
if [[ -z "$latest" ]]; then
|
||||
next="v0.1.0"
|
||||
elif [[ "$latest" =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
|
||||
next="v${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.$((${BASH_REMATCH[3]} + 1))"
|
||||
else
|
||||
echo "Latest tag '$latest' is not a vMAJOR.MINOR.PATCH tag" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "tag=$next" >> "$GITHUB_OUTPUT"
|
||||
echo "Releasing $next"
|
||||
|
||||
- name: Tag the release
|
||||
if: steps.changes.outputs.changed == 'true'
|
||||
shell: bash
|
||||
env:
|
||||
RELEASE_TAG: ${{ steps.version.outputs.tag }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
git tag -a "$RELEASE_TAG" -m "Release $RELEASE_TAG"
|
||||
git push origin "$RELEASE_TAG"
|
||||
|
||||
- name: Create Gitea release
|
||||
if: steps.changes.outputs.changed == 'true'
|
||||
shell: bash
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
RELEASE_TAG: ${{ steps.version.outputs.tag }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
server_url="${GITEA_SERVER_URL:-${GITHUB_SERVER_URL:-}}"
|
||||
repository="${GITEA_REPOSITORY:-${GITHUB_REPOSITORY:-}}"
|
||||
if [[ -z "$server_url" || -z "$repository" ]]; then
|
||||
echo "GITEA_SERVER_URL and GITEA_REPOSITORY are required" >&2
|
||||
exit 1
|
||||
fi
|
||||
curl --fail-with-body --silent --show-error \
|
||||
--request POST \
|
||||
--header "Authorization: token ${GITEA_TOKEN}" \
|
||||
--header "Content-Type: application/json" \
|
||||
"${server_url%/}/api/v1/repos/${repository}/releases" \
|
||||
--data "$(printf '{"tag_name":"%s","name":"%s","body":"Automated Go dependency update."}' "$RELEASE_TAG" "$RELEASE_TAG")"
|
||||
Reference in New Issue
Block a user