-
Notifications
You must be signed in to change notification settings - Fork 2.5k
105 lines (93 loc) · 3.61 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
name: Update NPM dependencies
on:
schedule:
- cron: "0 5 1,15 * *"
workflow_dispatch:
inputs:
config:
description: 'Config'
required: false
default: ''
jobs:
update:
runs-on: ubuntu-latest
strategy:
matrix:
deps: [production, dev-only]
steps:
- name: Setup node
# https://github.com/marketplace/actions/setup-node-js-environment
uses: actions/setup-node@v1
with:
node-version: '12'
- name: Checkout default branch
# https://github.com/marketplace/actions/checkout
uses: actions/checkout@v2
- name: Read config
run: |
npm i -g npm@7
npm -v
CONFIG='{}'
if [[ ! -z '${{ github.event.inputs.config }}' ]]; then
CONFIG='${{ github.event.inputs.config }}'
elif [[ -f "./.github/workflows-config.json" ]]; then
CONFIG=$( jq -c .updateDeps './.github/workflows-config.json' )
fi
echo "CONFIG=$CONFIG" >> $GITHUB_ENV
echo "Workflow config: $CONFIG"
- name: Check env variables
run: |
if [[ -z "${{ secrets.GH_BOT_USERNAME }}" ]]; then
echo "Expected 'GH_BOT_USERNAME' secret variable to be set."
exit 1
fi
if [[ -z "${{ secrets.GH_BOT_EMAIL }}" ]]; then
echo "Expected 'GH_BOT_EMAIL' secret variable to be set."
exit 1
fi
BRANCH_TARGET=$(echo '${{ env.CONFIG }}' | jq -r ".targetBranch")
if [[ -z "$BRANCH_TARGET" || "$BRANCH_TARGET" == "null" ]]; then
# Since 'Fetch changes' step fetches default branch, we just get branch name (which will be default one).
BRANCH_TARGET=$(git rev-parse --abbrev-ref HEAD)
fi
echo "BRANCH_TARGET=$BRANCH_TARGET" >> $GITHUB_ENV
- name: Check if update branch already exists
run: |
echo "BRANCH_UPDATE=deps-update_${{ env.BRANCH_TARGET }}_${{ matrix.deps }}" >> $GITHUB_ENV
if [[ $(git ls-remote --heads | grep deps-update_${{ env.BRANCH_TARGET }}_${{ matrix.deps }} | wc -c) -ne 0 ]]; then
echo "BRANCH_UPDATE=0" >> $GITHUB_ENV
fi
- name: Update NPM dependencies
if: env.BRANCH_UPDATE != 0
run: |
npm i
npm install -g npm-check
git checkout -b ${{ env.BRANCH_UPDATE }}
npm-check -y --${{ matrix.deps }}
- name: Add changes
if: env.BRANCH_UPDATE != 0
run: |
if [[ $(git diff origin/${{ env.BRANCH_TARGET }} | wc -c) -ne 0 ]]; then
git config --local user.email "${{ secrets.GH_BOT_EMAIL }}"
git config --local user.name "${{ secrets.GH_BOT_USERNAME }}"
git add package*.json
git commit -m "Update NPM ${{ matrix.deps }} dependencies."
echo "HAS_CHANGES=1" >> $GITHUB_ENV
fi
- name: Push changes
if: env.HAS_CHANGES == 1
# https://github.com/marketplace/actions/github-push
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ env.BRANCH_UPDATE }}
- name: Create PR
if: env.HAS_CHANGES == 1
# https://github.com/marketplace/actions/github-pull-request-action
uses: repo-sync/pull-request@v2
with:
source_branch: "${{ env.BRANCH_UPDATE }}"
destination_branch: "${{ env.BRANCH_TARGET }}"
pr_title: "Update NPM ${{ matrix.deps }} dependencies"
pr_body: "Updated NPM ${{ matrix.deps }} dependencies."
github_token: ${{ secrets.GITHUB_TOKEN }}