The most simple gitlab-ci file to deploy your commits – Devops

Usually, we have a git repositori setup in a test server where we execute a git pull every time we want to deploy code changes to the this server.

We want to automate “git pull” and many other task in every commit to our git server branch (master), using gitlab devops. We have created a file in our test server that contains all tasks we execute in a deploy.

We need to have a “runner”  in our gitlab instance.

After runner installation, you have to register Runner in your project. Go where you intalled runner (gitlab server) and follow this intructions :

To register a runner under Linux (gitlab server):

  1. Run the following command:

    sudo gitlab-runner register
  2. Enter your GitLab instance URL (also known as the gitlab-ci coordinator URL).
  3. Enter the token you obtained to register the runner.
  4. Enter a description for the runner. You can change this value later in the GitLab user interface.
  5. Enter the tags associated with the runner, separated by commas. You can change this value later in the GitLab user interface.
  6. Provide the runner executor. For most use cases, enter docker.
  7. If you entered docker as your executor, you’ll be asked for the default image to be used for projects that do not define one in .gitlab-ci.yml.

In our test server we have to setup ssh key copying the user ssh public key into ~ssh/authorized_keys  file.

In Gitlab->projects->Settings->CI / CD, you have to add the following variables :

  • SSH_HOSTS  = test-server-ip-or-domain
  • SSH_PRIVATE_KEY = user ssh private key (you should create new one for this job)

Then your gitlab-ci.yml would look like this : 

You must change $SSH_HOST by your ip test server and <your-domain> 

  ## Install ssh-agent if not already installed, it is required by Docker.
  ## (change apt-get to yum if you use an RPM-based image)
  - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'

  ## Run ssh-agent (inside the build environment)
  - eval $(ssh-agent -s)

  ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
  ## We're using tr to fix line endings which makes ed25519 keys work
  ## without extra base64 encoding.
  ##- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
  - ssh-add <(echo "$SSH_PRIVATE_KEY")
  - ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts

  ## Create the SSH directory and give it the right permissions
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh

  ## Optionally, if you will be using any Git commands, set the user name and
  ## and email.
  # - git config --global ""
  # - git config --global "User name"
  type: deploy
    name: Production
    url: <your-domain>
    - ssh user@$SSH_HOST "./ && exit"
    - master

That’s all folks








Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s