-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
344 lines (319 loc) · 11.6 KB
/
action.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
name: 'relock-conda'
description: 'GitHub action to relock conda environments using conda-lock'
author: Matthew R. Becker and the regro-cf-autotick-bot team
branding:
icon: 'rotate-cw'
color: 'green'
inputs:
environment-file:
description: 'the environment to (re)lock'
required: false
default: environment.yml
lock-file:
description: 'the lock file to update'
required: true
default: 'conda-lock.yml'
ignored-packages:
description: 'comma- or newline-separated list of packages whose version changes are ignored when relocking'
required: true
default: ''
include-only-packages:
description: >
comma-or newline-separated list of packages to exclusively include when relocking, all others will be ignored,
ignored-packages is applied to this list
required: true
default: ''
relock-all-packages:
description: 'whether to relock on an update to any package in the environment, not just those in the environment file'
required: true
default: false
github-token:
description: 'GitHub personal access token to use for making PRs'
required: true
action:
description: >
the action to take if the lock file is updated: `pr` will make a PR; `file` will leave the updated lock file
in the current working directory; `commit` will commit the updated lock file and push the changes
required: true
default: 'pr'
automerge:
description: 'whether to automatically merge the PR'
required: true
default: false
base-branch:
description: >
the base branch for PRs (See
[this documentation](https://github.com/peter-evans/create-pull-request/blob/main/docs/concepts-guidelines.md#events-which-checkout-a-commit)
for more details.)
head-branch:
description: 'the head branch for PRs'
required: true
default: 'relock-conda'
skip-if-pr-exists:
description: 'whether to skip relocking if a PR already exists'
required: true
default: false
draft:
description: 'whether to open the PR as a draft'
required: true
default: false
git-user-name:
description: >
the name to use for git commits; the default of '' attempts to infer the name
from the login associated with the GitHub token via the API. if this fails,
we use the value `github.actor` from the context.
required: false
default: ''
git-user-email:
description: >
the email to use for git commits; the default of '' attempts to infer the email
from the login associated with the GitHub token via the API. if this fails,
we use '${git-user-name}@users.noreply.github.com'.
required: false
default: ''
outputs:
pull-request-number:
description: "the number of the PR that was opened"
value: ${{ steps.pr.outputs.pull-request-number }}
relocked:
description: "whether the environment was relocked"
value: ${{ steps.set-output.outputs.relocked }}
summary:
description: "the summary of the relock operation"
value: ${{ steps.relock.outputs.summary }}
runs:
using: "composite"
steps:
# https://stackoverflow.com/a/73828715/1745538
- name: skip if PR already exists
id: check
shell: bash -leo pipefail {0}
run: |
# skip if PR already exists
if [[ "${EVENT_NAME}" == "issue_comment" ]]; then
if [[ "${HAS_RELOCK_SLUG}" == "false" || "${COMMENT_IS_PR}" == "false" ]]; then
res="true"
else
res="false"
fi
else
if [[ "${INPUT_ACTION}" != "pr" ]]; then
res="false"
else
prs=$(gh pr list \
--repo "$GITHUB_REPOSITORY" \
--head '${{ inputs.head-branch }}' \
--json title \
--jq 'map(select(.title == "relock w/ conda-lock")) | length')
if [[ ${prs} != "0" && ${SKIP_IF_PR_EXISTS} == "true" ]]; then
res="true"
else
res="false"
fi
fi
fi
echo "skip=${res}" >> "$GITHUB_OUTPUT"
env:
GH_TOKEN: ${{ inputs.github-token }}
SKIP_IF_PR_EXISTS: ${{ inputs.skip-if-pr-exists }}
INPUT_ACTION: ${{ inputs.action }}
EVENT_NAME: ${{ github.event_name }}
HAS_RELOCK_SLUG: ${{ startsWith(github.event.comment.body, '/relock-conda') }}
COMMENT_IS_PR: ${{ github.event.issue.pull_request != null }}
- name: react to issue comment
if: >-
steps.check.outputs.skip != 'true'
&& github.event_name == 'issue_comment'
&& startsWith(github.event.comment.body, '/relock-conda')
shell: bash -leo pipefail {0}
run: |
# react to issue comment
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
'/repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions' \
-f "content=rocket"
env:
GH_TOKEN: ${{ inputs.github-token }}
- name: setup conda-lock
if: ${{ steps.check.outputs.skip != 'true' }}
uses: mamba-org/setup-micromamba@068f1ab4b37ed9b3d9f73da7db90a0cda0a48d29 # v1
with:
environment-name: relock-env
create-args: >-
python
click
ruamel.yaml
conda
conda-lock
pygithub
condarc: |
channels:
- conda-forge
- name: checkout PR if running on an issue
if: >-
steps.check.outputs.skip != 'true'
&& github.event_name == 'issue_comment'
&& startsWith(github.event.comment.body, '/relock-conda')
shell: bash -leo pipefail {0}
run: |
# checkout PR if running on an issue
gh pr checkout --repo "$GITHUB_REPOSITORY" '${{ github.event.issue.number }}'
env:
GH_TOKEN: ${{ inputs.github-token }}
- name: relock
id: relock
if: ${{ steps.check.outputs.skip != 'true' }}
shell: bash -leo pipefail {0}
run: |
# relock
echo "::group::relock"
python ${{ github.action_path }}/relock.py \
--environment-file='${{ inputs.environment-file }}' \
--lock-file='${{ inputs.lock-file }}' \
--ignored-packages='${{ inputs.ignored-packages }}' \
--relock-all-packages='${{ inputs.relock-all-packages }}' \
--include-only-packages='${{ inputs.include-only-packages }}' \
> ${{ github.action_path }}/summary.txt
{
echo 'summary<<EOF'
cat ${{ github.action_path }}/summary.txt
echo EOF
} >> "$GITHUB_OUTPUT"
rm ${{ github.action_path }}/summary.txt
echo "::endgroup::"
- name: commit changes
if: >-
steps.check.outputs.skip != 'true'
&& steps.relock.outputs.env_relocked == 'true'
&& (
inputs.action == 'commit'
|| (
github.event_name == 'issue_comment'
&& startsWith(github.event.comment.body, '/relock-conda')
)
)
shell: bash -leo pipefail {0}
run: |
if [[ '${{ inputs.git-user-name }}' == '' ]]; then
login=$(gh api user --jq '.name' || echo '')
if [[ "${login}" == *"status\":\"4"* ]]; then
login=''
fi
if [[ "${login}" == "" || "${login}" == "null" ]]; then
login='${{ github.actor }}'
fi
else
login='${{ inputs.git-user-name }}'
fi
if [[ '${{ inputs.git-user-email }}' == '' ]]; then
email=$(gh api user --jq '.email' || echo '')
if [[ "${email}" == *"status\":\"4"* ]]; then
email=''
fi
if [[ "${email}" == "" || "${email}" == "null" ]]; then
email=${login}@users.noreply.github.com
fi
else
email='${{ inputs.git-user-email }}'
fi
echo "login: ${login}"
echo "email: ${email}"
git config set user.email ${email}
git config set user.name ${login}
if [[ '${{ github.event_name }}' == 'issue_comment' ]]; then
branch=$(gh pr view --json headRefName --jq '.headRefName')
repo=$(gh pr view --json headRepository --jq '.headRepository.name')
owner=$(gh pr view --json headRepositoryOwner --jq '.headRepositoryOwner.login')
else
branch=$(git rev-parse --abbrev-ref HEAD)
repo=$(basename -s .git `git config --get remote.origin.url`)
owner=$(dirname `git config --get remote.origin.url` | rev | cut -d'/' -f1 | rev)
fi
git add '${{ inputs.lock-file }}'
git commit -m 'relock w/ conda-lock'
git push https://x-access-token:${GH_TOKEN}@github.com/${owner}/${repo}.git ${branch}:${branch}
env:
GH_TOKEN: ${{ inputs.github-token }}
- name: open PR
id: pr
if: >-
steps.check.outputs.skip != 'true'
&& steps.relock.outputs.env_relocked == 'true'
&& inputs.action == 'pr'
&& github.event_name != 'issue_comment'
uses: peter-evans/create-pull-request@67ccf781d68cd99b580ae25a5c18a1cc84ffff1f
with:
commit-message: relock w/ conda-lock
title: relock w/ conda-lock
body: "This pull request relocks the dependencies with conda-lock.
${{ steps.relock.outputs.summary }}"
branch: ${{ inputs.head-branch }}
delete-branch: true
token: ${{ inputs.github-token }}
labels: dependencies
base: ${{ inputs.base-branch }}
draft: ${{ inputs.draft }}
- name: comment on issue
if: >-
steps.check.outputs.skip != 'true'
&& steps.relock.outputs.env_relocked == 'true'
&& (
inputs.action == 'commit'
|| (
github.event_name == 'issue_comment'
&& startsWith(github.event.comment.body, '/relock-conda')
)
)
shell: bash -leo pipefail {0}
run: |
# comment on issue
gh pr comment --repo "$GITHUB_REPOSITORY" --body "I've relocked the dependencies with conda-lock!
${{ steps.relock.outputs.summary }}" '${{ github.event.issue.number }}'
env:
GH_TOKEN: ${{ inputs.github-token }}
- name: automerge
if: >-
steps.check.outputs.skip != 'true'
&& inputs.automerge == 'true'
&& steps.relock.outputs.env_relocked == 'true'
&& steps.pr.outputs.pull-request-number != ''
&& inputs.action == 'pr'
&& github.event_name != 'issue_comment'
shell: bash -leo pipefail {0}
run: |
# automerge
gh pr merge --merge --auto '${{ steps.pr.outputs.pull-request-number }}'
env:
GH_TOKEN: ${{ inputs.github-token }}
- name: set outputs
id: set-output
if: always()
shell: bash -leo pipefail {0}
run: |
# set outputs
res=""
# if we skipped, we did not relock
if [[ "${res}" == "" && '${{ steps.check.outputs.skip }}' == 'true' ]]; then
res="false"
fi
# if the env was relocked, then we look at PR vs file
if [[ "${res}" == "" && '${{ steps.relock.outputs.env_relocked }}' == 'true' ]]; then
# for a PR, we need to know if it was opened
if [[ '${{ inputs.action }}' == 'pr' ]]; then
if [[ '${{ steps.pr.outputs.pull-request-number }}' != '' ]]; then
res="true"
else
res="false"
fi
else
# if not a PR, then we updated the file
res="true"
fi
fi
# if we get here, default to false
if [[ "${res}" == "" ]]; then
res="false"
fi
echo "relocked=${res}" >> "$GITHUB_OUTPUT"