List of successful jobs run on GitLab

Intro

Using Continuous Integration ( also known as CI ), can save hours of time spent building and deploying code. CI also eliminates the need to share credentials and keys with multiple developers. This guide will explain how to automate the process of uploading changes to a server using GitLab.

Setup FTP

It is recommended to create an FTP account specifically for GitLab to use. Set the FTP account's path to the same path that you want to upload files into. To improve security the password should only contain alphanumeric characters so that it can be masked on GitLab.

Set Variables

Environment variables set within project settingsUsing environment variables allows the FTP credentials to remain hidden and secure. To set the environment variables navigate to your repository and then Settings -> CI / CD. This job requires FTP_HOST, FTP_USER, and FTP_PASS to be set with your FTP credentials.

Create .gitlab-ci.yml

To start using CI on GitLab you must add a .gitlab-ci.yml file to the root of your project repository.

before_script:
  - apt-get update -qq

upload:
  stage: deploy
  script:
    - apt-get install lftp -y
    - lftp -e "mirror -R dist/ ./ ; quit" -u $FTP_USER,$FTP_PASS $FTP_HOST
  only:
    - master

The first section tells the Runner to run apt-get update before each job, this is good practice.

before_script:
  - apt-get update -qq

Declare a job named upload and set the job's stage. GitLab automatically runs any job with a stage of build, test, or deploy and in that order.

upload:
  stage: deploy

Declare the commands to be run during this job, these commands are executed in the order listed. The first command installs lftp, the second uses lftp to upload files to the server. Supply lftp with the FTP host, username and password; the -e "..." parameter describes which FTP commands to run. In this case the dist/ directory is mirrored recursively into the FTP root directory, then quit lftp.

  script:
    - apt-get install lftp -y
    - lftp -e "mirror -R dist/ ./ ; quit" -u $FTP_USER,$FTP_PASS $FTP_HOST

If you are using FTPS, change line 8 to the code below.

    - lftp -e "set ftp:ssl-protect-data true ; mirror -R dist/ ./ ; quit" -u $FTP_USER,$FTP_PASS $FTP_HOST

Lastly, declare that this job is only run on the master branch of the repository, if your workflow uses a production branch use that instead.

  only:
    - master

Push to Branch

Commit the .gitlab-ci.yml file and then push your branch to GitLab, this will immediately trigger a new pipeline which will run each of the jobs listed in the .gitlab-ci.yml file.

Navigate to the CI / CD page of your repository to see the status of the pipeline, select a specific job to view the terminal output in real-time. If everything goes to plan the job will pass and the server files will have been updated. Errors in the YAML file or the commands run during a job can cause a failure; to debug these errors simply navigate to the jobs page, select the failed job and then review the terminal output.

Useful Links

About GitLab Continuous Integration
Getting started with GitLab CI/CD
LFTP manual page
YAML syntax

May 12th 2019

Nicholas Horlocker

continuous integration, ci, ftp, gitlab, devops