Update OpenCode #130
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: 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 |