-
Notifications
You must be signed in to change notification settings - Fork 1
131 lines (117 loc) · 4.52 KB
/
reusable-sync.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
name: Sync file
# Be aware that the PR opened by this action won't run any actions itself because of
# limitations within GitHub Actions where an Action can't cause another Action to run.
# See eg: https://github.com/orgs/community/discussions/25332#discussioncomment-3247510
on:
workflow_call:
inputs:
url:
description: 'The URL to sync'
required: true
type: string
path:
description: 'The path to sync it to'
required: true
type: string
branch-name:
description: 'The name of the branch to create'
required: false
default: automated/sync
type: string
pr-title:
description: 'The title of the PR.'
required: false
default: 'Syncing of file'
type: string
pr-body:
description: 'The body of the PR.'
required: false
default: 'This PR syncs a specified file'
type: string
commit-message:
description: 'The title of the PR.'
required: false
default: "fix: sync file"
type: string
npm-post-sync-script:
description: 'The name of a script to run post sync.'
required: false
type: string
no-lockfile:
description: 'Whether to expect a lockfile or not'
required: false
default: false
type: boolean
permissions:
contents: write
pull-requests: write
jobs:
fetch_and_update:
name: Fetch and sync
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Fetch ${{ inputs.url }}
run: curl --fail "${{ inputs.url }}" > ${{ inputs.path }}
- name: Set up Node.js
if: ${{ inputs.npm-post-sync-script }}
uses: actions/setup-node@v3
with:
node-version: lts/*
- name: Determine npm cache directory windows
id: npm-cache-dir-windows
if: runner.os == 'Windows'
run: echo "dir=$(npm config get cache)" >> $env:GITHUB_OUTPUT
- name: Determine npm cache directory non-windows
id: npm-cache-dir
if: runner.os != 'Windows'
run: echo "dir=$(npm config get cache)" >> $GITHUB_OUTPUT
- name: Restore cache using package-lock.json hash
uses: actions/cache@v3
if: ${{ !inputs.no-lockfile }}
with:
path: ${{ steps.npm-cache-dir-windows.outputs.dir || steps.npm-cache-dir.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Restore cache using package.json hash
uses: actions/cache@v3
if: ${{ inputs.no-lockfile }}
with:
path: ${{ steps.npm-cache-dir-windows.outputs.dir || steps.npm-cache-dir.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('package.json') }}
restore-keys: |
${{ runner.os }}-node-
- run: npm ci --no-audit --no-fund
if: ${{ inputs.npm-post-sync-script && !inputs.no-lockfile }}
env:
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
- run: npm install --no-audit --no-fund
if: ${{ inputs.npm-post-sync-script && inputs.no-lockfile }}
env:
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
- name: Run post-sync npm script
if: ${{ inputs.npm-post-sync-script }}
run: npm run ${{ inputs.npm-post-sync-script }}
# Inspired by https://github.com/actions/toolkit/blob/0db3029fcfcb1393a2b3ccd14caecd7a3460af4e/.github/workflows/update-github.yaml#L22-L23
# And by https://github.com/actions/deploy-pages/blob/ff669327f73994ba35f45d1cd2fc81d82d1c852c/.github/workflows/rebuild-dependabot-prs.yml#L43-L44
- name: Push all changes
id: push-changes
run: |
if [[ "$(git diff origin/${{ inputs.branch-name }} --stat)" != "" ]]; then
echo "::set-output name=createPR::true"
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git checkout -b ${{ inputs.branch-name }}
git add .
git commit -m "${{ inputs.commit-message }}"
git push origin ${{ inputs.branch-name }} -fu
fi
- name: Open a PR for the sync
if: ${{steps.push-changes.outputs.createPR}}
continue-on-error: true
run: gh pr create --head ${{ inputs.branch-name }} --title " ${{ inputs.pr-title }}" --body "${{ inputs.pr-body }}"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}