How to setup GitLab CI/CD on Fuga Cloud
gitlab ci-cd

How to setup GitLab CI/CD on Fuga Cloud

In this tutorial you will learn how to create a CI/CD pipeline that deploys to the Fuga Object Store using GitLab and GitLab runners. GitLab comes with built-in Continuous Integration, Continuous Deployment, and Continuous Delivery support to build, test, and deploy your application.

When a commit is made, a CI/CD pipeline will be started. GitLab will notify the runner that a new job is available. This runner will run all the tests defined for the project and will (re)deploy the application when the tests are successful. The flow looks like this:

GitLab ci flow

Requirements

Creating a new security group

To access GitLab by a browser outside of our local network, we need to open the ports 80 (HTTP) and 443 (HTTPS). This chapter will explain how to open these ports.

  1. On your Fuga Dashboard go to Compute -> Access and Security.

  2. Click on + Security group to create a new security group.

  3. Enter a name for the security group, for example webservers.

  4. Click on Create Security group to save the new group.

  5. Click on the Manage Rules button from the newly created security group.

  6. Click on + Add Rule in the top right corner of the screen.

  7. From the Rule drop-down list select HTTP and click Add.

  8. Repeat steps 6 and 7 for HTTPS.

Creating a Fuga Object Store container

This chapter will explain how to create a Fuga Object Store container which will be used for the deployment.

  1. Log in to your Fuga Cloud dashboard.

  2. Click on Containers and create a new container using the orange + Container button.

  3. Give it a fitting name and check the Public checkbox, so that we can access it without authentication later on. Please note that making your container Public allows everyone to access your files, you should change this after the tutorial.

  4. Click on the + Create button to add the container.

Installing GitLab on a Fuga instance

In this part of the tutorial installing GitLab and setting up the root account will be covered. In this tutorial we will use the Enterprise Editon (ce) of GitLab. However there is also an opensource version of GitLab, the Community Edition (ce). If you want to use that version instead, change the commands with ce in them to ee. For example apt install gitlab-ce to apt install gitlab-ee.

  1. Fire up an instance on Fuga that has enough performance for your needs. For this tutorial we will use a c1.medium with 2 cores and 4GB of RAM. Take a look at the GitLab Requirements. Ubuntu 18.04 is used in this tutorial.

  2. Click on Compute -> Instances.

  3. In the row of the GitLab instance, click on the down arrow right of the Create Snapshot button and click on Edit Security groups

  4. Our new security group should be on the left side of the popup under All Security Groups.

  5. Click on the + button and the security group should move to the right.

  6. Click Save to save the changes we made to our security.

  7. Log on into your instance using the following example command in your Terminal:

    ssh ubuntu@FLOATING_IP
    

    For other examples of SSH follow the Getting Started guide (55).

  8. Start by updating your repositories by using the command:

    sudo apt update
    
  9. Install required packages for GitLab with the following command:

    sudo apt install -y curl openssh-server ca-certificates
    
  10. Add the GitLab repository:

    curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
    
  11. Now it’s time to install GitLab, make sure to change the EXTERNAL_URL to your domain or the floating IP from your instance. This would for example be: http://gitlab.example.com or if using an IP: http://185.XX.XX.XX.

    sudo EXTERNAL_URL="http://gitlab.example.com" apt install gitlab-ce
    
  12. When GitLab is done installing, browse to the EXTERNAL_URL with your browser and setup your account. For example http://gitlab.example.com. The username of the first account, is called root.

Installing a GitLab Runner that uses Docker

After installing GitLab on an instance, the next step is installing and registering a GitLab runner to GitLab. This chapter will cover the installation and registration of a GitLab runner.

  1. Fire up another instance on Fuga that has enough performance for your GitLab runner. For the GitLab runner we will use a c1.small with Ubuntu 18.04.

  2. Log on into your GitLab runner instance using the following example command:

    ssh ubuntu@FLOATING_IP
    

    For other examples of connecting with SSH see Getting Started guide (55)

  3. Start by updating your repositories by using the command:

    sudo apt update
    
  4. Install Docker with the following command:

    sudo apt install docker.io
    
  5. Add the GitLab Runner repository:

    curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
    
  6. Now install the GitLab runner package:

    sudo apt install gitlab-runner
    
  7. Go to your GitLab URL with your browser and login with root and your chosen password.

  8. Click on Configure GitLab.

  9. Go to Overview -> Runners in the left menu bar. Keep this page open because we need the URL and registration token in the following steps.

  10. Start the registration of the GitLab runner in the GitLab runner instance:

    sudo gitlab-runner register
    
  11. When asked for your GitLab URL, enter the URL of your GitLab. For example http://gitlab.example.com or http://185.xx.xx.xx.

  12. When asked for a GitLab-ci token, enter the token on the page of step 9.

  13. When asked for a description of your GitLab runner, enter a fitting description.

  14. When asked for tags for your GitLab runner, enter tags that you deem fitting.

  15. When asked to lock your GitLab runner, keep it default; true.

  16. When asked for an Executor for your GitLab runner enter docker.

  17. When asked for a default Docker image, enter nginx. You can choose anything you like but for this tutorial the nginx image is used.

  18. The runner should have been registered successfully. Check your GitLab runners on your GitLab dashboard and it should look like this:

GitLab runner

Fuga Object Store tokens

To connect to your Fuga Object Store access and secret tokens are required. If you already have these you can skip this chapter.

  1. To list and create API keys for the Object Store we need the Fuga Openstack CLI. If you haven’t installed it yet checkout our tutorial.

  2. After following the tutorial for your operating system it’s time to list the credentials. We need the credentials to access our Object Store container in later steps.

  3. To list all the credentials, enter the command:

openstack ec2 credentials list
  1. If the list of credentials is empty, a new pair of credentials can be generated with:
openstack ec2 credentials create
  1. Either copy the Access and Secret key to clipboard or save them temporarily

Setting up CI/CD

It’s now time to setup the CI/CD pipeline within GitLab. The tokens of the previous chapter will be used here.

  1. To create a new project on your GitLab, click the plus icon located on the top of the screen next to the search bar and click New project.

  2. Give it a name and click on Create project.

  3. Open the new project and go to Settings -> CI/CD, which is located in the left menu bar.

  4. Expand the Secret variables menu and enter your access key as ACCESS_TOKEN and your secret key as SECRET_TOKEN. The values for these keys should be the tokens from the Fuga Object Store tokens chapter. It should look like the picture below: GitLab secret variables

  5. Click on Save variables.

  6. Now it’s time to add an SSH key to your GitLab root account, so we can actually push and pull to our GitLab. In the top right corner of the screen click on your profile picture with the down arrow and select Settings.

  7. Go to SSH Keys in the left menu bar.

  8. Copy the contents of your id_rsa.pub and enter a fitting title:

    cat .ssh/id_rsa.pub
    
  9. Click Add key.

  10. Clone your new project to your system using the git link located on the project page:

    git clone git@gitlab.example.com:root/your-project.git
    
  11. Open your new git folder inside your terminal:

    cd your-project
    
  12. Create a new file that will contain our CI/CD settings:

    touch .gitlab-ci.yml
    
  13. Open the file in your favorite text editor and copy paste the following code inside:

      image: nginx
      stages:
        - test
        - deploy
    
      test:
        stage: test
        script:
          - echo "This is the test stage."
    
      deploy:
        stage: deploy
        script:
          - apt update
          - apt install -y s3cmd
          - s3cmd --quiet --no-check-certificate --access_key "$ACCESS_TOKEN" --secret_key "$SECRET_TOKEN" --host object.api.fuga.io --host-bucket object.api.fuga.io/%\(bucket\) --signature-v2 --exclude ".git/*" put -r  ./ s3://YOUR_CONTAINER
    

    Make sure to replace YOUR_CONTAINER with the name of your container made in the ‘Creating a Fuga Object Store container’ chapter. The last command will upload all the files from your git repository to your Fuga Object Store container.

  14. Save the file and we’re now ready to test our CI/CD pipeline.

Testing the CI/CD pipeline

We will now test the CI/CD pipeline by creating a basic HTML file. If the pipeline works correctly we will see a live update.

  1. Next to your .gitlab-ci.yml file, create a new file called index.html:

    touch index.html
    
  2. Open index.html with your favorite text editor and paste in the follow example HTML:

    <html>
      <head>
        <title>CI/CD test</title>
      </head>
      <body>
        <h1>Hello, CI/CD!</h1>
      </body>
    </html>
    
  3. Add the .gitlab-ci.yml and index.html to your git staged changes:

    git add .
    
  4. Create a new git commit with these changes:

    git commit -m "Initial commit with .gitlab-ci and test html page."
    
  5. Push the changes to the GitLab:

    git push
    
  6. Go to your GitLab project and in the left menu bar click on CI/CD.

  7. A list of pipelines should appear like below: CI/CD pipeline

  8. There should be two green check marks marking both of our CI/CD stages as successful.

  9. Open a new tab in your webbrowser and go to http://object.api.fuga.io/swift/v1/YOUR_CONTAINER/index.html. Make to replace YOUR_CONTAINER with the name of your own container.

  10. The HTML that you committed to your Gitlab should be displaying now.

  11. You now have a working GitLab CI/CD base to improve further on! Don’t forget to change your container to private again if you don’t want to give everyone access to your files.

Was this article helpful?


Next article:

How to Autoscale your GitLab runners on OpenStack

Autoscaling your GitLab runners on Fuga has many advantages. For example, you can only have runners active during office hours or just when you need them. The unused runners will be removed automatically. Since Fuga Cloud is pay for what you use, your total costs for your GitLab CI/CD will go down. In this tutorial is explained on how to add autoscaling GitLab runners to your GitLab using GitLab CI and Docker Machine.