2010-09-21 20 views
18

Come si codificano le versioni di rilascio in git?git: codifica automatica delle versioni

Ora ho ogni versione identificata dal numero di build, ma aumentano anche se non ci sono cambiamenti nel repository. La mia idea è di averla generata automaticamente su una distribuzione di successo sul server di staging. Per esempio.

  • corsa Hudson costruire
  • quando ha successo, Aggiungi nuovo tag, cioè 1.0-1
  • sul prossimo successo accumulo aggiungere tag successivo, 1,0-2
  • tag versione viene visualizzata poi nel footer del sito

Ciò richiederebbe:

  • Hudson per gestire prossimi numeri di versione
  • o di script per memorizzare ultimo tag in qualche file di
  • o analizzare i tag git per determinare ultimi

Qualche consiglio?

risposta

1

Hudson contrassegna automaticamente la build, se si utilizza il plugin git e si lascia che Hudson estrae il codice. Non sono sicuro che questo venga spinto automaticamente; nella nostra configurazione facciamo tagging extra e includiamo un 'git push --tags' nel nostro script di build, quindi vedremo sicuramente i tag Hudson nel nostro repository centrale.

16

Ho scritto questo per aiutare con l'aggiornamento dei tag in modo incrementale, ad es. 1.0.1 a 1.0.2 ecc

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

È attivato da un hook di git (se sì, quale?), o lo fai manualmente invece di 'git push'? – Gady

+1

È solo in un file .sh che eseguo manualmente (ma il file .sh viene eseguito automaticamente come parte di una build di Jenkins in cui il progetto richiede sempre un aggiornamento del numero di versione). Probabilmente potrebbe essere aggiunto ad un hook in qualche modo (non so davvero molto sui git git per essere onesto quindi non posso farci a meno di avere paura!) – timhc22

+1

Aggiungi '2>/dev/null' alla fine di' git descrive - contiene $ GIT_COMMIT' per rimuovere l'avviso "fatale". Grazie per la sceneggiatura – koxon

2

Davvero buona soluzione timhc22 L'unica cosa è che ci vuole l'ultimo tag (indipendentemente dal ramo) Se si lavora su un progetto con più rami si potrebbe avere un problema . Ho proposto solo un miglioramento con la tua 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 

Questo funziona per esempio se si dispone:

  • un ramo maestro: creerà master-XYZ
  • un rilascio/XY: creerà XYZ

In ogni caso grazie mille mi ha aiutato molto

1

Sto usando come di seguito. Funziona perfettamente con i rami. Sottotitoli ispirati ai commenti precedenti e allo 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 
Problemi correlati