-
Notifications
You must be signed in to change notification settings - Fork 0
133 lines (113 loc) · 4.94 KB
/
Copy pathupdate.yml
File metadata and controls
133 lines (113 loc) · 4.94 KB
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
name: Update OpenCode
on:
schedule:
- cron: '0 17 * * *'
workflow_dispatch:
jobs:
update-opencode:
runs-on: ubuntu-latest
permissions:
contents: write
issues: write
outputs:
fail_reason: ${{ steps.check.outputs.fail_reason }}
steps:
- uses: actions/checkout@v6
- uses: cachix/install-nix-action@v31
with:
nix_path: nixpkgs=channel:nixos-unstable
- name: Check for updates
id: check
continue-on-error: true
env:
GH_TOKEN: ${{ github.token }}
run: |
ERR_LOG="$(mktemp)"
die() {
echo "fail_reason=$1" | tr -d '\n\r' >> "$GITHUB_OUTPUT"
echo "$1" >&2
exit 1
}
LATEST_VERSION=""
for i in 1 2 3; do
if LATEST_VERSION=$(gh api repos/anomalyco/opencode/releases/latest --jq '.tag_name[1:]' 2>>"$ERR_LOG"); then
break
fi
[ "$i" -lt 3 ] && sleep 5 || true
done
if [ -z "$LATEST_VERSION" ]; then
die "Could not fetch latest opencode version from GitHub API after 3 attempts. Last error: $(tail -n 1 "$ERR_LOG" 2>/dev/null || echo 'unknown')"
fi
echo "fail_reason=" >> "$GITHUB_OUTPUT"
CURRENT_VERSION=$(grep -oP '"version":\s*"\K[^"]+' version.json)
echo "Latest: $LATEST_VERSION"
echo "Current: $CURRENT_VERSION"
if [ "$LATEST_VERSION" != "$CURRENT_VERSION" ]; then
echo "New version available!"
echo "update_needed=true" >> $GITHUB_OUTPUT
echo "version=$LATEST_VERSION" >> $GITHUB_OUTPUT
else
echo "Already up to date."
echo "update_needed=false" >> $GITHUB_OUTPUT
fi
- name: Update Flake
if: steps.check.outputs.update_needed == 'true'
run: |
chmod +x ./update-version.sh
./update-version.sh "${{ steps.check.outputs.version }}"
- name: Update flake.lock
if: steps.check.outputs.update_needed == 'true'
run: nix flake update
- name: Verify Flake
if: steps.check.outputs.update_needed == 'true'
run: |
nix flake check --no-build
nix build .#opencode --no-link
- name: Commit and Push
if: steps.check.outputs.update_needed == 'true'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add version.json opencode.nix opencode-desktop.nix flake.lock
git commit -m "chore: update opencode to ${{ steps.check.outputs.version }}"
git push
- name: Close resolved failure issues
if: steps.check.outcome == 'success'
env:
GH_TOKEN: ${{ github.token }}
run: |
LABEL="auto-failure"
gh label create "$LABEL" --repo "$GITHUB_REPOSITORY" 2>/dev/null || true
OPEN=$(gh issue list --repo "$GITHUB_REPOSITORY" --label "$LABEL" --state open --json number --jq '.[].number')
for n in $OPEN; do
echo "Closing auto-failure issue #$n (workflow succeeded again)"
gh issue close "$n" --repo "$GITHUB_REPOSITORY" --comment "The Update OpenCode workflow succeeded again — closing automatically."
done
# continue-on-error above would let the job pass; fail the run when the check failed.
- name: Fail the run
if: steps.check.outcome == 'failure'
run: exit 1
report-failure:
runs-on: ubuntu-latest
needs: update-opencode
if: failure()
permissions:
issues: write
steps:
- name: Open failure issue
env:
GH_TOKEN: ${{ github.token }}
run: |
LABEL="auto-failure"
gh label create "$LABEL" --repo "$GITHUB_REPOSITORY" 2>/dev/null || true
REASON="${{ needs.update-opencode.outputs.fail_reason }}"
[ -n "$REASON" ] || REASON="See the failed run logs for details."
TITLE="[auto] Update OpenCode workflow failed"
BODY=$(printf 'The **Update OpenCode** workflow failed on %s.\n\n- Run: %s/%s/actions/runs/%s\n- Job: update-opencode\n\n**Failure reason:** %s\n\n_This issue was created automatically. It will be closed automatically when the workflow succeeds again._\n' "$(date -u +'%Y-%m-%d %H:%M UTC')" "$GITHUB_SERVER_URL" "$GITHUB_REPOSITORY" "$GITHUB_RUN_ID" "$REASON")
EXISTING=$(gh issue list --repo "$GITHUB_REPOSITORY" --label "$LABEL" --state open --json number --jq '.[0].number // empty')
if [ -n "$EXISTING" ]; then
echo "Failure issue #$EXISTING already open — adding comment instead."
gh issue comment "$EXISTING" --repo "$GITHUB_REPOSITORY" --body "Still failing on run $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID — $REASON"
else
gh issue create --repo "$GITHUB_REPOSITORY" --title "$TITLE" --label "$LABEL" --body "$BODY"
fi