代码之家  ›  专栏  ›  技术社区  ›  takeshin

发布的自动标记

  •  25
  • takeshin  · 技术社区  · 15 年前

    如何在Git中标记发布版本?

    现在,每个版本都由内部版本号标识,但即使在repo中没有更改,它们也会增加。我的想法是在成功部署到临时服务器上时自动生成它。例如。

    • 运行Hudson Build
    • 成功后,添加新标签,即1.0-1
    • 下一个 成功的 构建添加下一个标记,1.0-2
    • 版本标记随后显示在网站页脚中

    这需要:

    • 哈德逊管理下一个版本号
    • 或脚本将最后一个标记存储在某个文件中
    • 或者解析git标记以确定最后一个

    有什么小窍门吗?

    6 回复  |  直到 7 年前
        1
  •  6
  •   Community Mohan Dere    9 年前

    你所说的更像是 technical revision number 像那个 a git describe would generate .

    这与真正的应用程序版本不同,因为它仍然需要独立于Hudson进行管理。 depends on a versioning policy .

        2
  •  27
  •   timhc22    11 年前

    我写这篇文章是为了帮助逐步更新标签,例如1.0.1到1.0.2等等。

    #!/bin/bash
    
    #get highest tag number
    VERSION=`git describe --abbrev=0 --tags`
    
    #replace . with space so can split into an array
    VERSION_BITS=(${VERSION//./ })
    
    #get number parts and increase last one by 1
    VNUM1=${VERSION_BITS[0]}
    VNUM2=${VERSION_BITS[1]}
    VNUM3=${VERSION_BITS[2]}
    VNUM3=$((VNUM3+1))
    
    #create new tag
    NEW_TAG="$VNUM1.$VNUM2.$VNUM3"
    
    echo "Updating $VERSION to $NEW_TAG"
    
    #get current hash and see if it already has a tag
    GIT_COMMIT=`git rev-parse HEAD`
    NEEDS_TAG=`git describe --contains $GIT_COMMIT`
    
    #only tag if no tag already (would be better if the git describe command above could have a silent option)
    if [ -z "$NEEDS_TAG" ]; then
        echo "Tagged with $NEW_TAG (Ignoring fatal:cannot describe - this means commit is untagged) "
        git tag $NEW_TAG
        git push --tags
    else
        echo "Already a tag on this commit"
    fi
    
        3
  •  2
  •   Graham Perks    15 年前

    如果您使用Git插件并让Hudson提取代码,Hudson会自动标记构建。我不确定这是否会被自动推送;在我们的设置中,我们会做额外的标记,并在构建脚本中包含一个“git push--tags”,所以我们肯定会在我们的中央存储库中看到hudson标记。

        4
  •  2
  •   Geoffrey    8 年前

    很好的解决方案timhc22 唯一的事情是它接受最后一个标签(无论分支是什么) 如果你在一个有多个分支的项目上工作,你可能会遇到一个问题。 我只建议改善你的基础。

    #!/bin/sh
    
    # retrieve branch name
    BRANCH_NAME=$(git branch | sed -n '/\* /s///p')
    
    # remove prefix release
    REGEXP_RELEASE="release\/"
    VERSION_BRANCH=$(echo "$BRANCH_NAME" | sed "s/$REGEXP_RELEASE//") 
    
    echo "Current version branch is $VERSION_BRANCH"
    
    # retrieve the last commit on the branch
    VERSION=$(git describe --tags --match=$VERSION_BRANCH* --abbrev=0)
    
    # split into array
    VERSION_BITS=(${VERSION//./ })
    
    #get number parts and increase last one by 1
    VNUM1=${VERSION_BITS[0]}
    VNUM2=${VERSION_BITS[1]}
    VNUM3=${VERSION_BITS[2]}
    VNUM3=$((VNUM3+1))
    
    #create new tag
    NEW_TAG="$VNUM1.$VNUM2.$VNUM3"
    
    echo "Updating $VERSION to $NEW_TAG"
    
    #get current hash and see if it already has a tag
    GIT_COMMIT=`git rev-parse HEAD`
    NEEDS_TAG=`git describe --contains $GIT_COMMIT`
    
    #only tag if no tag already (would be better if the git describe command above could have a silent option)
    if [ -z "$NEEDS_TAG" ]; then
        echo "Tagged with $NEW_TAG (Ignoring fatal:cannot describe - this means commit is untagged) "
        git tag $NEW_TAG
        git push --tags
    else
        echo "Already a tag on this commit"
    fi
    

    例如,如果您有:

    • 主分支:将创建master-x.y.z
    • A release/X.Y:将创建X.Y.Z

    不管怎样,谢谢你,它帮助了我很多。

        5
  •  2
  •   Dias Abdraimov    7 年前

    如果您需要POSIX版本,几乎与上面的答案相同

    #!/bin/sh
    
    #Get the highest tag number
    VERSION=`git describe --abbrev=0 --tags`
    VERSION=${VERSION:-'0.0.0'}
    
    #Get number parts
    MAJOR="${VERSION%%.*}"; VERSION="${VERSION#*.}"
    MINOR="${VERSION%%.*}"; VERSION="${VERSION#*.}"
    PATCH="${VERSION%%.*}"; VERSION="${VERSION#*.}"
    
    #Increase version
    PATCH=$((PATCH+1))
    
    #Get current hash and see if it already has a tag
    GIT_COMMIT=`git rev-parse HEAD`
    NEEDS_TAG=`git describe --contains $GIT_COMMIT`
    
    #Create new tag
    NEW_TAG="$MAJOR.$MINOR.$PATCH"
    echo "Updating to $NEW_TAG"
    
    #Only tag if no tag already (would be better if the git describe command above could have a silent option)
    if [ -z "$NEEDS_TAG" ]; then
        echo "Tagged with $NEW_TAG (Ignoring fatal:cannot describe - this means commit is untagged) "
        git tag $NEW_TAG
    else
        echo "Already a tag on this commit"
    fi
    
        6
  •  1
  •   abhishekrvce    8 年前

    我使用如下。它与树枝完美配合。下面的片段是从上面的评论和 gitversion / semver.org .

    #!/bin/sh
    
    # This script will be executed after commit in placed in .git/hooks/post-commit
    
    # Semantic Versioning 2.0.0 guideline
    # 
    # Given a version number MAJOR.MINOR.PATCH, increment the:
    # MAJOR version when you make incompatible API changes,
    # MINOR version when you add functionality in a backwards-compatible manner, and
    # PATCH version when you make backwards-compatible bug fixes.
    
    echo "Starting the taging process based on commit message +semver: xxxxx"
    
    #get highest tags across all branches, not just the current branch
    VERSION=`git describe --tags $(git rev-list --tags --max-count=1)`
    
    # split into array
    VERSION_BITS=(${VERSION//./ })
    
    echo "Latest version tag: $VERSION"
    
    #get number parts and increase last one by 1
    VNUM1=${VERSION_BITS[0]}
    VNUM2=${VERSION_BITS[1]}
    VNUM3=${VERSION_BITS[2]}
    # VNUM3=$((VNUM3+1))
    
    # Taken from gitversion
    # major-version-bump-message: '\+semver:\s?(breaking|major)'
    # minor-version-bump-message: '\+semver:\s?(feature|minor)'
    # patch-version-bump-message: '\+semver:\s?(fix|patch)'
    # get last commit message and extract the count for "semver: (major|minor|patch)"
    COUNT_OF_COMMIT_MSG_HAVE_SEMVER_MAJOR=`git log -1 --pretty=%B | egrep -c '\+semver:\s?(breaking|major)'`
    COUNT_OF_COMMIT_MSG_HAVE_SEMVER_MINOR=`git log -1 --pretty=%B | egrep -c '\+semver:\s?(feature|minor)'`
    COUNT_OF_COMMIT_MSG_HAVE_SEMVER_PATCH=`git log -1 --pretty=%B | egrep -c '\+semver:\s?(fix|patch)'`
    
    if [ $COUNT_OF_COMMIT_MSG_HAVE_SEMVER_MAJOR -gt 0 ]; then
        VNUM1=$((VNUM1+1)) 
    fi
    if [ $COUNT_OF_COMMIT_MSG_HAVE_SEMVER_MINOR -gt 0 ]; then
        VNUM2=$((VNUM2+1)) 
    fi
    if [ $COUNT_OF_COMMIT_MSG_HAVE_SEMVER_PATCH -gt 0 ]; then
        VNUM3=$((VNUM3+1)) 
    fi
    
    # count all commits for a branch
    GIT_COMMIT_COUNT=`git rev-list --count HEAD`
    echo "Commit count: $GIT_COMMIT_COUNT" 
    export BUILD_NUMBER=$GIT_COMMIT_COUNT
    
    #create new tag
    NEW_TAG="$VNUM1.$VNUM2.$VNUM3"
    
    echo "Updating $VERSION to $NEW_TAG"
    
    #only tag if commit message have version-bump-message as mentioned above
    if [ $COUNT_OF_COMMIT_MSG_HAVE_SEMVER_MAJOR -gt 0 ] ||  [ $COUNT_OF_COMMIT_MSG_HAVE_SEMVER_MINOR -gt 0 ] || [ $COUNT_OF_COMMIT_MSG_HAVE_SEMVER_PATCH -gt 0 ]; then
        echo "Tagged with $NEW_TAG (Ignoring fatal:cannot describe - this means commit is untagged) "
        git tag "$NEW_TAG"
    else
        echo "Already a tag on this commit"
    fi