Check for New Release #32
This file contains hidden or 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 for New Release | |
| on: | |
| schedule: | |
| - cron: "05 0 * * 0" # 每周日凌晨 12:05 | |
| workflow_dispatch: | |
| permissions: | |
| actions: write | |
| contents: read | |
| issues: write # 必须启用,确保有权限读写 Issue | |
| jobs: | |
| check-release: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Fetch Latest Release | |
| id: fetch_release | |
| run: | | |
| latest_release=$(curl -s https://api.github.com/repos/cryptomator/cli/releases/latest | jq -r '.tag_name') | |
| echo "latest_release=$latest_release" >> $GITHUB_ENV | |
| echo "Latest release: $latest_release" | |
| - name: Find or Create Tracking Issue | |
| id: issue | |
| uses: actions/github-script@v6 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| result-encoding: string | |
| script: | | |
| const title = "Latest Release Tracker"; | |
| const owner = context.repo.owner; | |
| const repo = context.repo.repo; | |
| // 尝试查找已存在的 Issue | |
| const issues = await github.rest.issues.listForRepo({ | |
| owner, | |
| repo, | |
| state: "open", | |
| per_page: 100 | |
| }); | |
| let issue = issues.data.find(i => i.title === title); | |
| // 如果没有,就新建一个 | |
| if (!issue) { | |
| const created = await github.rest.issues.create({ | |
| owner, | |
| repo, | |
| title, | |
| body: "This issue is used by GitHub Actions to track the last fetched release." | |
| }); | |
| issue = created.data; | |
| } | |
| return issue.number; | |
| - name: Read Last Release from Comment | |
| id: read_comment | |
| uses: actions/github-script@v6 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| result-encoding: string | |
| script: | | |
| const issue_number = Number('${{ steps.issue.outputs.result }}'); | |
| const comments = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number, | |
| per_page: 100 | |
| }); | |
| const lastComment = comments.data.reverse().find(c => c.body.startsWith("Last Release:")); | |
| const last_release = lastComment ? lastComment.body.split(":")[1].trim() : ""; | |
| return last_release; | |
| - name: Compare Release | |
| id: compare | |
| run: | | |
| last_release=$(echo "${{ steps.read_comment.outputs.result }}" | xargs) | |
| if [ "${{ env.latest_release }}" == "$last_release" ]; then | |
| echo "No new release found." | |
| echo "should_continue=false" >> $GITHUB_ENV | |
| exit 0 | |
| fi | |
| echo "New release detected: ${{ env.latest_release }}" | |
| echo "should_continue=true" >> $GITHUB_ENV | |
| - name: Trigger Docker Build Workflow | |
| if: env.should_continue == 'true' | |
| uses: actions/github-script@v6 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| github.rest.actions.createWorkflowDispatch({ | |
| owner: 'latelan', | |
| repo: 'cryptomator-cli-docker', | |
| workflow_id: 'docker-build.yml', | |
| ref: 'main', | |
| inputs: { | |
| release_tag: '${{ env.latest_release }}' | |
| } | |
| }); | |
| - name: Update Tracking Comment | |
| if: env.should_continue == 'true' | |
| uses: actions/github-script@v6 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const issue_number = Number('${{ steps.issue.outputs.result }}'); | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number, | |
| body: `Last Release: ${{ env.latest_release }}` | |
| }); |