# Conditional steps using when

**URL:** <https://discuss.circleci.com/t/conditional-steps-using-when/47324>\
**Category:** Build Environment\
**Created:** [March 3, 2023, 7:31pm UTC](https://discuss.circleci.com/t/conditional-steps-using-when/47324 "2023-03-03T19:31:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsorelli-hedgepoint](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rsorelli-hedgepoint/32/30808_2.png) [@rsorelli-hedgepoint](https://discuss.circleci.com/u/rsorelli-hedgepoint)\
**Post date:** [March 3, 2023, 7:31pm UTC](https://discuss.circleci.com/t/conditional-steps-using-when/47324/1 "2023-03-03T19:31:33Z")

</div>

Hello folks,

I’m trying to run some conditional steps using when to control when running or not a deployment step on my workflow as folllow.

```auto
version: 2.1

workflows: 
  ci-cd-production:
    jobs:
      - build
      - deploy:
          requires:
            - build

jobs:

  build:
    docker: 
      - image: cimg/node:lts-browsers
    steps:
      - checkout
      - run: echo "Run build Job"

  deploy:
    docker: 
      - image: cimg/node:lts-browsers
    steps:
      - run:
          name: 'Checks if its a Pull Request (PR)'
          command: |
            if [-n "$CIRCLE_PULL_REQUEST"]; then
              isPullRequest=true
            else
              isPullRequest=false
            fi
            echo isPullRequest: $isPullRequest
            echo export ISPULLREQUEST=$isPullRequest >> "$BASH_ENV"
      - when:
          condition: 
            equal: [true, $isPullRequest]
          steps:
            - run: echo "DEPLOY NOT EXECUTED"
      - when:
          condition:
            equal: [false, $isPullRequest]
          steps:
            - run: echo "DEPLOYING APPLICATION..."

```

For some reason my when condition to identify if it’s a PR or not is not working.  
Both conditions true and false are not executed when the workflow is loaded.

Anyone has any hint to help?

Thanks in advance,

---

<div class="post-metadata">

**Author:** ![kelvintaywl](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/kelvintaywl/32/28141_2.png) [@kelvintaywl](https://discuss.circleci.com/u/kelvintaywl)\
**Post date:** [March 4, 2023, 3:20pm UTC](https://discuss.circleci.com/t/conditional-steps-using-when/47324/2 "2023-03-04T15:20:49Z")

</div>

Hi @rsorelli-hedgepoint ,

The when condition expression will not evaluate runtime environment variables.  
As such the above expressions will unfortunately not work.

You can however rely on simple bash if-else expression then.

```auto
- run: |
   if […]; then
     echo true
   else
     echo false
   fi

```

Hope that clarifies your question 🙂

---

<div class="post-metadata">

**Author:** ![rsorelli-hedgepoint](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rsorelli-hedgepoint/32/30808_2.png) [@rsorelli-hedgepoint](https://discuss.circleci.com/u/rsorelli-hedgepoint)\
**Post date:** [March 6, 2023, 12:12pm UTC](https://discuss.circleci.com/t/conditional-steps-using-when/47324/3 "2023-03-06T12:12:26Z")

</div>

Hi @kelvintaywl ,

Thanks for your reply and explanation.  
Considering your points, is there any way I can trigger a step or even a workflow when it’s a PR or not a PR.

Long story short what I meant to do is:

if it’s a PR, than run build and test  
if it’s not a PR, than run build, test and deploy

Best regards,

---

<div class="post-metadata">

**Author:** ![rit1010](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rit1010/32/34738_2.png) [@rit1010](https://discuss.circleci.com/u/rit1010)\
**Post date:** [March 6, 2023, 3:17pm UTC](https://discuss.circleci.com/t/conditional-steps-using-when/47324/4 "2023-03-06T15:17:15Z")

</div>

Which repo platform are you using as there are a number of parameters set based on the repo that you can use in condition statements as these are known at the time that the circle.yml script is processed.

```
 https://circleci.com/docs/pipeline-variables/

```

Depending on how you are using the repo and what processes you have in place using tags to pass information to the circleci script is quite common.

---

<div class="post-metadata">

**Author:** ![rsorelli-hedgepoint](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rsorelli-hedgepoint/32/30808_2.png) [@rsorelli-hedgepoint](https://discuss.circleci.com/u/rsorelli-hedgepoint)\
**Post date:** [March 6, 2023, 3:36pm UTC](https://discuss.circleci.com/t/conditional-steps-using-when/47324/5 "2023-03-06T15:36:39Z")

</div>

Hi @rit1010 ,

I’m using Github.

AFAIK according to CicleCI [documentation](https://circleci.com/docs/variables/) the way to identify a pull request is thru the `CIRCLE_PULL_REQUEST` environment variable.

That’s why I’m recovering it from a “run command” and trying to use to manage the workflow/step execution. But as mentioned by kelvin it can’t be done.

I need to find a workarround to manage that and fit to my CI/CD pipeline.

Thanks,

---

<div class="post-metadata">

**Author:** ![rit1010](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rit1010/32/34738_2.png) [@rit1010](https://discuss.circleci.com/u/rit1010)\
**Post date:** [March 6, 2023, 3:40pm UTC](https://discuss.circleci.com/t/conditional-steps-using-when/47324/6 "2023-03-06T15:40:37Z")

</div>

Also, there is another possible option, but I have never had a need to try it so my knowledge is limited.

Circleci has continuation and dynamic-continuation orbs which allow the execution of a circleci yml script via the API. These would allow a simple main config.yml script to pull any environment variables you need to process into a parameter list that is then passed onto the yml script that does all the real work. As the values are available as parameters at the time the script is processed they should be useable in conditional commands.

---

<div class="post-metadata">

**Author:** ![rsorelli-hedgepoint](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rsorelli-hedgepoint/32/30808_2.png) [@rsorelli-hedgepoint](https://discuss.circleci.com/u/rsorelli-hedgepoint)\
**Post date:** [March 6, 2023, 5:51pm UTC](https://discuss.circleci.com/t/conditional-steps-using-when/47324/7 "2023-03-06T17:51:31Z")

</div>

Interesting point !  
I’ll read the documentation and try to figure out how it can help in my scenario.

Thanks !

---

<div class="post-metadata">

**Author:** ![rit1010](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rit1010/32/34738_2.png) [@rit1010](https://discuss.circleci.com/u/rit1010)\
**Post date:** [March 6, 2023, 7:17pm UTC](https://discuss.circleci.com/t/conditional-steps-using-when/47324/8 "2023-03-06T19:17:47Z")

</div>

The best part of the docs can be found here

> **[Using dynamic configuration - CircleCI](https://circleci.com/docs/using-dynamic-configuration/)**
>
> A how to guide for using CircleCI dynamic configuration.

One thing I do know after a bit of playing around is when using the parameters parameter the JSON object is in fact a JSON object within a JSON string - so you put it in single quotes.

---

<div class="post-metadata">

**Author:** ![wyardley](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/wyardley/32/19231_2.png) [@wyardley](https://discuss.circleci.com/u/wyardley)\
**Post date:** [March 7, 2023, 5:17am UTC](https://discuss.circleci.com/t/conditional-steps-using-when/47324/9 "2023-03-07T05:17:55Z")

</div>

> [@rsorelli-hedgepoint](#):
>
> if it’s a PR, than run build and test  
> if it’s not a PR, than run build, test and deploy

May I make a suggestion?  
I think you’re maybe overcomplicating it a little, and I also don’t think there’s a way to “fix” the above to work the way I think you want it to.

Also, if you don’t have “only build pull requests” set, you could also run into a deploy when someone pushes a commit to a random branch without making a PR, which I doubt you want? Do you have a large number of possible branches that could be getting deployed off of, and if so, do the branchnames have a predictable pattern?

IMO, your best bet is to deploy off either a cut tag, or on push to one or more branches or branch patterns. Then you can do something like

```yaml
workflows:
  version: 2
  build-and-deploy:
    jobs:
      - checkout
      - lint:
          requires:
            - checkout
      - build:
          requires:
            - checkout
      - deploy:
          requires:
            - lint
            - build
          # Only deploy on pushes to "main" and "develop" branches
          filters:
            branches:
              only:
                # Note: filters for branches or tags can also be regexes, so 
                # you can get pretty elaborate here
                - main
                - develop

```

A better in some ways, but slightly more complex, option, would be to have something like semantic-release that runs on pushes to `main`, and cuts a tag when a release is needed, then have a _separate_ workflow that deploys off that tag once it’s cut.

Is there a reason something like that won’t work for you?

All that said, I think even if you want to go the original route, it might be easier to have a single deploy step and have that in an `if` condition based directly on `CIRCLE_PULL_REQUEST` vs trying to define and redefine 3-4 different env vars (none of which will be accessible to Circle when it runs that conditional logic). That will work, whereas I could be wrong, but pretty sure the `when` condition (which IIRC is evaluated before your job even runs) won’t have any env vars (whether they’re in `$BASH_ENV` or not) in scope / available.

So, again, I personally wouldn’t recommend this, and see suggestions above, but something like this might work.

```yaml
  deploy:
    steps:
      - run:
          name: deploy
          command: |
            if [-n "$CIRCLE_PULL_REQUEST"]; then
              echo "Not deploying"
            else
              echo "Deploying..."
              do some other stuff here
            fi              

```

In theory, you could use pipeline parameters ([Project values and variables - CircleCI](https://circleci.com/docs/variables/#pipeline-values)), however `pipeline.trigger_parameters.circleci.event_type` seems to be GitLab specific, so depending on your VCS provider, this might not be an option.

---

<div class="post-metadata">

**Author:** ![rsorelli-hedgepoint](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rsorelli-hedgepoint/32/30808_2.png) [@rsorelli-hedgepoint](https://discuss.circleci.com/u/rsorelli-hedgepoint)\
**Post date:** [March 7, 2023, 1:44pm UTC](https://discuss.circleci.com/t/conditional-steps-using-when/47324/10 "2023-03-07T13:44:53Z")

</div>

@wyardley

Thanks for your sugestion.  
Filtering by branch worked like a charm.  
I really appreciate your support and time.

Best,

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [March 14, 2023, 1:45pm UTC](https://discuss.circleci.com/t/conditional-steps-using-when/47324/11 "2023-03-14T13:45:52Z")

</div>

Closed on March 14, 2023, 1:45pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
