Automatically Bumping the Version Number on Release with GitLab CI/CD

GitLab CI/CD is a continuous integration and continuous delivery service built into GitLab. It helps developers automatically run a series of tasks after every commit, such as building, testing, packaging and deploying. This article shows how to use GitLab CI/CD to automatically increment the version number and push it to the repository, so you no longer have to bother updating version numbers by hand.

Prerequisites

  • A GitLab account, with a project created or imported
  • A .gitlab-ci.yml file in the project root directory, which defines the GitLab CI/CD configuration
  • GitLab Runner installed and registered with the GitLab server; the machine running GitLab Runner needs the jq tool installed.

How to Configure Gitlab CI/CD

To use Gitlab CI/CD to automatically increment the version number and push it to the repository, we need to do the following:

  1. Create a .gitlab-ci.yml file in the project root directory. This is a declarative file that defines the Gitlab CI/CD pipeline and jobs.
  2. Define a deploy stage in the .gitlab-ci.yml file to perform the version number update.
  3. In the deploy stage, use the jq command-line tool to change the version number.
  4. Enable Gitlab CI/CD in the project settings and add the required environment variables.

Here we assume the project already has a version number stored in the package.json file. If not, adjust accordingly for your own project.

For example:

{
  "name": "my-app",
  "version": "1.0.0",
  // 其他字段
}

We also need to add the following environment variable in the project settings:

  • GitLab_Access_Tokens: Create an Access Token under project settings -> Access Tokens and assign its value to this environment variable.

These environment variables can be added under project settings -> CI/CD -> Variables, and you can choose whether to protect or mask them.

Here is an example .gitlab-ci.yml file:

stages:
  - deploy

master-deploy:
  stage: deploy
  only:
    - master # 只有master更新的时候才执行命令
  except:
    - /^Increment version number to/ # 避免触发以 "Increment version number to" 开头的提交
  script: # 执行的命令
    - chmod +x ./version_update.sh # 赋予脚本执行权限
    - ./version_update.sh # 运行自动更新版本号脚本
  

Here is an example version_update.sh file:

#!/bin/bash

# 从 package.json 中提取当前版本号
current_version=$(jq -r '.version' package.json)

# 分割版本号
IFS='.' read -ra version_parts <<< "$current_version"

# 递增修订号
((version_parts[2]++))

# 重新构建新版本号
new_version="${version_parts[0]}.${version_parts[1]}.${version_parts[2]}"

# 更新 package.json 中的版本号
jq ".version = \"$new_version\"" package.json > temp.json
mv temp.json package.json

# 提交版本号更新
git config user.name "your-name"
git config user.email "your-email"
git remote add gitlab_origin https://oauth2:$GitLab_Access_Tokens@your-gitlab-host.com/your-username/your-repo.git
git add package.json
git commit -m "Increment version number to $new_version"
git push gitlab_origin HEAD:master -o ci.skip

How to Verify the Result

Once we commit code, Gitlab CI/CD is triggered automatically and runs the jobs defined in the .gitlab-ci.yml file. We can check the pipeline status and logs on the project page under CI/CD -> Pipelines.