Code Monkey home page Code Monkey logo

git-hooks-post-receive's Introduction

Remote server

  • Creamos el directorio para nuestro cogido de despliegue
ssh [email protected]
mkdir ~/deploy
  • Añadimos un repositorio limpio de git
$ git init --bare ~/git-hooks-post-receive.git
  • Añadimos el hook post-receive en git
cd ~/git-hooks-post-receive.git/hooks
nano post-receive
chmod +x post-receive

Contenido del fichero post-receive:

#!/bin/bash
while read oldrev newrev ref
do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref =~ .*/production$ ]];
    then
        echo "Master ref received.  Deploying master branch to production..."
        git --work-tree=/home/ubuntu/deploy --git-dir=/home/ubuntu/git-hooks-post-receive.git checkout -f production
		cd /home/ubuntu/deploy
		slc build
		slc deploy http://localhost:8701 ../git-hooks-post-receive-0.0.1.tgz
    else
        echo "Ref $ref successfully received.  Doing nothing: only the production branch may be deployed on this server."
    fi
done

Otra alternativa del fichero post-receive:

#!/bin/bash

target_branch="production"
working_tree="deploy"

while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ -n "$branch" ] && [ "$target_branch" == "$branch" ]; then
    
       GIT_WORK_TREE=$working_tree git checkout $target_branch -f
       NOW=$(date +"%Y%m%d-%H%M")
       git tag release_$NOW $target_branch
    
       echo "   /==============================="
       echo "   | DEPLOYMENT COMPLETED"
       echo "   | Target branch: $target_branch"
       echo "   | Target folder: $working_tree"
       echo "   | Tag name     : release_$NOW"
       echo "   \=============================="
       slc build
	   slc deploy http://localhost:8701 ../git-hooks-post-receive-0.0.1.tgz
    fi
done

Nota

Fijemonos en la linea:

  git --work-tree=/home/ubuntu/deploy --git-dir=/home/ubuntu/git-hooks-post-receive.git checkout -f production

donde:

  • --work-tree será el directorio donde estará nuestro código a desplegar
  • --git-dir será el directorio donde estará el las carpetas propias de git

Local

  • Creamos un proyecto
git init git-hooks-post-receive
cd git-hooks-post-receive
npm init
npm install express --save
  • Editamos el fichero index.js
var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!porfin un pasito massssssssssssss');
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000');
});

  • Creamos la rama production y mezclamos con master
git branch -a production
git merge master
  • Conectar con server el git local
git add remote deploy_production [email protected]:git-hooks-post-receive.git
  • Empujar al servidor produccion
git push deploy_production

Nota

La conexion con el servidor será con ssh por lo que el servidor tendrá al menos un servidor que permita conexiones ssh para la comunicación con git del servidor. También puntualizar que hay que descartar de la url de conexion("[email protected]:git-hooks-post-receive.git") el "/home/ubuntu" ya que por defecto la conexion ssh nos situará en el directorio de trabajo del usuario con el que vamos a conectar.

git-hooks-post-receive's People

Contributors

valdepeace avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.