CircleCI-Public / path-filtering-orb

MIT License
24 stars 62 forks source link

Add dynamic base-revision #89

Open rjblopes opened 1 year ago

rjblopes commented 1 year ago

This PR adds dynamic base-revision feature, supporting use-cases such as the issue discussed here: #65

It's achieved by adding base-revision eval step before the merge in the python script. Can be used with environment variables or sub-shell scripts evaluating to string.

I tested added method with attached config file, which returned this output:

Environment Variables
PARAM=${CIRCLE_BRANCH:-dev}
PR_BASE=feature/hot

Running python script...
Env base: ${CIRCLE_BRANCH:-dev}
Computed base: test-ci-trigger
Done!

Environment Variables
PARAM=$PR_BASE
PR_BASE=feature/hot

Running python script...
Env base: $PR_BASE
Computed base: feature/hot
Done!

Environment Variables
PARAM=$(echo "staging")
PR_BASE=feature/hot

Running python script...
Env base: $(echo "staging")
Computed base: staging
Done!

Environment Variables
PARAM=main
PR_BASE=feature/hot

Running python script...
Env base: main
Computed base: main
Done!

Config.yml

yaml
version: 2.1

commands:
  print:
    parameters:
      param:
        type: string
    steps:
      - run:
          name: Eval Param
          command: |
            echo 'Environment Variables'
            echo PARAM=$PARAM 
            echo PR_BASE=$PR_BASE
            echo ''

            echo "${TEST_SCRIPT}" > temp_test_script.py

            echo 'Running python script...'
            python3 temp_test_script.py

          environment:
            PARAM: << parameters.param >>
            TEST_SCRIPT: |
              import os
              import subprocess

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

                return base

              print(f"Env base: {os.environ.get('PARAM')}")
              print(f"Computed base: {eval_base(os.environ.get('PARAM'))}")
              print("Done!")

jobs:
  print-base:
    machine: true
    parameters:
      pr-base1:
        type: string
        default: main
      pr-base2:
        type: string
        default: main
      pr-base3:
        type: string
        default: main
      pr-base4:
        type: string
        default: main
    steps:
      - run:
          name: Fetch PR Base Branch
          command: |
            echo 'export PR_BASE=feature/hot' >> "$BASH_ENV"
      - print:
          param: << parameters.pr-base1 >>
      - print:
          param: << parameters.pr-base2 >>
      - print:
          param: << parameters.pr-base3 >>
      - print:
          param: << parameters.pr-base4 >> # test named ref using default value

workflows:
  my-workflow:
    jobs:
      - print-base:
          pr-base1: ${CIRCLE_BRANCH:-dev} # test shell env var with default
          pr-base2: $PR_BASE # test custom env var
          pr-base3: $(echo "staging") # test script which evaluates to string
pvicente commented 1 year ago

@Fernando-Abreu I would love if this is supported. It could resolve an issue that I've recently opened https://github.com/CircleCI-Public/path-filtering-orb/issues/92.

Any plans to add this support? Many thanks

rjblopes commented 10 months ago

For anyone needing this, the fork is available here: 🚀

@pvicente