-
Notifications
You must be signed in to change notification settings - Fork 0
135 lines (130 loc) · 5.04 KB
/
update-deps.yml
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
# Inspired by:
# - https://github.com/nhoizey/1y/blob/master/.github/workflows/update-dependencies.yml
# - https://michaelcurrin.github.io/code-cookbook/recipes/ci-cd/github-actions/workflows/node/upgrade-packages.html
name: Update Dependencies
on:
workflow_dispatch:
# workflow_call:
# schedule:
# # https://crontab.guru/#0_8_*_*_1
# - cron: "0 8 * * 1"
jobs:
update-packages:
name: Update NPM Packages
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "lts/Gallium"
cache: "npm"
- name: Install dependencies
run: npm ci
- name: Check for Outdated Packages
id: check
run: |
# Log execution steps
# set -x
# Stop everything if there is any error
# set -e
# Get the list of npm package in dependencies that have more recent versions
OUTDATED=$(npm outdated || true)
if [[ -z "$OUTDATED" ]]; then
# There is no package with more recent versions
echo 'Nothing to update' >> "${GITHUB_STEP_SUMMARY}"
# Set a variable to know that next steps should NOT be run
echo "updatable=false" >> "${GITHUB_OUTPUT}"
else
# There are packages with more recent versions
# Get the header of the packages table
HEADER=$(echo "$OUTDATED" | grep "Package")
# Get the list of packages that will really be updated, with compatible semver
COMPATIBLE=$(echo "$OUTDATED" | awk '$2!=$3' | grep -v "Package" || true)
if [[ -z "$COMPATIBLE" ]]; then
# There is no compatible update
COMPATIBLE_COUNT=0
else
# Count how many compatible updates are available
COMPATIBLE_COUNT=$(($(echo "$COMPATIBLE" | wc -l) + 0))
fi
# Get the list of packages that have recent versions not compatible with semver
NOT_COMPATIBLE=$(echo "$OUTDATED" | awk '$2==$3' || true)
if [[ -z "$NOT_COMPATIBLE" ]]; then
# There is no not compatible update
NOT_COMPATIBLE_COUNT=0
else
# Count how many not compatible updates are available
NOT_COMPATIBLE_COUNT=$(($(echo "$NOT_COMPATIBLE" | wc -l) + 0))
fi
if [[ $COMPATIBLE_COUNT -gt 0 ]]; then
# There are compatible updates, let's list them in the Action summary
{
echo "${COMPATIBLE_COUNT} package(s) will be updated:"
echo '```'
echo "${HEADER}"
echo "${COMPATIBLE}"
echo '```'
} >> "${GITHUB_STEP_SUMMARY}"
# Keep track of this list for the Pull Request body
delimiter="$(openssl rand -hex 8)"
{
echo "updates_list<<${delimiter}"
echo "${COMPATIBLE_COUNT} package(s) will be updated:"
echo '```'
echo "${HEADER}"
echo "${COMPATIBLE}"
echo '```'
echo "${delimiter}"
} >> "${GITHUB_OUTPUT}"
# Set a variable to know that next steps should be run
echo "updatable=true" >> "${GITHUB_OUTPUT}"
else
echo 'Nothing to update' >> "${GITHUB_STEP_SUMMARY}"
# Set a variable to know that next steps should NOT be run
echo "updatable=false" >> "${GITHUB_OUTPUT}"
fi
if [[ $NOT_COMPATIBLE_COUNT -gt 0 ]]; then
# Also show the list of not compatible updates in the Action summary
{
echo ''
echo "${NOT_COMPATIBLE_COUNT} not compatible update(s):"
echo '```'
echo "${HEADER}"
echo "${NOT_COMPATIBLE}"
echo '```'
} >> "${GITHUB_STEP_SUMMARY}"
fi
fi
- name: Update Packages
id: update
if: steps.check.outputs.updatable == 'true'
run: |
npm update --save
- name: Lint
if: steps.check.outputs.updatable == 'true'
run: npm run lint --if-present
- name: Test
if: steps.check.outputs.updatable == 'true'
run: npm run test --if-present
- name: Build
if: steps.check.outputs.updatable == 'true'
run: npm run build --if-present
- name: Commit and create PR
if: steps.check.outputs.updatable == 'true'
uses: peter-evans/create-pull-request@v5
with:
title: "build(deps): update NPM packages"
body: |
# Updating dependencies
${{ steps.check.outputs.updates_list }}
branch: "update-dependencies"
delete-branch: true
commit-message: "build(deps): update NPM packages"
labels: |
type: dependencies
automerge