I’ve been using GitLab CI more and more to replace the traditional Filezilla method. When you use git-FTP with Gitlab, all the process of transfer of files will be automatic. You don’t need to frequently move the files to your server manually.
Getting started
To start using GitLab CI you have to create a .gitlab-ci.yml file in the root directory of your project. This file tells GitLab what Docker image it should use and what steps to go through.
Note: I will assume that you know how to push the files to the Gitlab repository.
Copy and paste below code on to your .gitlab-ci.yml file and save it.
Before we can test our CI pipeline, we have to store our FTP username and password somewhere. Putting it in the gitlab-ci.yml file would be very insecure because everyone can see it (especially if your code is open source).
Instead, you should store it as an environment variable:
In the sidebar, go to “Settings” and open “CI / CD”
Now enter your credentials in the “Environment variables” section. In the git-ftp command, we used $FTP_STAGING_USERNAME and $FTP_STAGING_PASSWORD so we’ll define their values here:
Configuring the FTP credentials as environment variables in GitLab CI.
Note that here we don’t include the dollar sign $ . It’s also wise to enable the “Protected” options.
Click on “Save variables” and now we’re ready to test it out!
Testing the GitLab CI pipeline
To test your pipeline you just have to commit some new changes to your git repository and push them to GitLab. Alternatively, you can also trigger a build manually by going to “CI / CD” and clicking on “Run Pipeline”.
If everything was configured correctly you should see your job running successfully:
Lastly
Once you have made your first push to the server, you will need to change some lines on the .gitlab-ci.yml file.
Edit the .gitlab-ci.yml file and replace git ftp init to git ftp push
Always add .gitignore file to the root of the directory. It will ignore all the files that are excluded in the .gitignore file.
git-ftp will transfer one file at a time, so if you have more than 3000 files which WordPress does have. It will take some time to complete the file transfer. In your first commit, you will have more files so it will be slow at first and then afterward it will only transfer files that have been changed/modified.
There are times when you might want to use git to zip files. I am a WordPress Developer so below codes are taken from one of my projects. git archive --format zip HEAD:wp-content/themes/travelers-blog > travelers-blog.zip The above code will search the folders wp-content/themes/travelers-blog and put all the files of /travelers-blog in travelers-blog.zip git archive --format=zip --prefix=...