Level 1 Continuous Integration script

JoelBonetR 🥇 - May 5 '20 - - Dev Community

If you aren't using CI scripts yet or you've this scripts on your company's repositories but never thought about it, i'll let you some basics on a simple way to reach it.

I'm using a simple shared hosting I've for testing purposes, this fact put limits on the ways i can set up my CI script, i'll detail this part later.

First of all, the script, which i named .gitlab-ci.yml (as i'm using GitLab, each git server will have their own name by default or let you choose yours).

TL;DR: Simply copy and paste the entire script below into you .gitlab-ci.yml, set the environment variables and that's all!

stages:
  - deploy

deploy:
  stage: deploy
  image: debian:stretch-slim
  only:
    - master

  script:
    - apt-get update && apt install -y --no-install-recommends lftp
    - lftp -e  "set ftp:ssl-allow no; set ssl:verify-certificate no; mirror -R ./dist/ "$REMOTE_ROUTE -p 21 -u $USER,$PSWD $HOST    

What does this CI script do?

  • "Programmagically" deploys the project on desired hosting when a push to master is performed (by something with rights to push to master or when a merge request to master is accepted).

Ok, now let's explain it in a more accurate way:
I had to delete the hyphens because DEV editor interpret them as list items on short snippets, don't know why exactly. The complete script has the correct syntax.

  • Defines a stage (called deploy in this case).
    stages:
    deploy
  • On the deploy stage we define an image (this will generate a docker container with the chosen OS on it, and the project inside it). In this case i'm using debian:stetch-slim. Fast to raise, low weight... I use it almost always.
    stage: deploy
    image: debian:stretch-slim
    
  • When to trigger this stage called deploy?
    only:
    master
    
    When something is pushed into [branch name], in the example we use master branch.
  • What the script will do?
  • * note that the script will run inside the docker image previously raised.

It will update remote repositories and install lftp (-y assuming yes if needed) without installing recommended things we don't need at this point (lftp documentation for further details)

script:
    - apt-get update && apt install -y --no-install-recommends lftp

Then, It will transfer the project through FTP (on the default port 21) to the $HOST's $REMOTE_ROUTE using $USER and $PASSWORD as authentication.

    - lftp -e  "set ftp:ssl-allow no; set ssl:verify-certificate no; mirror -R ./dist/ "$REMOTE_ROUTE -p 21 -u $USER,$PSWD $HOST    

The $keyword are environment variables related to a project, on gitLab, you can find where to set them on: Settings -> CI/CD -> Variables

Here's when i have limitations using a shared hosting; i cannot use an SSH connection, instead I've two options: FTP or SFTP.
If you are on a Cloud server, VPS or similar, you will be able to use SSH too, apart from SFTP, FTP and so.

You can - and must - use sFTP or SSH. Again, see lftp documentation for further details.
Remember that saying that SFTP is FTP over SSH is quite inaccurate, if you are not familiar with FTP, SFTP or SSH please, visit this link.

FOOTNOTES

I'm using FTP here as I need to add a private key to use SFTP (and this is only a shared hosting for testing purposes, mainly css + js, so all code is public and available online anyway). By the other hand i think this is probably the easiest script possible, which could help you start with DevOps tasks and test it using a free gitlab account with a cheap shared hosting (BTW some of them don't give SFTP connection), for example to publish your nice static portfolio or for testing reasons if you like it or you are a student or junior that wants something simple that want to test some web languages (php, html, css, js etc) and not environments.

If you're in a company or managing user data, or trying to add CI to your private project please, use SFTP or SSH connection instead of FTP for security reasons.

I'll add some SFTP client configuration instructions to use with an SFTP client, this could help you understand how it works or test SFTP connections before use it on a CI script:

SFTP clients typically require the following information to connect to a server:

Hostname — The server’s hostname (for example, hostname.example.com).

SSH port number — The port number on which sshd listens (for example, 22).

Security — Whether the client combines FTP and SFTP functionality.

Username — The SSH username that the client uses to connect to the server. The username can be a valid cPanel account username or the root user.

Important: *
  You cannot use an FTP account to connect via SFTP.

Password — The SSH user’s password.
Private Key — The SSH user’s private key. This is the absolute path to a private key on your local computer (for example, c:\data\id_dsa).

Important: *
You may see a warning that the server’s host key is unknown. To ensure that this message does not display again, accept the key and store it on your local computer.    

Recommendation for beginners

If you are learning Web Development I recommend you to use a basic shared hosting at the beginning as it gives you the power to try different configurations but you are not able to break the entire server. Also you will have your code available online and publish it wherever you are.

After trying like 40 different hosting companies on last 10 years I definitely go for THIS ONE , which has a C-Panel and lets you throw some console commands (with limitations) at less than 5 U$D a month (click Web Hosting button on the menu to see shared hosting plans).

If any doubt or if this post helped you in some manner, even if you didn't like it, please, comment below to keep on touch

JoelBonetR

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .