2010-09-21 9 views
18

¿Cómo se etiquetan las versiones de lanzamiento en git?git: Etiquetado automático de las versiones

Ahora tengo cada versión identificada por número de compilación, pero aumentan incluso si no hay cambios en el repositorio. Mi idea es que se genere automáticamente en una implementación exitosa en el servidor de transferencia. P.ej.

  • plazo Hudson construir
  • cuando tiene éxito, Añadir nueva etiqueta, es decir, 1.0-1
  • en el próximo éxito acumulación añadir etiqueta siguiente, se muestra 1.0-2
  • etiqueta de versión entonces en el sitio de pie de página

Para ello sería necesario:

  • Hudson para gestionar próximos números de versión
  • o script para almacenar última etiqueta en algún archivo
  • o analizar las etiquetas git para determinar últimos

Algún consejo?

Respuesta

1

Hudson etiqueta automáticamente la construcción, si utiliza el complemento git y deja que Hudson extraiga el código. No estoy seguro si esto se empuja automáticamente; en nuestra configuración hacemos un etiquetado adicional e incluimos un 'git push - etiquetas' en nuestro script de compilación, por lo que definitivamente vemos las etiquetas de Hudson en nuestro repositorio central.

16

Escribí esto para ayudar a actualizar las etiquetas de forma incremental, p. Ej. 1.0.1 a 1.0.2, etc.

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

Esto se desencadena por un gancho git (en caso afirmativo, ¿cuál?), o ejecuta esto manualmente en lugar de 'git push'? – Gady

+1

Está solo en un archivo .sh que ejecuto manualmente (pero el archivo .sh también se ejecuta automáticamente como parte de una compilación de Jenkins en la que el proyecto siempre necesita una actualización del número de versión). Probablemente podría agregarse a un gancho de alguna manera (realmente no sé mucho sobre ganchos git para ser honesto, así que no puedo evitarlo, ¡me temo!) – timhc22

+1

Agregue '2>/dev/null' al final de' git describe --contiene $ GIT_COMMIT' para eliminar la advertencia "fatal". Gracias por el guión – koxon

2

realmente buena solución timhc22 Lo único que se necesita es que la última etiqueta (independientemente de la rama) Si usted trabaja en un proyecto con múltiples ramas que podría tener un problema . Propuse solo una mejora con su base.

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

Esto funciona, por ejemplo, si tiene:

  • una rama principal: creará maestro-XYZ
  • un comunicado/XY: creará XYZ

En cualquier caso muchas gracias me ayudó mucho.

1

Estoy utilizando como a continuación. Está funcionando perfectamente con las ramas. A continuación fragmentos inspirados en los comentarios anteriores y 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 
Cuestiones relacionadas