-
Notifications
You must be signed in to change notification settings - Fork 16
252 lines (215 loc) · 10 KB
/
update-opencode.yml
File metadata and controls
252 lines (215 loc) · 10 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
name: Update OpenCode with nix-update
# IMPORTANT: GitHub scheduled workflows requirements:
# 1. Only runs on the default branch (master/main)
# 2. Automatically disabled after 60 days of repository inactivity
# 3. Must be pushed to default branch before schedule activates
# 4. All times are in UTC
#
# To reactivate after inactivity:
# - Make any commit to the repository
# - Or manually trigger using workflow_dispatch
on:
schedule:
# Run every 6 hours at 15 minutes past (avoids peak times)
# Times: 00:15, 06:15, 12:15, 18:15 UTC
- cron: "15 */6 * * *"
workflow_dispatch:
inputs:
version:
description: "Specific version to update to (optional)"
required: false
type: string
permissions:
contents: write
jobs:
update-opencode:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
- name: Set up Nix
uses: cachix/install-nix-action@v24
with:
nix_path: nixpkgs=channel:nixos-unstable
extra_nix_config: |
experimental-features = nix-command flakes
- name: Install nix-update
run: nix profile install github:Mic92/nix-update
- name: Configure Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Get current version for comparison
id: current-version
run: |
CURRENT_VERSION=$(grep -o 'version = "[^"]*"' package.nix | head -1 | sed 's/version = "\(.*\)"/\1/')
echo "current-version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
echo "Current version: $CURRENT_VERSION"
- name: Update OpenCode with nix-update
id: update-check
run: |
echo "Running nix-update for OpenCode..."
# Run nix-update for flakes - specify the flake attribute
if nix-update --flake opencode --build; then
# Check if any files were actually changed
if git diff --quiet; then
echo "No changes detected - already at latest version"
echo "has-changes=false" >> $GITHUB_OUTPUT
else
NEW_VERSION=$(grep -o 'version = "[^"]*"' package.nix | head -1 | sed 's/version = "\(.*\)"/\1/')
echo "Successfully updated to version: $NEW_VERSION"
echo "has-changes=true" >> $GITHUB_OUTPUT
echo "new-version=$NEW_VERSION" >> $GITHUB_OUTPUT
fi
else
echo "nix-update failed, checking if it's a vendorHash issue..."
# Check if nix-update updated the version but build failed
if git diff --name-only | grep -q "package.nix"; then
echo "package.nix was modified, attempting to fix vendorHash..."
# Run the vendor hash update script
if ./scripts/update-vendor-hash.sh; then
echo "Successfully updated vendorHash"
NEW_VERSION=$(grep -o 'version = "[^"]*"' package.nix | head -1 | sed 's/version = "\(.*\)"/\1/')
echo "has-changes=true" >> $GITHUB_OUTPUT
echo "new-version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "vendor-hash-updated=true" >> $GITHUB_OUTPUT
else
echo "Failed to update vendorHash"
exit 1
fi
else
echo "nix-update failed without modifying package.nix"
exit 1
fi
fi
- name: Handle manual version input
if: github.event.inputs.version
id: manual-version
run: |
MANUAL_VERSION="${{ github.event.inputs.version }}"
CURRENT_VERSION="${{ steps.current-version.outputs.current-version }}"
if [ "$CURRENT_VERSION" = "$MANUAL_VERSION" ]; then
echo "Already at requested version: $MANUAL_VERSION"
echo "has-changes=false" >> $GITHUB_OUTPUT
else
echo "Manual version specified: $MANUAL_VERSION"
# For manual versions, we'll run nix-update and then check if it updated to the desired version
NEW_VERSION=$(grep -o 'version = "[^"]*"' package.nix | head -1 | sed 's/version = "\(.*\)"/\1/')
if [ "$NEW_VERSION" = "$MANUAL_VERSION" ]; then
echo "Successfully updated to manual version: $MANUAL_VERSION"
else
echo "Warning: nix-update found version $NEW_VERSION, but manual version $MANUAL_VERSION was requested"
fi
fi
- name: Create update branch and commit changes
if: steps.update-check.outputs.has-changes == 'true'
id: create-branch
run: |
NEW_VERSION="${{ steps.update-check.outputs.new-version }}"
BRANCH_NAME="update-opencode-${NEW_VERSION}-$(date +%s)"
echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT
echo "Creating branch: $BRANCH_NAME"
git checkout -b "$BRANCH_NAME"
echo "Committing changes"
git add .
# Build commit message based on what was updated
COMMIT_MSG="Update OpenCode to version $NEW_VERSION"
COMMIT_MSG="${COMMIT_MSG}\n- Updated version and all platform-specific hashes"
if [ "${{ steps.update-check.outputs.vendor-hash-updated }}" = "true" ]; then
COMMIT_MSG="${COMMIT_MSG}\n- Fixed Go vendorHash for tui module"
fi
COMMIT_MSG="${COMMIT_MSG}\n- Updated subpackage dependencies (tui, node_modules)"
COMMIT_MSG="${COMMIT_MSG}\n- Automated update via nix-update"
echo -e "$COMMIT_MSG" | git commit -F -
- name: Create tag on branch
if: steps.update-check.outputs.has-changes == 'true'
run: |
NEW_VERSION="${{ steps.update-check.outputs.new-version }}"
echo "Creating tag v$NEW_VERSION"
git tag -a "v$NEW_VERSION" -m "OpenCode Flake version $NEW_VERSION"
- name: Merge branch to master
if: steps.update-check.outputs.has-changes == 'true'
id: merge-branch
run: |
BRANCH_NAME="${{ steps.create-branch.outputs.branch-name }}"
echo "Switching to master and merging $BRANCH_NAME"
# Switch to master and ensure it's up to date
git checkout ${{ github.ref_name }}
git pull origin ${{ github.ref_name }}
# Attempt fast-forward merge
if git merge "$BRANCH_NAME" --ff-only; then
echo "Successfully merged $BRANCH_NAME to master"
echo "merge-success=true" >> $GITHUB_OUTPUT
else
echo "Fast-forward merge failed. Master may have changed during update."
echo "merge-success=false" >> $GITHUB_OUTPUT
echo "This could happen if:"
echo "1. Another workflow updated master during this run"
echo "2. Manual commits were made to master"
echo "The update branch $BRANCH_NAME contains the changes and can be manually reviewed."
exit 1
fi
- name: Handle merge failure
if: failure() && steps.merge-branch.outputs.merge-success == 'false'
run: |
echo "Merge failed - the update was successful but couldn't be automatically merged."
echo "Branch ${{ steps.create-branch.outputs.branch-name }} contains the updates."
echo "Manual intervention may be required to resolve conflicts."
- name: Push changes and tags
if: steps.update-check.outputs.has-changes == 'true'
run: |
NEW_VERSION="${{ steps.update-check.outputs.new-version }}"
echo "Pushing master and tags"
git push origin ${{ github.ref_name }}
git push origin "v$NEW_VERSION"
- name: Create GitHub Release
if: steps.update-check.outputs.has-changes == 'true'
uses: softprops/action-gh-release@v1
with:
tag_name: v${{ steps.update-check.outputs.new-version }}
name: OpenCode Flake ${{ steps.update-check.outputs.new-version }}
body: |
OpenCode Flake version ${{ steps.update-check.outputs.new-version }}
This release packages [OpenCode](https://github.com/sst/opencode) v${{ steps.update-check.outputs.new-version }} as a Nix flake.
**Automated Update**: This version was automatically updated using nix-update, ensuring all dependencies and hashes are correct.
## Installation
```bash
# Run OpenCode directly
nix run github:AodhanHayter/opencode-flake
# Or install to your profile
nix profile install github:AodhanHayter/opencode-flake
```
## Using in a flake
```nix
{
inputs.opencode-flake.url = "github:AodhanHayter/opencode-flake";
outputs = { self, nixpkgs, opencode-flake, ... }: {
# Use in your outputs
packages.x86_64-linux.opencode = opencode-flake.packages.x86_64-linux.default;
};
}
```
draft: false
prerelease: false
- name: Cleanup update branch
if: always() && steps.create-branch.outputs.branch-name
run: |
BRANCH_NAME="${{ steps.create-branch.outputs.branch-name }}"
if [ -n "$BRANCH_NAME" ]; then
echo "Cleaning up branch: $BRANCH_NAME"
# Ensure we're on master before deleting branch
git checkout ${{ github.ref_name }} 2>/dev/null || true
# Delete the update branch
git branch -D "$BRANCH_NAME" 2>/dev/null || echo "Branch $BRANCH_NAME already deleted or doesn't exist"
fi
- name: Summary
if: always()
run: |
if [ "${{ steps.update-check.outputs.has-changes }}" = "true" ]; then
echo "✅ Successfully updated OpenCode to version ${{ steps.update-check.outputs.new-version }}"
echo "🏷️ Created tag and GitHub release"
else
echo "ℹ️ No updates needed - already at latest version"
fi