Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active October 13, 2022 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halberom/d4d6b642d801bbfeeca524126f18fa98 to your computer and use it in GitHub Desktop.
Save halberom/d4d6b642d801bbfeeca524126f18fa98 to your computer and use it in GitHub Desktop.
CICD semantic versioning foo - bump a repository version based on past tags instead of VERSION file

bump magic

Principal behind this is that I want to automatically bump the semantic version of a repository, but I don't want to manage a VERSION file, as that requires the CICD tool to commit back to master.

A VERSION file can work, but it has several limitations

  • we can't properly configure branch protection if the CICD tool needs to push directly to master
  • we have to pull in between each version bump
  • the CICD list of jobs fills up with [skip ci] bump version entries
  • there is a risk of bump conflict if person A pushes, and then person B pushes before the bump is complete.

This example is for CircleCI (note a read/write deploy key is required), but it should work just as easily for Gitlab, Jenkins etc.

The main magic happens with this line

        @(git tag --sort=-creatordate | grep -E '^\d+\.\d+\.\d+$$' || echo '0.0.0') | head -n 1 > VERSION

it will list all tags sorted by their creatordate, latest to oldest, from that we can grep for whatever regex we want - if nothing is found (e.g. in a new repo), then we output the starting version number 0.0.0 and put it in a version file which the bump tool will parse.

thanks go to @treeder for the very nice and simple bump tool - https://github.com/treeder/dockers/tree/master/bump

# .config/circleci.yml
version: 2
jobs:
test:
... do stuff
bump:
docker:
# use a very simple version bump tool, from
# https://github.com/treeder/dockers/tree/master/bump
- image: treeder/bump:1.2.8
working_directory: ~/repo
steps:
- checkout
- setup_remote_docker
- add_ssh_keys:
fingerprints:
- "blah:blah:blah"
- run:
name: install pre-requisites
command: apk --no-cache add git make openssh-client
# may or may not be necessary - checkme
#- run:
# name: skip StrictHostKeyChecking for github
# command: echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
- run:
name: bump the version
command: make bump
build:
...
release:
...
workflows:
version: 2
build:
jobs:
- build
- bump:
requires:
- build
filters:
branches:
only: master
tags:
ignore: /.*/
release:
jobs:
- release:
filters:
branches:
ignore: /.*/
tags:
only: /^\d+\.\d+\.\d+$/
.PHONY: bump
# version bump, assumes we're in treeder/bump in CircleCI
bump:
@git config --global user.email "-"
@git config --global user.name "CircleCi"
@(git tag --sort=-creatordate | grep -E '^\d+\.\d+\.\d+$$' || echo '0.0.0') | head -n 1 > VERSION
@/script/bump $$(git log -1 --pretty=%B)
@git tag $$(cat VERSION)
@git push origin $$(cat VERSION)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment