Skip to content

Instantly share code, notes, and snippets.

@rattrayalex
Created June 28, 2021 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rattrayalex/d86b4c98bf70193e30f88dab2904da14 to your computer and use it in GitHub Desktop.
Save rattrayalex/d86b4c98bf70193e30f88dab2904da14 to your computer and use it in GitHub Desktop.
Running different CircleCI workflows based on environment variables created in a shell script
version: 2.1
setup: true
orbs:
continuation: circleci/continuation@0.1.2
jobs:
determine-workflow:
machine: true
steps:
- checkout
- run:
name: Generate parameters for main job
shell: /usr/bin/env bash -eo pipefail
command: |
env
export COMMIT_MESSAGES="$(git log stage.. --pretty=format:%B)";
if [[ "$CIRCLE_TAG" =~ ^v[0-9.]+$ ]]; then
WORKFLOW='prod'
elif [[ "$CIRCLE_BRANCH" =~ ^stage ]]; then
WORKFLOW='stage'
elif [[ "$COMMIT_MESSAGES" =~ deploy.demo ]]; then
WORKFLOW='demo'
else
WORKFLOW='dev'
fi
cat \<<- EOF > /home/circleci/main_parameters.json
{
"workflow": "$WORKFLOW"
}
EOF
cat /home/circleci/main_parameters.json
- continuation/continue:
configuration_path: .circleci/main.yml
parameters: /home/circleci/main_parameters.json
workflows:
version: 2
setup:
jobs:
- determine-workflow
version: 2.1
setup: false
parameters:
workflow:
type: enum
enum:
- prod
- stage
- demo
- dev
default: dev
jobs:
# ...
workflows:
version: 2
stage:
when:
equal: [stage, << pipeline.parameters.workflow >>]
jobs: # ...
prod:
when:
equal: [prod, << pipeline.parameters.workflow >>]
jobs: # ...
dev:
when:
or:
- equal: [dev, << pipeline.parameters.workflow >>]
- equal: [demo, << pipeline.parameters.workflow >>]
jobs: # ...
demo:
when:
equal: [demo, << pipeline.parameters.workflow >>]
jobs: # ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment