Level 1 Continuous Integration script
Tags: tutorial, beginners, devops, productivity
Published: 2020-05-05 15:43:58

---
title: Level 1 Continuous Integration script
published: true
description: Do you work with CI? You will with this simple script
tags: tutorial, beginners, devops, productivity
cover_image: https://www.redhat.com/cms/managed-files/ci-cd-flow-mobile_0.png
---

<meta name="monetization" content="$ilp.uphold.com/7QaxJkDwRZED">


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).

<strong>TL;DR: </strong> Simply copy and paste the entire script below into you <code>.gitlab-ci.yml</code>, set the <a href="#env-vars">environment variables</a> and that's all!

<pre>
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    

</pre>

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:
<code>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. </code>
- Defines a stage (called deploy in this case).
<pre>
stages:
   deploy

</pre>

- 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.
<pre>
  stage: deploy
  image: debian:stretch-slim
</pre>
- When to trigger this stage called deploy? 
<pre>
 only:
    master
</pre>
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 <strong>inside the docker image </strong> previously raised.

It will update remote repositories and install <strong>lftp</strong> (-y assuming yes if needed) without installing recommended things we don't need at this point (<a href="https://lftp.yar.ru/lftp-man.html">lftp documentation for further details</a>)
<pre>
script:
    - apt-get update && apt install -y --no-install-recommends lftp

</pre>


Then, It will transfer the project through FTP (on the default port 21) to the <strong>$HOST</strong>'s <strong>$REMOTE_ROUTE</strong> using <strong>$USER</strong> and <strong>$PASSWORD</strong> as authentication. 
<pre>
    - lftp -e  "set ftp:ssl-allow no; set ssl:verify-certificate no; mirror -R ./dist/ "$REMOTE_ROUTE -p 21 -u $USER,$PSWD $HOST    
</pre>
<span id="env-vars"> </span>

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 <a href="https://lftp.yar.ru/lftp-man.html">lftp documentation for further details</a>. 
Remember that saying that SFTP is FTP over SSH is quite inaccurate, if you are not familiar with FTP, SFTP or SSH please, <a href="https://en.wikipedia.org/wiki/SSH_File_Transfer_Protocol">visit this link.</a>

<h2>FOOTNOTES</h2>

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:

<pre>
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.    

</pre>

<h2>Recommendation for beginners </h2>

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 <strong>definitely go for <a href="https://manage.banahosting.com/aff.php?aff=1965"> THIS ONE </a></strong>, 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


<a href="https://joelbonetr.com/">JoelBonetR</a>