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.zipThe above code will search the folders wp-content/themes/travelers-blog and put all the files of /travelers-blog in travelers-blog.zipgit archive --format=zip --prefix=...
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.
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.
stages: - MY_SITE_STAGING deploy: stage: MY_SITE_STAGING image: dotsunited/git-ftp script: - git config git-ftp.url "$HOST" - git config git-ftp.user "$FTP_STAGING_USERNAME" - git config git-ftp.password "$FTP_STAGING_PASSWORD" - git ftp init only: - master
Environment variables: username & password
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:
- Go to your project on gitlab.com
- 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_PASSWORD so we’ll define their values here:
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:
Once you have made your first push to the server, you will need to change some lines on the
.gitlab-ci.yml file and replace
git ftp init to
git ftp push
1111stages: - MY_SITE_STAGING deploy: stage: MY_SITE_STAGING image: dotsunited/git-ftp script: - git config git-ftp.url "$HOST" - git config git-ftp.user "$FTP_STAGING_USERNAME" - git config git-ftp.password "$FTP_STAGING_PASSWORD" - git ftp push only: - master
Some Useful Information
- Always add
.gitignorefile to the root of the directory. It will ignore all the files that are excluded in the
git-ftpwill 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.