【问题标题】:How to go about implementing semantic versioning with bitbucket pipelines and branches?如何使用 bitbucket 管道和分支实现语义版本控制?
【发布时间】:2018-10-26 13:08:58
【问题描述】:

我有一个 python 应用程序,我想像这样实现语义版本控制:MAJOR.MINOR.PATCH.BUILD。我想通过 bitbucket 管道尽可能地自动化它。

我将在下面回答我自己的问题,以与其他人分享我是如何做到这一点的,因为那里的博文/资源很少。

【问题讨论】:

    标签: bash bitbucket bitbucket-pipelines


    【解决方案1】:

    所有相关文件都包含在下面,但这里是它的要点。我有一个 master 分支,在合并到 master 之前,我会从中创建各种功能分支。我已将每次合并到主分支标记为次要更新。这是通过下面的代码完全自动化的。无论我在哪个分支上,每次新提交都会自动更新内部版本号。最后,补丁编号和主要编号更新是通过 BB 管道中的触发器半自动进行的。

    我将版本号存储在 version.txt 文件中,每次构建我都会更新版本号并运行提交并从该构建中推送,但使用 [skip CI] 跳过该次要提交的构建过程,否则它会循环无限地。如果需要,很乐意进一步解释。

    version.sh 脚本的一部分来自linux - simplify semantic versioning script 上的回答

    这是我的bitbucket-pipelines.yml 文件的样子:

    image: python:3.6
    
    pipelines:
      default:
        - step:
            script: # Modify the commands below to build your repository.
              # Linting check
              - pip3 --version
              - pip3 install -r requirements.txt
              - pylint backend
              - bash version.sh build $BITBUCKET_BUILD_NUMBER $BB_AUTH_STRING
    
      branches:
        master:
          -step:
            name: Minor version update
            script:
              # Linting check
              - pip3 --version
              - pip3 install -r requirements.txt
              - pylint backend
              # Increase minor version number
              - bash version.sh minor $BITBUCKET_BUILD_NUMBER $BB_AUTH_STRING
          -step:
            trigger: manual
            name: Major version update
            script:
              - bash version.sh major $BITBUCKET_BUILD_NUMBER $BB_AUTH_STRING
          -step:
            trigger: manual
            name: Patch version update
            script:
              - bash version.sh patch $BITBUCKET_BUILD_NUMBER $BB_AUTH_STRING
    

    和version.sh:

    #!/bin/bash
    
    #CONFIG
    USER=""
    REPO=""
    USERNAME=""
    EMAIL=""
    
    major() {
    if IFS=. read -r major rest <version.txt || [ -n "$major" ]; then
      echo "$((major + 1)).0.0.$1" >"version.txt"
    else
      echo "ERROR: Unable to read version number from version.txt" >&2
      exit 1
    fi
    }
    
    minor() {
    if IFS=. read -r major minor patch build <version.txt || [ -n "$major" ]; then
      echo "$major.$((minor + 1)).0.$1" >"version.txt"
    else
      echo "ERROR: Unable to read version number from version.txt" >&2
      exit 1
    fi
    }
    
    # Need to substract one from minor because automatically runs
    patch() {
    if IFS=. read -r major minor patch build <version.txt || [ -n "$major" ]; then
      echo "$major.$((minor - 1)).$((patch + 1)).$1" >"version.txt"
    else
      echo "ERROR: Unable to read version number from version.txt" >&2
      exit 1
    fi
    }
    
    build() {
    if IFS=. read -r major minor patch build <version.txt || [ -n "$major" ]; then
      echo "$major.$minor.$patch.$1" >"version.txt"
    else
      echo "ERROR: Unable to read version number from version.txt" >&2
      exit 1
    fi
    }
    
    update() {
    echo "New version = $(<version.txt)"
    git config --global push.default simple
    git remote set-url origin https://${1}@bitbucket.org/${USER}/${REPO}.git
    git config user.name $USERNAME
    git config user.email $EMAIL
    git config -l
    git add version.txt
    git commit -m "[skip CI]"
    git push
    }
    
    case "$1" in
      major)
        major $2
        update $3
        ;;
      minor)
        minor $2 
        update $3
        ;;
      patch)
        patch $2
        update $3
        ;;
      build)
        build $2
        update $3
        ;;
      *)
        echo "Usage: bash version.sh {major|minor|patch|build} build_number bb_auth_string"
        exit 1
    esac
    exit 0
    

    最后version.txt是一个简单的文本文件,四个数字用一个点隔开,比如4.3.2.1

    很高兴就如何改进我的方法提出任何建议。

    【讨论】:

      猜你喜欢
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      • 2023-01-27
      • 2018-06-12
      • 2018-08-08
      • 2012-04-01
      • 2019-05-30
      • 2021-02-23
      相关资源
      最近更新 更多