A
A
Al2016-04-27 15:07:27
git
Al, 2016-04-27 15:07:27

How to auto-deploy when pushing to a specific branch in a repository?

Let's say we have Gitlab, with the test.git repository . It has many branches, and the two main ones are master and qa . During the development process, developers create their own branches that correspond to the tasks, then all this is merged through merge requests into the dev branch . When you need to roll out a release, there is a need to deploy to a test server from the qa branch , and when the test is successful, you need to deploy to production from the master branch .
Let's assume that the test server and the production server are on the same machine. At the moment, after all the changes from dev to qa have been merged, after pushing, I go to the server and execute a command there, something like this:

git archive --remote=/git-data/repositories/root/test.git qa | tar -x -C /test-server/sites/test/

If we tested it and everything is OK, the qa branch is merged into master , and I again go to the server and execute the following command:
git archive --remote=/git-data/repositories/root/test.git master | tar -x -C /prod-server/sites/test/

It's all stressful, and I want to automate this process. I would like to create some kind of bash script (for example, just drive the same git archive into it for now), which will automatically run after making changes to the master or qa branch . For example, after pushing to qa , the /home/testuser/deplloy-qa.sh script will work , and after pushing to master , the /home/testuser/deplloy-master.sh script will work . Or, it would be possible to execute such scripts not automatically when pushing, but from the Gitlab interface (which is more interesting, and in my opinion more convenient), there are such things as Builds / Runners - I don’t know if they can be used to solve my task.
I would be grateful for useful answers, I would like to understand and see on my example how to solve the problem, both in the first and second ways.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vayho, 2016-04-27
@vayho

Use CI - Jenkins/Teamcity etc.

S
Sergey, 2016-04-27
@Andr1an

You need to make a post-receive hook, git passes arguments to it in stdin in this order: oldrev newrev ref. Here is an example:

#!/bin/bash
while read oldrev newrev ref; do
    branch=${ref##*/}
    if ; then
        # вот он, ваш мастер
    elif ; then
        # qa
    fi
done

A
Arthur, 2016-08-26
@ArturF

If still relevant, then look towards using the built-in CI. You will need an SSH runner to access the target machine, and the deployment script will need to be uploaded to the repository

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question