-
Notifications
You must be signed in to change notification settings - Fork 0
202 lines (174 loc) · 7.28 KB
/
ncudeps.yaml
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
name: Update Dependencies
on:
schedule:
- cron: '0 12 */2 * *' # Runs every other day at 12:00 UTC
workflow_dispatch:
push:
branches:
- main
jobs:
update-dependencies:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Node.js (LTS)
uses: actions/setup-node@v3
with:
node-version: '20'
- name: Install npm-check-updates
run: npm install -g npm-check-updates pnpm
- name: Check for existing PR
id: check-pr
run: |
existing_pr=$(gh pr list --json number,title --jq '.[] | select(.title | startswith("deps: Update dependencies")) | .number')
if [ ! -z "$existing_pr" ]; then
echo "EXISTING_PR=true" >> $GITHUB_ENV
echo "Existing dependency update PR found: #$existing_pr"
exit 0
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create branch
if: env.EXISTING_PR != 'true'
run: |
BRANCH_NAME="deps/update-$(date +%Y%m%d-%H%M%S)"
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
git checkout -b $BRANCH_NAME
- name: Run npm-check-updates in Root
if: env.EXISTING_PR != 'true'
id: run-ncu-root
run: |
ncu -w * -u > ncu-root.log
cat ncu-root.log
# Extract updated packages and their new versions
UPDATED_PACKAGES=$(grep -E '^\s*[^ ]+\s+\S+\s+→\s+\S+' ncu-root.log | sed 's/^ *//' | awk '{gsub("@", "", $1); print $1 " " $4}' | tr '\n' ', ' | sed 's/, $//')
echo "UPDATED_PACKAGES=$UPDATED_PACKAGES" >> $GITHUB_ENV
- name: Run npm-check-updates in apps/web
if: env.EXISTING_PR != 'true'
id: run-ncu-web
run: |
cd apps/web
ncu -u > ../../ncu-web.log
cat ../../ncu-web.log
# Extract updated packages and append to existing list
WEB_UPDATED_PACKAGES=$(grep -E '^\s*[^ ]+\s+\S+\s+→\s+\S+' ../../ncu-web.log | sed 's/^ *//' | awk '{gsub("@", "", $1); print $1 " " $4}' | tr '\n' ', ' | sed 's/, $//')
if [ ! -z "$WEB_UPDATED_PACKAGES" ]; then
echo "UPDATED_PACKAGES=$UPDATED_PACKAGES, $WEB_UPDATED_PACKAGES" >> $GITHUB_ENV
fi
- name: Check for changes
if: env.EXISTING_PR != 'true'
id: check-changes
run: |
if git diff --quiet package.json apps/web/package.json; then
echo "No dependency updates available."
echo "HAS_CHANGES=false" >> $GITHUB_ENV
exit 0
else
echo "HAS_CHANGES=true" >> $GITHUB_ENV
fi
- name: Install updated dependencies
if: env.EXISTING_PR != 'true' && env.HAS_CHANGES == 'true'
id: install-dependencies
run: |
# Clean install to ensure lock file is updated correctly
rm -rf node_modules
rm -rf apps/web/node_modules
# Set PNPM to bypass frozen lockfile for dependency updates
pnpm i --no-frozen-lockfile > npm-install.log 2>&1
INSTALL_EXIT_CODE=$?
if [ $INSTALL_EXIT_CODE -ne 0 ]; then
echo "INSTALL_FAILED=true" >> $GITHUB_ENV
fi
continue-on-error: true
- name: Run build
if: env.EXISTING_PR != 'true' && env.HAS_CHANGES == 'true' && env.INSTALL_FAILED != 'true'
id: build
run: |
# Run build with frozen lockfile (CI behavior)
export PNPM_FLAGS="--frozen-lockfile"
pnpm install $PNPM_FLAGS > install-verify.log 2>&1
pnpm build > build.log 2>&1
BUILD_EXIT_CODE=$?
if [ $BUILD_EXIT_CODE -ne 0 ]; then
echo "BUILD_FAILED=true" >> $GITHUB_ENV
fi
continue-on-error: true
- name: Prepare PR description
if: env.EXISTING_PR != 'true' && env.HAS_CHANGES == 'true'
id: pr-description
run: |
{
echo "## Dependency Updates"
echo ""
echo "### Updated Packages"
echo "\`\`\`"
cat ncu-root.log
echo ""
cat ncu-web.log
echo "\`\`\`"
echo ""
echo "### Installation Logs"
echo "\`\`\`"
cat npm-install.log
echo "\`\`\`"
echo ""
if [ -f install-verify.log ]; then
echo "### Verification Install Logs"
echo "\`\`\`"
cat install-verify.log
echo "\`\`\`"
echo ""
fi
if [ -f build.log ]; then
echo "### Build Logs"
echo "\`\`\`"
cat build.log
echo "\`\`\`"
fi
} > pr-body.txt
- name: Create Issue and Draft PR for failures
if: env.EXISTING_PR != 'true' && env.HAS_CHANGES == 'true' && (env.INSTALL_FAILED == 'true' || env.BUILD_FAILED == 'true')
run: |
if [ "${{ env.INSTALL_FAILED }}" = "true" ]; then
TITLE="Dependency Update Failed: npm install error"
BODY_HEADER="## Dependency Update Failed\n\nThe automated dependency update process encountered an error during the npm install step."
else
TITLE="Dependency Update Failed: Build Error"
BODY_HEADER="## Build Failed After Dependency Update\n\nThe automated dependency update process encountered an error during the build step."
fi
# Create issue
ISSUE_NUMBER=$(gh issue create --repo ${{ github.repository }} --title "$TITLE" \
--body "$(echo -e "$BODY_HEADER\n\n$(cat pr-body.txt)")" --json number -q .number)
# Commit changes
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add package.json pnpm-lock.yaml apps/web/package.json
git commit -m "deps: Update dependencies (with issues)
Related to #$ISSUE_NUMBER"
git push origin ${{ env.BRANCH_NAME }}
# Create draft PR linking to the issue
gh pr create --title "deps: Update dependencies (has issues)" \
--body "$(cat pr-body.txt)
⚠️ This PR has some issues that need to be resolved. See #$ISSUE_NUMBER for details." \
--base main \
--head ${{ env.BRANCH_NAME }} \
--draft
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create PR for successful updates
if: env.EXISTING_PR != 'true' && env.HAS_CHANGES == 'true' && env.INSTALL_FAILED != 'true' && env.BUILD_FAILED != 'true'
run: |
# Commit changes
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add package.json pnpm-lock.yaml apps/web/package.json
git commit -m "deps: Update dependencies"
git push origin ${{ env.BRANCH_NAME }}
# Create PR
gh pr create --title "deps: Update dependencies" \
--body "$(cat pr-body.txt)" \
--base main \
--head ${{ env.BRANCH_NAME }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}