jeff-h
jeff-h

Reputation: 2639

How do I set up a very simple deployment using a Gitlab CI runner?

I'd like to automatically deploy new work from my Gitlab repository to a live website running on a production server. The live website is a GIT repo clone of the live branch.

My questions:

My .gitlab-ci.yml file:

deploy_to_production:
  script:
    - deploy.sh
  only:
    - live
  tags:
    - prod

Details

To do this, I want to run a simple shell script that I wrote (deploy.sh) on the production server, whenever I push to a specific branch of my repo (the live branch, in my case). This shell script is in my GIT repository, alongside my .gitlab-ci.yml.

The script basically just does a git reset, fetch and pull, bringing the production version up to date with what's in the repository.

I have installed a "shell" multi-runner on my server, and connected that to gitlab.com, where I can see it active.

Overall, I am barking up the wrong tree? Should I change my deploy.sh so that it doesn't do a git checkout of the new work, but rather uses cp or perhaps rsync to move the new code from ~/builds/... into the production website?

Upvotes: 3

Views: 3533

Answers (1)

tmt
tmt

Reputation: 8654

Q: Is the cloning a mandatory behavior?

As far as I know it is because the assumption is that during the build process you work with the code.

If that's not your case and you only want to "tell the production server" to pull the code from your repo, then maybe you can take a different approach and use a webhook instead. GitLab would make a HTTP request to your server and pass the information about the event (push, merge, etc.) in the JSON format. The receiving script would then parse it and make a decision whether to do the git pull.

If you do have other tasks to perform on the code during the build process before deployment, then yes, alter the strategy and use the code that the runner has already checked out for you. I'd probably attempt to have the change atomic in some way (rsyncing into the live directory would leave your application in some uncertain state when it runs and in a mess if it fails for some reason), e.g. by having the live code in a directory that is symlinked. The deployment script would then copy the new code directory to its final destination, make any necessary adjustments and change the symlink's target from the old live directory to the new one.

Q: If it really has to clone the repo each time, why doesn't it git reset it?

That's more of a question for the developers. Anybody else's answer would be just speculation.

Q: How do I run my deploy.sh which is in the repository root?

If the deploy.sh script has the executable bit set, then use ./deploy.sh, otherwise bash deploy.sh.

Upvotes: 4

Related Questions