Git post-ricezione Gancio per Staging Sito
-
27-09-2019 - |
Domanda
Sto cercando di impostare Git per la stadiazione il mio sito web in modo che io possa git pull
per ottenere la versione corrente di lavorare su livello locale e poi git push
per spingere le modifiche al server remoto. Ce l'ho impostato in modo che funziona il modo in cui voglio che, ma dopo spingo, devo git checkout -f
eseguire manualmente o git reset --hard HEAD
sul server remoto.
Ho provato a mettere questi in uno script di shell come il post-ricezione gancio sul server, ma semplicemente non sembra avere alcun effetto. So che lo script è in esecuzione, perché sto vedendo "I cambiamenti spinto al server" dopo spingo. Ecco il gancio post-ricezione:
#!/bin/sh
git reset --hard HEAD
echo "Changes pushed to server."
Soluzione
La risposta alla tua domanda è qui: http://toroid.org/ams/git-website-howto
In breve, ciò che si vuole fare è aggiungere un "albero di lavoro indipendente" al repository nudo. Normalmente si pensa del vostro albero di lavoro come contenente la directory .git
. repository nudi non hanno un albero di lavoro, per definizione, ma è possibile crearne uno tutto il tempo che è in una directory diversa da quella del pronti contro termine a nudo.
Il post-ricezione gancio è solo un git checkout -f
semplice per replicare HEAD
del repository nella directory di lavoro. Apache utilizza come sua radice dei documenti, e siete a posto. Ogni volta che si spinge al repository nuda, Apache inizierà immediatamente a servirla.
Io in genere utilizzare questo per spingere automaticamente a un server di gestione temporanea per vedere se l'ambiente "reale" sarà vomitare sulle mie modifiche. Distribuzione al server dal vivo è una storia completamente diversa. : -)
Altri suggerimenti
Aggiornamento marzo 2015
Come ho detto in " Che cosa è questo messaggio di avviso quando si spinge Git le modifiche a un repository remoto? ", è in realtà può spingere direttamente per un repo non nuda ora (Git 2.3.0+, febbraio 2015) con:
git config receive.denyCurrentBranch updateInstead
Aggiornamento l'albero di lavoro di conseguenza, ma si rifiutano di farlo se ci sono eventuali modifiche non.
Questo permetterebbe di evitare qualsiasi post-ricezione gancio.
(risposta originale: ott 2010)
Il GitFAQ raccomanda per non-nudo repo questo gancio post-update:
(Potrebbe dare più pallida idea di ciò che sta realmente accadendo nella esecuzione gancio. Nota: questo è un gancio di post-aggiornamento, non un post-ricezione)
#!/bin/sh
#
# This hook does two things:
#
# 1. update the "info" files that allow the list of references to be
# queries over dumb transports such as http
#
# 2. if this repository looks like it is a non-bare repository, and
# the checked-out branch is pushed to, then update the working copy.
# This makes "push" function somewhat similarly to darcs and bzr.
#
# To enable this hook, make this file executable by "chmod +x post-update".
git-update-server-info
is_bare=$(git-config --get --bool core.bare)
if [ -z "$is_bare" ]
then
# for compatibility's sake, guess
git_dir_full=$(cd $GIT_DIR; pwd)
case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac
fi
update_wc() {
ref=$1
echo "Push to checked out branch $ref" >&2
if [ ! -f $GIT_DIR/logs/HEAD ]
then
echo "E:push to non-bare repository requires a HEAD reflog" >&2
exit 1
fi
if (cd $GIT_WORK_TREE; git-diff-files -q --exit-code >/dev/null)
then
wc_dirty=0
else
echo "W:unstaged changes found in working copy" >&2
wc_dirty=1
desc="working copy"
fi
if git diff-index --cached HEAD@{1} >/dev/null
then
index_dirty=0
else
echo "W:uncommitted, staged changes found" >&2
index_dirty=1
if [ -n "$desc" ]
then
desc="$desc and index"
else
desc="index"
fi
fi
if [ "$wc_dirty" -ne 0 -o "$index_dirty" -ne 0 ]
then
new=$(git rev-parse HEAD)
echo "W:stashing dirty $desc - see git-stash(1)" >&2
( trap 'echo trapped $$; git symbolic-ref HEAD "'"$ref"'"' 2 3 13 15 ERR EXIT
git-update-ref --no-deref HEAD HEAD@{1}
cd $GIT_WORK_TREE
git stash save "dirty $desc before update to $new";
git-symbolic-ref HEAD "$ref"
)
fi
# eye candy - show the WC updates :)
echo "Updating working copy" >&2
(cd $GIT_WORK_TREE
git-diff-index -R --name-status HEAD >&2
git-reset --hard HEAD)
}
if [ "$is_bare" = "false" ]
then
active_branch=`git-symbolic-ref HEAD`
export GIT_DIR=$(cd $GIT_DIR; pwd)
GIT_WORK_TREE=${GIT_WORK_TREE-..}
for ref
do
if [ "$ref" = "$active_branch" ]
then
update_wc $ref
fi
done
fi
Per questo al lavoro, si sarebbe ancora bisogno di specificamente consentire spingere le modifiche al ramo corrente utilizzando una di queste impostazioni di configurazione:
git config receive.denyCurrentBranch ignore
o
git config receive.denyCurrentBranch warn
Ho avuto lo stesso problema esatto. In una risposta a questo link: http://toroid.org/ams/git-website-howto - The seguente comando ce l'ha fatta:
sudo chmod +x hooks/post-receive
Abbiamo perso un permesso sudo
prima configurato la roba.
fissi versione della sceneggiatura di VonC, lavora per me (assolutamente garanzie).
#!/bin/sh
#
# This hook does two things:
#
# 1. update the "info" files that allow the list of references to be
# queries over dumb transports such as http
#
# 2. if this repository looks like it is a non-bare repository, and
# the checked-out branch is pushed to, then update the working copy.
# This makes "push" function somewhat similarly to darcs and bzr.
#
# To enable this hook, make this file executable by "chmod +x post-update".
set -e
git update-server-info
is_bare=$(git config --get --bool core.bare)
if [ -z "${is_bare}" ]
then
# for compatibility's sake, guess
git_dir_full=$(cd $GIT_DIR; pwd)
case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac
fi
update_wc() {
ref=$1
echo "Push to checked out branch $ref" >&2
if [ ! -f ${GIT_DIR}/logs/HEAD ]
then
echo "E:push to non-bare repository requires a HEAD reflog" >&2
exit 1
fi
if (cd ${GIT_WORK_TREE}; git diff-files -q --exit-code >/dev/null)
then
wc_dirty=0
else
echo "W:unstaged changes found in working copy" >&2
wc_dirty=1
desc="working copy"
fi
if git diff-index --cached HEAD@{1} >/dev/null
then
index_dirty=0
else
echo "W:uncommitted, staged changes found" >&2
index_dirty=1
if [ -n "$desc" ]
then
desc="$desc and index"
else
desc="index"
fi
fi
if [ "$wc_dirty" -ne 0 -o "$index_dirty" -ne 0 ]
then
new=$(git rev-parse HEAD)
echo "W:stashing dirty $desc - see git-stash(1)" >&2
( trap 'echo trapped $$; git symbolic-ref HEAD "'"$ref"'"' 2 3 13 15 ERR EXIT
git update-ref --no-deref HEAD HEAD@{1}
cd ${GIT_WORK_TREE}
git stash save "dirty $desc before update to $new";
git symbolic-ref HEAD "$ref"
)
fi
# eye candy - show the WC updates :)
echo "Updating working copy" >&2
(cd ${GIT_WORK_TREE}
git diff-index -R --name-status HEAD >&2
git reset --hard HEAD
# need to touch some files or restart the application? do that here:
# touch *.wsgi
)
}
if [ x"${is_bare}" = x"false" ]
then
active_branch=$(git symbolic-ref HEAD)
export GIT_DIR=$(cd ${GIT_DIR}; pwd)
GIT_WORK_TREE="${GIT_DIR}/.."
for ref in $(cat)
do
if [ x"$ref" = x"${active_branch}" ]
then
update_wc $ref
fi
done
fi
semplice script per impostare questa distribuzione git:
La preparazione post-ricezione gancio:
echo '#!/bin/sh' > .git/hooks/post-receive
echo 'git checkout -f' >> .git/hooks/post-receive
echo 'git reset --hard' >> .git/hooks/post-receive
chmod +x .git/hooks/post-receive
Permettere spinta in questo repository, anche se non è nudo:
git config receive.denycurrentbranch false
io sono solo indovinare, ma questo potrebbe essere qualche problema di autorizzazione (il percorso completo necessaria? cd
?). Controllare cosa sta realmente accadendo nei file di log.
Tuttavia la pubblicazione dei file tramite git è sempre solo uno compiti del processo di pubblicazione. In genere è necessario copiare alcuni file, cancellare altri, l'installazione, autorizzazioni di aggiornamento, generare documenti ecc.
Per una soluzione complessa uno script di build potrebbe essere migliore di qualsiasi gancio git. Strumenti in grado di gestire questi compiti molto bene:
(Mi rendo conto che non è la risposta vi aspettate, ma è troppo lungo per inviare come commento)