自动标记版本

时间:2010-09-21 12:05:52

标签: git bash hudson

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

现在我的每个版本都由内部版本号标识,但即使回购中没有任何更改,它们也会增加。我的想法是在登台服务器上成功部署后自动生成它。 E.g。

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

这需要:

  • Hudson管理下一个版本号
  • 或用于将最后一个标记存储在某个文件中的脚本
  • 或解析git标签以确定最后一次

任何提示?

9 个答案:

答案 0 :(得分:28)

我写这篇文章是为了帮助逐步更新标签,例如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

答案 1 :(得分:7)

您所谈论的内容更类似于technical revision number,就像a git describe would generate一样。

这与真正的应用程序版本不同,您应该仍然独立于Hudson管理depends on a versioning policy

答案 2 :(得分:2)

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

答案 3 :(得分:2)

非常好的解决方案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
  • 发布/ X.Y:将创建X.Y.Z

无论如何,非常感谢它给我带来了很多帮助。

答案 4 :(得分:2)

如果您需要Posix版本,则与上述答案几乎相同

-6.0 16.0

答案 5 :(得分:1)

我使用如下。它与分支机构完美配合。以下评论和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

答案 6 :(得分:1)

基于timhc22答案,但略作修改以处理初始标记,并在提交时没有标记的情况下静音输出错误

    $url = $_SERVER['REQUEST_URI'];     
$asiguri = 'asignaciones'; 
$comburi = 'combinaciones';
$legauri = 'legajos';
$inicuri = '/';
switch (true) {
    case strpos($url,$asiguri) === 1: //when $url = '/asignaciones' strpos returns '0' so 'asignaciones' returns '1'. when it returns 1 the code executes until the break
        $asignaciones = 'active_button';
        $combinaciones = $legajos = $inicio = 'unactive_button';

        break;
    case strpos($url,$comburi) === 1:
        $asignaciones = $legajos = $inicio = 'unactive_button';
        $combinaciones = 'active_button';
        break;
    case strpos($url,$legauri) === 1:
        $asignaciones = $combinaciones = $inicio = 'unactive_button';
        $legajos = 'active_button';
        break;
    case strpos($url, $inicuri) === 0: //leave at last because '/' is present in every other
        $asignaciones = $combinaciones = $legajos = 'unactive_button';
        $inicio = 'active_button';
        break;
    default:
        $asignaciones = $combinaciones = $legajos = $inicio = 'unactive_button';
}

答案 7 :(得分:0)

好的解决方案。我建议使用带注释的标签来表示版本吗?喜欢:

git tag -a "$NEW_TAG"  -m "autogenerated"

答案 8 :(得分:0)

希望这会有所帮助

#!/bin/bash

RES=$(git show-ref --tags)
if [ -z "$RES" ]; then
    NEW_TAG="v1.0.0"
else
    LATEST_TAG=$(git describe --tags --abbrev=0)
    IFS='.' read -r -a array <<< ${LATEST_TAG:1}
    one=${array[0]}
    two=${array[1]}
    three=${array[2]}

    if [ "$three" == "999" ]; then
        if [ "$two" == "999" ]; then
            three=0
            two=0
            ((one++))
        else
            ((two++))
            three=0
        fi
    elif [ "$two" == "999" ] && [ "$three" == "999" ]; then
        ((one++))
        two=0
    else
        ((three++))
    fi

    NEW_TAG="v${one}.${two}.${three}"
fi

git tag $NEW_TAG -a "autogenerated tag"
git push origin $NEW_TAG

或者如果你只想增加标签的最后一部分,使用这个

#!/bin/bash

RES=$(git show-ref --tags)
if [ -z "$RES" ]; then
    NEW_TAG=v1.0.0
else
    NEW_TAG=$(git describe --tags --abbrev=0 | awk -F. '{OFS="."; $NF+=1; print $0}')
fi

git tag $NEW_TAG -a "autogenerated tag"
git push origin $NEW_TAG