Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic base-revision #89

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/commands/set-parameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ parameters:
default: "main"
description: >
The revision to compare the current one against for the purpose
of determining changed files.
of determining changed files. Supports passing environment variables,
eg. $CIRCLE_BRANCH, or shell script evaluating to a string, eg. $(echo "dev").
mapping:
type: string
default: ""
Expand Down
15 changes: 14 additions & 1 deletion src/jobs/filter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ parameters:
default: "main"
description: >
The revision to compare the current one against for the purpose
of determining changed files.
of determining changed files. Supports passing environment variables,
eg. $CIRCLE_BRANCH, or shell script evaluating to a string, eg. $(echo "dev").
mapping:
type: string
default: ""
Expand Down Expand Up @@ -59,6 +60,10 @@ parameters:
description: >
Pick a specific cimg/python image variant:
https://hub.docker.com/r/cimg/python/tags
before_set_params_script:
default: ""
description: Script to run before set-parameters step. Used to load custom environment variables.
type: string

steps:
- checkout
Expand All @@ -69,6 +74,14 @@ steps:
steps:
- attach_workspace:
at: << parameters.workspace_path >>
- when:
condition:
not:
equal: ["", << parameters.before_set_params_script >>]
steps:
- run:
name: Before set-parameters script
command: << parameters.before_set_params_script >>
- set-parameters:
base-revision: << parameters.base-revision >>
mapping: << parameters.mapping >>
Expand Down
10 changes: 10 additions & 0 deletions src/scripts/create-parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def merge_base(base, head):
capture_output=True
).stdout.decode('utf-8').strip()

def eval_base(base):
if base.startswith('$'):
value = subprocess.run(
['sh', '-c', f"echo {base}"],
capture_output=True
).stdout.decode('utf-8').strip()
return 'main' if value == '' else value
return base

def parent_commit():
return subprocess.run(
['git', 'rev-parse', 'HEAD~1'],
Expand Down Expand Up @@ -112,6 +121,7 @@ def is_mapping_line(line: str) -> bool:
return not (is_comment_line or is_empty_line)

def create_parameters(output_path, config_path, head, base, mapping):
base = eval_base(base) # Evaluate base revision if it is an environment variable or script
checkout(base) # Checkout base revision to make sure it is available for comparison
checkout(head) # return to head commit
base = merge_base(base, head)
Expand Down