-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yaml
92 lines (81 loc) · 3.12 KB
/
action.yaml
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
name: "What changed"
description: "Action looks at changed files since last successful workflow run and determines what changed."
inputs:
files:
description: "Relevant files"
required: true
main-branch-name:
description: "Name of the main branch"
required: false
default: main
outputs:
changed:
description: "What changed: only-inputs, non-inputs, both"
value: ${{ steps.changed-files.outputs.changed }}
runs:
using: "composite"
steps:
- uses: "actions/checkout@v4"
with:
fetch-depth: 0
- name: Find start and end SHAs
uses: nrwl/nx-set-shas@v4
id: last-successful-commit-push
with:
main-branch-name: ${{ inputs.main-branch-name }}
- name: Determine what changed
id: changed-files
shell: python
env:
INPUT_BASE: ${{ steps.last-successful-commit-push.outputs.base }}
INPUT_HEAD: ${{ steps.last-successful-commit-push.outputs.head }}
INPUT_FILEPATHS: ${{ inputs.files }}
run: |
# Generated code below, DO NOT EDIT THIS LINE OR BELOW
import glob
import logging
import os
import subprocess
from dataclasses import dataclass
from itertools import chain
LOG = logging.getLogger(__name__)
@dataclass
class Options:
base: str
head: str
filepaths: str
def setup_logging():
logging.basicConfig(level=logging.DEBUG, format="::%(levelname)s::%(message)s")
# GitHub wants lowercase levelnames for workflow commands mapping
for level in logging.getLevelNamesMapping().values():
logging.addLevelName(level, logging.getLevelName(level).lower())
def main(options):
setup_logging()
cmd = ["git", "diff", "--name-only", options.base, options.head]
changed_files = set(subprocess.run(cmd, capture_output=True, text=True).stdout.splitlines())
LOG.debug("Changed files: %r", changed_files)
input_files = set(chain(*(glob.glob(f) for f in options.filepaths.split(","))))
LOG.debug("Input files: %r", input_files)
if changed_files.issubset(input_files):
LOG.info("Only inputs have been changed")
output = "only-inputs"
elif input_files.isdisjoint(changed_files):
LOG.info("No inputs have been changed")
output = "non-inputs"
else:
LOG.info("Both inputs and other files have been changed")
output = "both"
output_file = os.getenv("GITHUB_OUTPUT")
if output_file:
with open(output_file, "a") as f:
f.write(f"changed={output}\n")
else:
LOG.info("No GITHUB_OUTPUT environment variable found, printing to stdout")
LOG.info(f"changed={output}")
if __name__ == '__main__':
options = Options(
os.getenv("INPUT_BASE"),
os.getenv("INPUT_HEAD"),
os.getenv("INPUT_FILEPATHS"),
)
main(options)