How to restrain CI/CD runs

I’ve got a Gitlab pages site, and with the upcoming limited on pipeline time, I’m trying to set it so that CICD only runs on:

  • Master
  • If I put a specific string into the commit message
  • Whatever is necessary to make the merge requests happy

It’s breaking my merge requests in two ways:

  • If that string was in the last commit, there are two pipelines created (on in a detacted state).
  • if that string was not in the last commit, the merge request just spins trying to get information about the last pipeline.

Based on my reading, it sounds like this second pipeline is being created by the merge request. Is there a right way to do this?

My CICD file is:
workflow:
rules:
- if: $CI_MERGE_REQUEST_ID
when: always
- if: $CI_COMMIT_MESSAGE =~ /[CICD]/
when: always
- if: $CI_COMMIT_BRANCH == “master”
when: always

stages:
    - build
    - test
    - deploy
    - post

build:
    image: ruby:latest
    stage: build
    cache:
        paths:
            - vendor/
    script:
        - apt update -y
        - apt upgrade -y
        - apt-get install -y locales
        - echo "en_US UTF-8" > /etc/locale.gen
        - locale-gen en_US.UTF-8
        - export LANG=en_US.UTF-8
        - export LANGUAGE=en_US:en
        - export LC_ALL=en_US.UTF-8
        - bundle config set path 'vendor'
        - bundle install
        - bundle exec jekyll build -d test
    artifacts:
        paths:
            - test
        expire_in: 20 minutes
    variables:
        JEKYLL_ENV: production
        #https://blog.sparksuite.com/7-ways-to-speed-up-gitlab-ci-cd-times-29f60aab69f9
        DOCKER_DRIVER: overlay2
    needs: []

need_post_writeup:
    image: python:3.7-slim-buster
    stage: build
    script:
        - apt update -y
        - apt install -y curl
        - scripts/wait_for_post.sh
    artifacts:
        paths:
            - scripts/run_post_writeup
    only:
        - master
    variables:
        #https://blog.sparksuite.com/7-ways-to-speed-up-gitlab-ci-cd-times-29f60aab69f9
        DOCKER_DRIVER: overlay2
    needs: []

test-md:
    image: python:3.7-slim-buster
    stage: test
    script:
        - python -V
        - python -m pip install pytest delayed_assert pyyaml requests --quiet
        - python -m pytest -s -q scripts/test_filesystem.py scripts/test_posts.py
    variables:
        #https://blog.sparksuite.com/7-ways-to-speed-up-gitlab-ci-cd-times-29f60aab69f9
        DOCKER_DRIVER: overlay2
    needs: []

spelling:
    image: python:3.7-slim-buster
    stage: test
    script:
        - python -V
        - python -m pip install codespell --quiet
        - codespell --version
        - scripts/check_spell.sh
    variables:
        #https://blog.sparksuite.com/7-ways-to-speed-up-gitlab-ci-cd-times-29f60aab69f9
        DOCKER_DRIVER: overlay2
    needs: []

validate_html:
    image: cyb3rjak3/html5validator:latest
    stage: test
    script:
        - html5validator --root test --format text
    variables:
        #https://blog.sparksuite.com/7-ways-to-speed-up-gitlab-ci-cd-times-29f60aab69f9
        DOCKER_DRIVER: overlay2
    needs: [build]

pages:
    image: alpine:latest
    stage: deploy
    cache:
        paths:
            - vendor/
    script:
        - mv test public
    artifacts:
        paths:
            - public
    only:
        - master
    variables:
        #https://blog.sparksuite.com/7-ways-to-speed-up-gitlab-ci-cd-times-29f60aab69f9
        DOCKER_DRIVER: overlay2
    needs:
        - test-md
        - spelling
        - validate_html
        - build

post_writeup:
    image: python:3.7-slim-buster
    stage: post
    script:
        - python -V
        - python -m pip install requests feedparser pyyaml pytz python-dateutil --quiet
        - if test -f scripts/run_post_writeup; then echo "Running post_htb_writeup.py"; python scripts/post_htb_writeup.py -n -k $HTB_API_TOKEN; else echo "No new post. Skipping post_htb_writeup.py"; fi
    only:
        - master
    variables:
        #https://blog.sparksuite.com/7-ways-to-speed-up-gitlab-ci-cd-times-29f60aab69f9
        DOCKER_DRIVER: overlay2
    needs: [pages, need_post_writeup]