Skip to content

Instantly share code, notes, and snippets.

@samirfor
Last active October 18, 2017 19:20
Show Gist options
  • Save samirfor/9b255032b977b0a8a8ee to your computer and use it in GitHub Desktop.
Save samirfor/9b255032b977b0a8a8ee to your computer and use it in GitHub Desktop.
Configurando Webhook para Autodeploy no GitLab

Configurando Webhook para Autodeploy no GitLab

  1. Crie uma pasta no /var/www/ chamada .ssh.
 mkdir -p /var/www/.ssh
  1. Crie uma chave para o usuário www-data.
 ssh-keygen -t rsa -C "www-data"
  1. Quando perguntar por uma senha, deixe em branco.
  2. Quando perguntar onde salvar o arquivo da chave, digite:

/var/www/.ssh/id_rsa

  1. Agora, copie a chave pública.
 cat /var/www/.ssh/id_rsa.pub
  1. No GitLab, vá em settings do projeto,

crie uma nova Deploy Key, cole e salve.

  1. Mude o dono e as permissões da pasta /var/www/.ssh

recursivamente.

sudo chmod -R 0600 /var/www/.ssh
sudo chown -R www-data:www-data /var/www/.ssh
  1. Finalmente, clone seu projeto ou faça um acesso ssh para que a deploy key seja armazenada. Precisamos apenas armazenar o fingerprint SSH RSA do servidor no arquivo /var/www/.ssh/known_hosts. Assim, a operação atrás dos bastidores não travará quando tentar se conectar pela primeira vez sem a intervenção do usuário.:
 sudo -u www-data cd /var/www/ && git clone git@git.yourserver.com/user/someproject.git

ou

 sudo -u www-data ssh git.yourserver.com

Quando for pedido para armazenar o fingerprint RSA, digite "yes".

Macetes

Failed to add the host to the list of known hosts (/var/www/.ssh/known_hosts).
  • Configure permissão 777 em /var/www/.ssh/known_hosts:

    sudo chmod 777 /var/www/.ssh/known_hosts
  • Execute o passo 8 novamente.

  • Configure a permissão novamente:

    sudo chmod 600 /var/www/.ssh/known_hosts
Verifique o remoto

Caso tenha clonado o via protocolo HTTP, é necessário trocar o remoto para o protocolo GIT.

git remote remove origin
git remote add origin git@enderecogitlab:seuprojeto.git

Fontes

<?php
// verifica se a requisição vem do servidor gitlab
$repositorio = "seurepositorio";
$gitlab_ips = array('10.10.10.10', '127.0.0.1');
if (!in_array($_SERVER['REMOTE_ADDR'], $gitlab_ips)) {
throw new Exception("Isto não parece uma requisição válida do Gitlab.\n");
}
if ($payload = file_get_contents('php://input')) {
$arquivo = fopen("/tmp/debug-webhook-${repositorio}.log", 'w');
fwrite($arquivo, file_get_contents('php://input'));
try {
$payload = json_decode($payload);
} catch (Exception $ex) {
echo $ex;
exit(0);
}
// put the branch you want here
if ($payload->ref != 'refs/heads/master') {
echo 'cabeçalho errado';
exit(0);
}
//put the branch you want here, as well as the directory your site is in
$op = array();
exec('cd /var/www/sgps && git fetch origin && git reset --hard origin/master && git merge origin/master', $op);
fwrite($arquivo, "\n---- FIM JSON PAYLOAD ---\n");
fwrite($arquivo, "\n---- INICIO RETORNO DE COMANDOS ---\n");
foreach ($op as $linha) {
fwrite($arquivo, $linha);
}
fclose($arquivo);
} else {
echo 'requisição falhou';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment