Update Dependencies #32
This file contains 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 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: Setup Bun | |
uses: oven-sh/setup-bun@v1 | |
with: | |
bun-version: latest | |
- name: Install npm-check-updates | |
run: bun install -g npm-check-updates | |
- 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 | |
if: env.EXISTING_PR != 'true' | |
id: run-ncu | |
run: | | |
ncu -u > ncu.log | |
cat ncu.log | |
# Extract updated packages and their new versions | |
UPDATED_PACKAGES=$(grep -E '^\s*[^ ]+\s+\S+\s+→\s+\S+' ncu.log | sed 's/^ *//' | awk '{gsub("@", "", $1); print $1 " " $4}' | tr '\n' ', ' | sed 's/, $//') | |
echo "UPDATED_PACKAGES=$UPDATED_PACKAGES" >> $GITHUB_ENV | |
- name: Check for changes | |
if: env.EXISTING_PR != 'true' | |
id: check-changes | |
run: | | |
if git diff --quiet 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 | |
bun install > 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: | | |
bun install > install-verify.log 2>&1 | |
bun run 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.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: install error" | |
BODY_HEADER="## Dependency Update Failed\n\nThe automated dependency update process encountered an error during the 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 bun.lockb | |
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 bun.lockb | |
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 }} |