在我们的开发环境中,我们已经设置了NuGet本地服务器(BaGet)。我们采用了Gitflow的想法。当准备好要在Baget上发布库时,开发人员应首先在master
分支上增加Tag(首先需要通过pull-request批准),然后将库推入Baget。我们这样做是为了使Git
和Nuget
的版本保持同步。
开发人员手动控制版本保持同步的过程(Git标签和NuGet版本),有时某些团队成员忘记定义Git版本标签,而只是将库推送到Baget。
如果script
可以在将库推送到Baget服务器之前检查当前Git标记,并且仅在 Tag
我们使用此脚本将信息推送到Baget:
#!/bin/bash
clear
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR
ostype=`uname`
KEY=$NUGET_KEY
SOURCE=$NUGET_URL
while :
do
clear
echo "Input your package version: "
read version
Common="Dayan.Common/bin/Debug/Dayan.Common."$version".nupkg"
dotnet nuget push $Common -s $SOURCE -k $KEY
echo "press enter to continue ..."
read
done
我可以通过某种方式检查bash中的git命令以获取项目master分支上的最后一次提交的Tag,并与用户输入的版本进行检查吗?
答案 0 :(得分:1)
进行检查的一种方法是使用git
命令rev-list
。
此命令将输出最近一次提交的提交SHA:
$ git rev-list -n 1 HEAD
dfe4a9989b33e97f25645d79fd62900cc3209ec7
此命令将输出标签3.1.5
的提交SHA:
$ git rev-list -n 1 "3.1.5"
a35117a201290b63b53ba6372dbf8bbfc68f28b9
以下示例脚本将帮助您入门:
#!/bin/bash
echo "Input your package version: "
read version
last_commit=$(git rev-list -n 1 HEAD 2>/dev/null)
last_commit_result=$?
if [ "$last_commit_result" != "0" ]; then
echo "Failed to get the SHA of the most recent commit"
exit 1
fi
version_commit=$(git rev-list -n 1 "$version" 2>/dev/null)
version_commit_result=$?
if [ "$version_commit_result" != "0" ]; then
echo "There is no commit with the tag: $version"
exit 1
fi
if [ "$last_commit" = "$version_commit" ]; then
echo "The most recent commit has the tag: $version"
else
echo "The most recent commit does NOT have the tag: $version"
fi
如果您还想确保脚本仅从master
运行,则将其添加到脚本开头附近:
active_branch=$(git branch --show-current 2>/dev/null)
active_branch_result=$?
if [ "$active_branch_result" != "0" ]; then
echo "Failed to get the active branch"
exit 1
elif [ "$active_branch" != "master" ]; then
echo "The active branch is not master"
exit 1
fi