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

Conversation

rjblopes
Copy link

@rjblopes rjblopes commented Jul 29, 2023

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
Copy link

pvicente commented Oct 20, 2023

@Fernando-Abreu I would love if this is supported. It could resolve an issue that I've recently opened #92.

Any plans to add this support?
Many thanks

@rjblopes
Copy link
Author

rjblopes commented Jan 18, 2024

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

@pvicente

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants