-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
271 lines (253 loc) · 8.8 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
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
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
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@06375d89d211a1232ef63355742e9e2e564bc7f7 # 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 ${{ 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: |
# commit changes
python ${{ github.action_path }}/commit_lockfile.py \
--lock-file='${{ inputs.lock-file }}' \
--event-name='${{ github.event_name }}'
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@5e914681df9dc83aa4e4905692ca88beb2f9e91f
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: 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"