Add GitLab jobs when a tag does not exist

How can I do the following when using GitLab pipeline defined using .gitlab-ci.yml

  1. Get a version number from a file in the Git repo. For example using something like VSN=$(yq '.version' galaxy.yml | sed 's/"//g')
  2. Check if a tag exists in the repository for that version.
  3. Add jobs to the pipeline when there is no tag for the version.

For an example see my pipeline.

I have a job to publish my Ansible collection to Galaxy. This is a manual job and this results in status of “blocked”. As a workaround I configured allow_failure: true so the status will not be “blocked” but I don’t like this workaround because failures should not be allowed.

What I would like to do is to not add the jobs at all because the jobs will fail anyway if the tag already exists. The publish job should only be added to the pipeline if the tag does not exist.

publish:
  stage: galaxy
  needs:
    - prepare
  script:
    - cat README-GALAXY.md > README.md  # readme property in galaxy.yml is ignored by galaxy website
    - ansible-galaxy collection build . --force
    - ansible-galaxy collection publish $C2_NAMESPACE-$C2_NAME-$C2_VERSION.tar.gz --api-key $GALAXY_API_KEY
  artifacts:
    paths:
      - $C2_NAMESPACE-$C2_NAME-$C2_VERSION.tar.gz
  when: manual
  allow_failure: true

Just a suggestion here ( I am not sure if this works ) → You could make the needs “optional”.

Works for me, thanks.