How to force-run a periodic workflow?

I’m using CircleCI for periodic builds (3 a week), and it works great.

Except when there’s a issue and need to issue a fix. Since my CircleCI only triggers based on schedule, pushing the fix upstream doesn’t initiate a build.

If I manually choose ‘Rerun workflow’ from the dashboard, CircleCI unfortunately reuses the old commit (and not the most recent one).

So I likely need a job that triggers based on some condition. I thought about inspecting the commit message (as discussed here on the forum in other threads, like here). But I’m hopelessly stuck making that work.

:arrow_right: If I don’t a .circleci.yml approach, let me know since that’s the quickest approach.

Here’s what I tried so far:

Use a bash variable

  implement_fix:
    docker:
    - image: cibuilds/hugo:0.54.0
    steps:
    - checkout
    - run:
        name: Check if commit includes [fix]
        command: |
          CMT=git log --format=oneline -n 1 $CIRCLE_SHA1
          echo $CMT

Output:

CMT=git log --format=oneline -n 1 $CIRCLE_SHA1
echo $CMT

/bin/bash: log: command not found
Exited with code 127

Perhaps a quoted variable?

  implement_fix:
    docker:
    - image: cibuilds/hugo:0.52
    steps:
    - checkout
    - run:
        name: Check if commit includes [fix]
        command: |
          CMT="git log --format=oneline -n 1 $CIRCLE_SHA1"
          echo $CMT
echo $CMT

git log --format=oneline -n 1 381a054044c20bbfb475087d547d089497fccd39

Using bash export, following the docs

https://circleci.com/docs/2.0/env-vars/

  implement_fix:
    docker:
    - image: cibuilds/hugo:0.54.0
    steps:
    - checkout
    - run:
        name: Check if commit includes [fix]
        command: |
          echo 'export CMT_MSG=git log --format=oneline -n 1 $CIRCLE_SHA1' >> $BASH_ENV
    - run:
        name: 'Commit message is:'
        command: echo $CMT_MSG

Woops, neither works:

/tmp/.bash_env--0-build: line 1: export: `--format=oneline': not a valid identifier
/tmp/.bash_env--0-build: line 1: export: `-n': not a valid identifier
/tmp/.bash_env--0-build: line 1: export: `1': not a valid identifier
/tmp/.bash_env--0-build: line 1: export: `6db97969c361b9858c028a57bd41507c8adedcd3': not a valid identifier
git

Using an environment variable, following this thread

  implement_fix:
    docker:
    - image: cibuilds/hugo:0.54.0
    environment:
    - GIT_COMMIT_DESC: git log --format=oneline -n 1 $CIRCLE_SHA1
    steps:
    - checkout
    - run:
        name: 'Commit message is:'
        command: echo $GIT_COMMIT_DESC

Output:

echo $GIT_COMMIT_DESC

git log --format=oneline -n 1 $CIRCLE_SHA1

Use when command, from the docs

https://circleci.com/docs/2.0/configuration-reference/#the-when-attribute

implement_fix:
docker:
    - image: cibuilds/hugo:0.54.0
environment:
    GIT_COMMIT_DESC: $(git log --format=oneline -n 1 $CIRCLE_SHA1)
steps:
    - checkout
    - run:
        name: "Set environment variable"
        command: echo $GIT_COMMIT_DESC
    - when:
        condition:
        steps:
        - run:
            name: "Successful match of condition"
            command: echo "Success!"

Doesn’t work for me; output is the following instead of the actual commit message:

$(git log --format=oneline -n 1 $CIRCLE_SHA1)

I made a bit ‘progress’ this afternoon. My CircleCI job is now:

implement_fix:
docker:
    - image: cibuilds/hugo:0.54.0
steps:
    - checkout
    - run:
        name: "Set environment variable"
        command: |
        echo "export COMMIT_MESSAGE=\"$(git log --format=oneline -n 1 $CIRCLE_SHA1)\"" >> $BASH_ENV
        echo ${COMMIT_MESSAGE}
        echo 'export CONTINUE_BUILD=\"$([[ ${COMMIT_MESSAGE} == *_"[fix]"_* ]])\"' >> $BASH_ENV
        echo ${CONTINUE_BUILD}
    - run:
        name: "test condition"
        command: |
        if [[ ${COMMIT_MESSAGE} == *_"[fix]"_* ]]; then
            echo "Found [fix] in commit message"
        else
            echo "did not found the prerequisite in the message"
        fi
    - when:
        condition: "$CONTINUE_BUILD"
        steps:
        - run:
            name: "Successful match of condition"
            command: echo "Success!"

The problem that I have is that my ‘test condition’ step shows the correct true/false value, but my when step runs every time. Which should of course not happen; only when there’s a true value.

What am I missing?

It wasn’t initially clear to me what you are after. It sounds like you want to be able to trigger a manual build after a scheduled build fails, and that your standard build on code push does something different.

If so, would a tag push be OK? I use that for a variety of non-standard build cases, and it works great.

A tag push sounds like a good idea! Thanks for thinking along. :slight_smile:

Unfortunately, I can neither make this work. I studied the doc and came up with:

  deploy-fix:
    jobs:
      - build:
          filters:
            tags:
              only: /.*fix.*/

My idea was to have this task run whenever there’s a commit tagged with ‘fix’ in it. I haven’t used Java regex before, but did work with other languages’ regex and the Java doc is clear to me.

But this ‘deploy-fix’ task runs every time I push something to CircleCI. Without that commit even having a tag.

I’m not sure why because it’s literal the same example as the docs use. Except the regex is different, but that should only trigger against tag names I use, and I have to use one.

Yes, it is a bit fiddly. The docs do try to help on this point - I think you need an ignore statement as well. See my example here. It uses YAML references, but it parses to exactly the same thing as if the filters were inline.

Thanks, I got it working from your example!

Here’s the code I end up using for reference and others in the future:

  deploy-fix:
    jobs:
      - build:
          filters:
            tags:
              only: /.*fix.*/
            branches:
              ignore: /.*/

Thanks again and have a nice weekend. :slight_smile: