# \[Workaround\] Using scheduled workflows with Dynamic config

**URL:** <https://discuss.circleci.com/t/workaround-using-scheduled-workflows-with-dynamic-config/40344>\
**Category:** Support Solutions\
**Tags:** dynamic-config\
**Created:** [June 11, 2021, 5:45pm UTC](https://discuss.circleci.com/t/workaround-using-scheduled-workflows-with-dynamic-config/40344 "2021-06-11T17:45:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbialostosky](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/nbialostosky/32/24962_2.png) [@nbialostosky](https://discuss.circleci.com/u/nbialostosky)\
**Post date:** [June 11, 2021, 5:45pm UTC](https://discuss.circleci.com/t/workaround-using-scheduled-workflows-with-dynamic-config/40344/1 "2021-06-11T17:45:10Z")

</div>

# [Workaround] Using scheduled workflows with Dynamic config

We recently released a feature called [Dynamic config](https://circleci.com/docs/2.0/dynamic-config/) that unlocks a lot of options in using CircleCI. However, at the moment this feature is unable to be used in conjunction with [scheduled workflows](https://circleci.com/docs/2.0/workflows/#scheduling-a-workflow). The team is actively working on addressing this to allow for them to be used together. Since there isn’t a specific date of that release this guide is to help ensure both features can be used at the same time while that is being worked on.

This guide will walk through setting up a secondary repository that will _not_ have dynamic config enabled. It will simply contain a `config.yml` file that has scheduled workflows. Those workflows will execute a job that sends an API call to trigger a build at the repository that has dynamic config enabled, allowing for builds to be triggered on a specific cron schedule.

### Requirements

1. This guide utilizes Pipeline parameters, so you will need to be on at least version `2.1`
2. This guide requires the creation of a new repository, you will need the proper permissions at your organization to do that

### Create new ‘schedule’ repository

First step will create a new repository, ideally under the same organization where you have your main repository you want to have scheduled workflows on.

Within that new repository you will create a directory called `.circleci` at the root of the project. Within that directory create a file called `config.yml`.

The contents of `config.yml` will be similar to the following:

```auto
version: 2.1

jobs:
  run-scheduled-workflow:
    docker:
      - image: cimg/base:stable
    steps:
      - run:
          name: Run scheduled workflow
          command: |
            curl --location --request POST 'https://circleci.com/api/v2/project/(vcs)/(org)/(project)/pipeline' \
            --header 'Content-Type: application/json' \
            -u "${CIRCLE_TOKEN}:" \
            --data-raw '{
              "branch": "main",
                "parameters": {
                  "scheduled-workflow": true
              }
            }' 
      
workflows:
  scheduled-workflow:
    triggers:
       - schedule:
           cron: "30 9 * * *"
           filters:
             branches:
               only:
                - main
    jobs:
      - run-scheduled-workflow

```

You will want to modify the following items:

1. The URL of the curl you need to change `(vcs)` to either `github` or `bitbucket`, `(org)` to the organization where your dynamic config repo lives, `(project)` to the repository of your dynamic config project.
2. In the `--data-raw` section adjust `"branch": "main"` to the branch where you want to run the scheduled workflow.
3. In the `--data-raw` section add any additional parameters you may need/want to pass along.
4. Adjust the `cron: "30 9 * * *"` to be the [schedule in which you want to run on](https://circleci.com/docs/2.0/workflows/#scheduling-a-workflow).

Once the above changes are made and committed to the repository, [set this project up in CircleCI](https://circleci.com/docs/2.0/projects/).

### Configure Environment Variables

The curl in the job requires a personal API key to successfully trigger the workflow. This needs to be stored as a variable and can be done at the [project level](https://circleci.com/docs/2.0/env-vars/#setting-an-environment-variable-in-a-project), or, if you plan to use it across projects – [set these up in a context](https://circleci.com/docs/2.0/env-vars/#setting-an-environment-variable-in-a-context).

| Variable Name | Description |
| --- | --- |
| CIRCLE\_TOKEN | This will be a [personal API token](https://circleci.com/docs/2.0/managing-api-tokens/#creating-a-personal-api-token). |

Note: The personal API token used for the curl needs to have write access to the dynamic config repository, which means as long as the token is under your user and you have write access, it will work.

### Add ‘scheduled-workflow’ parameter to dynamic config

Now that we have the 2nd repository setup and have it running on a schedule, we need to ensure our dynamic config knows how to act when it receives the API call.

You will need to add `scheduled-workflow` as a [pipeline parameter](https://circleci.com/docs/2.0/pipeline-variables/#pipeline-parameters-in-configuration), like:

```auto
version: 2.1

setup: true

parameters:
  scheduled-workflow:
    type: boolean
    default: false

```

Next specify a new workflow that will only execute when `scheduled-workflow` is `true`:

```auto
workflows:
  version: 2
  scheduled-workflow:
    when: << pipeline.parameters.scheduled-workflow >>
    jobs:
      - schedule-job

```

Set the `jobs` to be the jobs you want to run on a schedule. If you don’t want your other workflows to trigger when this API call happens you will also need to add the following condition to the rest of your workflows:

```auto
when:
  equal: [false, << pipeline.parameters.scheduled-workflow >>]

```

### Resources

1. Here is a `config.yml` [example for the schedule repository](https://gist.github.com/nbialostosky/57373b3a548dd24701f315da8fa8c076).
2. Here is a `config.yml` [example for the dynamic config repository](https://gist.github.com/nbialostosky/9c80a7cb0bac7b77a0a3d90fd87a8e9d).

---

<div class="post-metadata">

**Author:** ![wenisman](https://avatars.discourse-cdn.com/v4/letter/w/ed8c4c/32.png) [@wenisman](https://discuss.circleci.com/u/wenisman)\
**Post date:** [July 5, 2021, 3:47am UTC](https://discuss.circleci.com/t/workaround-using-scheduled-workflows-with-dynamic-config/40344/2 "2021-07-05T03:47:53Z")

</div>

This example does not actually work, this is because any configuration with the **setup** stanza (ie setup: true) can only have a single workflow. Please see this support ticket:

> **["Max number of workflows exceeded." error](https://support.circleci.com/hc/en-us/articles/360060934851--Max-number-of-workflows-exceeded-error)**
>
> CircleCI Dynamic Configs, formally known as Setup Workflows, allows for a maximum of 1 workflows in the initial setup workflow. So if \`setup: true\` is set then the following config would be invalid...

if you want to do this you will need to use a continuation orb to call a scheduled job configuration file with a pipeline parameter set. The Dynamic Config will then look like

```auto
version: 2.1

setup: true

parameters:
  scheduled-workflow:
    type: boolean
    default: false

orbs:
  continuation: circleci/continuation@0.1.2

jobs:
  execute_workflow:
    docker:
      - image: cimg/base:stable
    steps:
      - when: << pipeline.parameters.scheduled-workflow >>
        steps:
          - continuation/continue:
              configuration_path: .circleci/scheduled_workflow.yml
    - when: 
        not: << pipeline.parameters.scheduled-workflow >>
        steps:
          - continuation/continue:
              configuration_path: .circleci/regular_workflow.yml

workflows:
  setup:
    jobs:
      - execute_workflow

```

secondly you cannot use the default `${CIRCLE_TOKEN}`, this will not let you execute the pipeline of a private pipeline. You will need to create a new API token and use that in the curl request. I suggest editing the curl request to be

```auto
OUTPUT=$(curl -X POST 'https://circleci.com/api/v2/project/{project_slug}/pipeline' \
 -H "Content-Type: application/json" \
 -H "Circle-Token: ${CIRCLECI_API_TOKEN}" \
 -d '{"branch": "main", "parameters": {"scheduled-workflow": true} }')

echo "${OUTPUT}" 

```

as this will print the ID of the pipeline and you can then curl  
` https://circleci.com/api/v2/pipeline/{pipeline-id}`  
and see any errors that occur

---

<div class="post-metadata">

**Author:** ![dragosMC91](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/dragosmc91/32/27561_2.png) [@dragosMC91](https://discuss.circleci.com/u/dragosMC91)\
**Post date:** [August 1, 2021, 1:53pm UTC](https://discuss.circleci.com/t/workaround-using-scheduled-workflows-with-dynamic-config/40344/3 "2021-08-01T13:53:12Z")

</div>

Hello, I am interested in dynamic configs since they will solve a specific problem in my project but I also need a few scheduled workflows which the project is dependent on.

Has any of the approaches mentioned here been tested ? I would hate to invest time in it and find out it’s not working in the end.

Thanks in advance !

---

<div class="post-metadata">

**Author:** ![wenisman](https://avatars.discourse-cdn.com/v4/letter/w/ed8c4c/32.png) [@wenisman](https://discuss.circleci.com/u/wenisman)\
**Post date:** [August 16, 2021, 5:37am UTC](https://discuss.circleci.com/t/workaround-using-scheduled-workflows-with-dynamic-config/40344/4 "2021-08-16T05:37:14Z")

</div>

Hi @dragosMC91,  
yes we are using this exact method, and so this is a tested solution at the time of writing.

Regards  
W

---

<div class="post-metadata">

**Author:** ![dragosMC91](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/dragosmc91/32/27561_2.png) [@dragosMC91](https://discuss.circleci.com/u/dragosMC91)\
**Post date:** [August 16, 2021, 3:42pm UTC](https://discuss.circleci.com/t/workaround-using-scheduled-workflows-with-dynamic-config/40344/5 "2021-08-16T15:42:47Z")

</div>

In the meantime I have implemented the same idea and it worked fine so far. Thank you for the valuable information @wenisman . Good luck!

---

<div class="post-metadata">

**Author:** ![ahocquard](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/ahocquard/32/28208_2.png) [@ahocquard](https://discuss.circleci.com/u/ahocquard)\
**Post date:** [September 27, 2021, 8:23am UTC](https://discuss.circleci.com/t/workaround-using-scheduled-workflows-with-dynamic-config/40344/6 "2021-09-27T08:23:46Z")

</div>

Any news about the support of schedule workflows with dynamic config? It was released 4 months ago, and the workaround for us is to use Github Action to trigger Circle Ci workflows…  
Is there any ticket where we can vote?

Thanks

---

<div class="post-metadata">

**Author:** ![nbialostosky](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/nbialostosky/32/24962_2.png) [@nbialostosky](https://discuss.circleci.com/u/nbialostosky)\
**Post date:** [September 27, 2021, 1:33pm UTC](https://discuss.circleci.com/t/workaround-using-scheduled-workflows-with-dynamic-config/40344/7 "2021-09-27T13:33:14Z")

</div>

Hi @ahocquard !

You can indeed vote here:

> **[Dynamic config (aka: Setup Jobs) | Cloud Feature Requests | CircleCI Ideas](https://circleci.canny.io/cloud-feature-requests/p/dynamic-config-aka-setup-jobs)**

The above is tracking the full completion of this feature, which entails implementation of a way to schedule workflows/pipelines with dynamic configs. If you vote on the above you’ll receive automatic updates on the progress of the feature. I will note that it’s something actively being worked on and we should have further updates shortly.

Thanks!

---

<div class="post-metadata">

**Author:** ![nbialostosky](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/nbialostosky/32/24962_2.png) [@nbialostosky](https://discuss.circleci.com/u/nbialostosky)\
**Post date:** [October 28, 2021, 6:34pm UTC](https://discuss.circleci.com/t/workaround-using-scheduled-workflows-with-dynamic-config/40344/8 "2021-10-28T18:34:00Z")

</div>

As an update on this workaround, we just launched ‘Scheduled Pipelines’ into Open Preview:

> [@Scheduled Pipelines Are Here](https://discuss.circleci.com/t/scheduled-pipelines-are-here/41684):
>
> Update 6-Dec-2022: We previously announced that we will be sunsetting scheduled workflows at the end of this year. Since that announcement went live, we’ve been closely tracking your feedback and feature requests, and have decided to postpone the deprecation - no new timelines are set yet. We know that there is more work we need to do in order to improve the existing Scheduled Pipelines experience and also make migration easier for all. We will share updates as we have them. Thanks for your pati…

This feature works with Dynamic Configs/Setup workflows and should allow you to continue to run pipelines on a specific schedule.

Hope the above helps!  
-Nick

---

<div class="post-metadata">

**Author:** ![nbialostosky](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/nbialostosky/32/24962_2.png) [@nbialostosky](https://discuss.circleci.com/u/nbialostosky)\
**Post date:** [November 7, 2021, 6:34pm UTC](https://discuss.circleci.com/t/workaround-using-scheduled-workflows-with-dynamic-config/40344/9 "2021-11-07T18:34:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kami](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/kami/32/35940_2.png) [@kami](https://discuss.circleci.com/u/kami)\
**Post date:** [July 10, 2024, 2:46pm UTC](https://discuss.circleci.com/t/workaround-using-scheduled-workflows-with-dynamic-config/40344/10 "2024-07-10T14:46:38Z")

</div>


