首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >版本的自动标记

版本的自动标记
EN

Stack Overflow用户
提问于 2010-09-21 20:05:52
回答 9查看 52.7K关注 0票数 53

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

现在,我已经通过内部版本号标识了每个版本,但即使在repo中没有任何更改,它们也会增加。我的想法是在登台服务器上成功部署时自动生成它。例如。

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

<代码>F29

这将需要:

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

有什么建议吗?

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2010-09-21 20:55:52

您正在讨论的内容更类似于像a git describe would generate这样的technical revision number

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

票数 11
EN

Stack Overflow用户

发布于 2014-12-06 22:03:15

我写这篇文章是为了帮助增量更新标签,比如从1.0.1到1.0.2等等。

2020更新:我已经在/here下面发布了一个改进的版本。(也值得看看下面@Geoffrey的答案,关于分支的一些评论)。

#!/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

6年后,我再次访问它,需要编写一个类似的脚本来上传到npm。下面的代码或here,欢迎您的评论和建议:

#!/bin/bash

# https://github.com/unegma/bash-functions/blob/main/update.sh

VERSION=""

#get parameters
while getopts v: flag
do
  case "${flag}" in
    v) VERSION=${OPTARG};;
  esac
done

#get highest tag number, and add 1.0.0 if doesn't exist
CURRENT_VERSION=`git describe --abbrev=0 --tags 2>/dev/null`

if [[ $CURRENT_VERSION == '' ]]
then
  CURRENT_VERSION='1.0.0'
fi
echo "Current Version: $CURRENT_VERSION"


#replace . with space so can split into an array
CURRENT_VERSION_PARTS=(${CURRENT_VERSION//./ })

#get number parts
VNUM1=${CURRENT_VERSION_PARTS[0]}
VNUM2=${CURRENT_VERSION_PARTS[1]}
VNUM3=${CURRENT_VERSION_PARTS[2]}

if [[ $VERSION == 'major' ]]
then
  VNUM1=$((VNUM1+1))
elif [[ $VERSION == 'minor' ]]
then
  VNUM2=$((VNUM2+1))
elif [[ $VERSION == 'patch' ]]
then
  VNUM3=$((VNUM3+1))
else
  echo "No version type (https://semver.org/) or incorrect type specified, try: -v [major, minor, patch]"
  exit 1
fi


#create new tag
NEW_TAG="$VNUM1.$VNUM2.$VNUM3"
echo "($VERSION) updating $CURRENT_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
#to publish, need to be logged in to npm, and with clean working directory: `npm login; git stash`
if [ -z "$NEEDS_TAG" ]; then
  npm version $NEW_TAG
  npm publish --access public
  echo "Tagged with $NEW_TAG"
  git push --tags
  git push
else
  echo "Already a tag on this commit"
fi

exit 0

备注:这不适用于zsh,因为(首先),zsh不使用0索引数组。

票数 69
EN

Stack Overflow用户

发布于 2018-08-07 21:43:52

如果您需要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
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3760086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档