自动标记版本

tak*_*hin 33 git bash hudson

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

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

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

这需要:

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

有小费吗?

tim*_*c22 40

我写这个是为了帮助逐步更新标签,例如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 2>/dev/null`

#only tag if no tag already
if [ -z "$NEEDS_TAG" ]; then
    git tag $NEW_TAG
    echo "Tagged with $NEW_TAG"
    git push --tags
else
    echo "Already a tag on this commit"
fi
Run Code Online (Sandbox Code Playgroud)

  • 在`git describe --contains $ GIT_COMMIT`的末尾添加`2>/dev/null`以删除"致命"警告.谢谢你的剧本 (3认同)

Von*_*onC 8

你所谈论的是更类似于一个技术性修订号像一个一个git describe将产生.

这与真正的应用程序版本不同,您应该仍然独立于Hudson管理,因为它依赖于版本控制策略.


Nal*_*ara 6

我在 gitlab pipelines 中使用这个函数,所以我希望这对你有帮助

#!/bin/bash

bump_version_tag(){
    if [ -z "$(git ls-remote --tags origin)" ]; then
        local new_tag="v1.0.0"
    else
        local current_tag=$(git ls-remote --tags origin | cut -f 3 -d '/')
        IFS='.' read -r -a array <<< "${current_tag:1}"
        local major=${array[0]}
        local minor=${array[1]}
        local patch=${array[2]}

        if [ "$minor" == 999 ] && [ "$patch" == 999 ]; then
            ((major++))
            minor=0
            patch=0
        elif [ "$patch" == 999 ]; then
            ((minor++))
            patch=0
        else
            ((patch++))
        fi
        new_tag="v${major}.${minor}.${patch}"
    fi
    echo $new_tag
}

git tag $bump_version_tag -a "autogenerated tag"
git push origin $NEW_TAG
Run Code Online (Sandbox Code Playgroud)

v如果在标签开头使用 use ${current_tag:1},否则使用${current_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
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您需要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
Run Code Online (Sandbox Code Playgroud)


Rad*_*der 5

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

#!/bin/bash

#get highest tag number
VERSION=`git describe --abbrev=0 --tags 2>/dev/null`

if [ -z $VERSION ];then
    NEW_TAG="1.0.0"
    echo "No tag present."
    echo "Creating tag: $NEW_TAG"
    git tag $NEW_TAG
    git push --tags
    echo "Tag created and pushed: $NEW_TAG"
    exit 0;
fi

#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}"

#get current hash and see if it already has a tag
GIT_COMMIT=`git rev-parse HEAD`
CURRENT_COMMIT_TAG=`git describe --contains $GIT_COMMIT 2>/dev/null`

#only tag if no tag already (would be better if the git describe command above could have a silent option)
if [ -z "$CURRENT_COMMIT_TAG" ]; then
    echo "Updating $VERSION to $NEW_TAG"
    git tag $NEW_TAG
    git push --tags
    echo "Tag created and pushed: $NEW_TAG"
else
    echo "This commit is already tagged as: $CURRENT_COMMIT_TAG"
fi
Run Code Online (Sandbox Code Playgroud)