How to Deploy to FTP Server from Gitlab Repository?

We can easily deploy our code from Gitlab repository to our FTP server by using Gitlab CI (Continuous Integration).

But What is Gitlab CI?

GitLab CI/CD is a part of GitLab, a web application with an API that stores its state in a database. It manages projects/builds and provides a nice user interface, besides all the features of GitLab. It is very useful for automating your deployment workflow.

It can do lot of things like Continuous Integration, Continuous Deployment, and Continuous Delivery support to build, test, and deploy your application.

But, today we gonna automate our task of Uploading last changed files to our FTP server by configuring Gitlab CI.

Let’s start by first creating a file named .gitlab-ci.yml in your root folder.

Then add the following code in your .gitlab-ci.yml file.

variables:
  HOST: "example.com"
  USERNAME: "yourUserNameHere"
  PASSWORD: "yourPasswordHere"

deploy:
  script:
    - apt-get update -qq && apt-get install -y -qq lftp
    - lftp -c "set ftp:ssl-allow no; open -u $USERNAME,$PASSWORD $HOST; mirror -Rnev ./ ./public_html --ignore-time --parallel=10 --exclude-glob .git* --exclude .git/"
  only:
    - master

The above code will push all your recently modified files in your Gitlab repository into public_html folder in your FTP Server root.

Just update the variables HOST, USERNAME and PASSWORD with your FTP Credentials and commit this file to your Gitlab Repository, you are good to go.

Now whenever you make changes in your master branch, Gitlab will automatically push your changes to your remote FTP server.