The Ultimate Guide: Getting Started with GitLab

With lots of software development and testing around different applications and products you certainly need a best way to deploy it in effective and in best way. With So many microservices and code it becomes very crucial for any developer or system engineers to collaborate and make a successful product ready.

Managing the code is now very well taken care by Git which is distributed code repository but on the top of it deployment has been very effective and easily managed with the help of GitLab

In this tutorial you will learn all about GitLab , Managing Pipelines , Projects and many more which a devops engineer should know to get started.

Table of Content

  1. What is GitLab?
  2. Prerequisites
  3. Creating Projects on GitLab
  4. Creating a Repository on GitLab
  5. Creating a Branch on GitLab
  6. Get started with GitLab CI/CD Pipelines
  7. Pipeline Architecture
  8. Conclusion

What is GitLab?

Git is a distributed version control designed to handle small to large projects with speed and efficiency. On the top of Git , GitLab is fully integrated platform to manage devops lifecycle or software developments.

It is single application to manage entire DevOps lifecycle.

Prerequisites

  • You should have GitLab account handy. If you don’t have create it from here

Creating Projects on GitLab

GitLab projects hold all the files , folders , code and all the documents you need to build your applications.

  • To create a project in GitLab click on Projects on the top and then click on Create a Project
  • Now click on Create blank project
  • On the Blank project tab provide the Project name and as this is demo we will keep this repository Private.
  • Now Project is successfully created.
  • You are ready to upload files either manually create/upload on GitLab
  • Also you can push the files using command line by cloning the repository and adding the files as show below.
git clone https://gitlab.com/XXXXXXXXX/XXXXX.git
cd firstgitlab
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

Creating a Repository on GitLab

A repository is a place where you store all your code and related files. It is part of a Project. You can create multiple repositories in a single project.

To create a new repository, all you need to do is create a new project or fork an existing project. Once you create a new project, you can add new files via UI or via command line.

Creating a Branch on GitLab

  • By Now, you saw GitLab Project creation. By default if you add any file it will be checked in master branch.
  • Click on New file and then select Dockerfile and add content and then commit the file by adding the comments.
  • You will see that Dockerfile is now added in master branch under FirstGitLab project.
  • So far we created a file which by default gets added in master branch. But if you need a separate Branch click on the Branches and then hit New Branch.
  • Provide a name for the new branch.

Get started with GitLab CI/CD Pipelines

Before you start CI/CD part on GitLab make sure to have following

  • runners : runners are agents that run your CI/CD jobs. To check the available runners Go to Settings > CI/CD and expand Runners. As long as you have at least one active available runner then you will be able to run the Job.
  • .gitlab-ci.yml file : In this file you define your CI/CD jobs , decisions which runner should take with specific conditions, structure of job and order of Jobs. Go to Project overview and then click on New file & name it as .gitlab-ci.yml
  • Now Paste the below content
build-job: 
    stage: build 
    script:
       - echo "Hello, $GITLAB_USER_LOGIN"
test-job:
    stage: test
    script: 
       - echo "Testing CI/CD Pipeline"
deploy-job:
    stage: deploy
    script:
       - echo "Deploy from the $CI_COMMIT_BRANCH branch" 
  • Now Pipeline should automatically trigger for this pipeline configuration. Click on Pipelines to validate and View status of pipeline.
  • To view details of a job, click the job name, for example build.
  • Pipelines can be scheduled to run automatically as and when required.

Pipeline Architecture

Pipelines are the fundamental building blocks for CI/CD in GitLab. There are three main ways to structure your pipelines, each with their own advantages. These methods can be mixed and matched if needed:

  • Basic: Good for straightforward projects where all the configuration are stored at one place. This is the simplest pipeline in GitLab. It runs everything in the build stage at the same time and once all of those finish, it runs everything in the test stage the same way, and so on.

If Build A is completed it waits for BUILD B and once both are completed it moves to next TEST STAGE. Similarly if TEST B is completed it will wait for TEST A and then once both are completed they move to DEPLOY STAGE.

Directed Acyclic Graph: Good for large, complex projects that need efficient execution and you want everything to run as quickly as possible.

If Build A and TEST A both are completed it moves to next DEPLOY STAGE even if TEST B is still running

Child/Parent Pipelines: Good for monorepos and projects with lots of independently defined components. This job is run mostly using trigger keyword.

Conclusion

GitLab is the first single application for software development, security, and operations that enables continuous DevOps. GitLab makes the software lifecycle faster and improves the speed of business.

GitLab provides solutions for each of the stages of the DevOps lifecycle. So Which application are you going to build ?

Hope you had learnt a lot from this guide and helped you. If you like please share.

The Ultimate Guide: Getting Started with Jenkins Pipeline

Are you struggling to automate the application deployment? Why not use Jenkins and Jenkins Pipeline to automate all the deployments more easily and effectively.

With Jenkins Pipeline, the deployment process is earlier and gives you more features to incorporate while deploying or managing the infrastructure.

In this tutorial, you will learn what is Jenkins Pipeline in depth. Let’s get started.

Join 28 other followers

Table of Content

  1. What is CI/CD ( Continuous Integration and Continuous deployments)?
  2. What is Jenkins Pipeline?
  3. How to create a basic Jenkins Pipeline
  4. Handling Parameters in Jenkins Pipeline
  5. How to work with Input Parameters
  6. Conclusion

Prerequisites

What is CI/CD ( Continuous Integration and Continuous deployments)?

CI/CD products are delivered to clients ingeniously and effectively using different automated stages. CI/CD saves tons of time for both developer and operations team, and there are very few chances of human errors. CI/CD stands for continuous integration and continuous deployments. It automates everything starting from integrating to deployments.

Continuous Integration and Continuous deployments
Continuous Integration and Continuous deployments

What is Continuous Integration?

Continuous integration is primarily used by developers so that developers’ code is built, tested, and then pushed to a shared repository whenever there is a change in code.

For every code push to the repository, you can create a set of scripts to build and test your application automatically. These scripts help decrease the chances that you introduce errors in your application.

This practice is known as Continuous Integration. Each change submitted to an application, even to development branches, is built and tested automatically and continuously.

What is Continuous Delivery?

Continuous delivery is a step beyond continuous integration. With Continuous Delivery, the application is not only continuously built and tested each time the code is pushed but the application is also deployed continuously. However, with continuous delivery, you trigger the deployments manually.

Continuous delivery checks the code automatically, but it requires human intervention to deploy the changes.

What is Continuous Deployment

Continuous deployment is again a step beyond continuous integration and the only difference between deployment and delivery is: deployment automatically takes the code from a shared repository and deploys the changes to environments such as Production where customers can see those changes.

This is the final stage of the CI/CD pipeline. With Continous Deployment, it hardly takes a minute to deploy the code to the environments. It depends on heavy pre-automation testing.

Examples of CI/CD :

  • Spinnaker and Screwdriver built platform for CD
  • GitLab, Bamboo, CircleCI, Travis CI and GoCD are built platform for CI/CD.

What is Jenkins Pipeline?

Jenkins Pipeline uses a group of plugins that help deliver a complete continuous delivery pipeline starting from building the code till deployment of the software right up to the customer.

Jenkins Pipeline plugin is automatically installed while installing the Jenkins with suggested plugins and allows you to write complex operations and code deployment as code using DSL language ( Domain-specific language). Some of the benefits of the Jenkins pipeline are:

Jenkinsfile ( declarative vs scripted pipeline)

Jenkins pipeline is written in code with Jenkinsfile that is easier, gives more ability to review, and supports various extensions & plugins. Also if Jenkins stops you can still continue to write or update the Jenkinsfile. With code capabilities you can allow waiting, approvals, stop, and many other functionalities. Jenkinsfile follows two syntaxes:

  • Declarative Pipeline: This is newer and writing code with this is much easier.
  • Scripted Pipeline: This is older and writing code with this is a little complicated.

Scripted pipeline syntax

Jenkins provides you an easier way to generate the Scripted pipeline syntax by navigating to the below URL.

http://Jenkins-server:8080/pipeline-syntax/
Scripted pipeline syntax
Scripted pipeline syntax

Declarative Pipeline syntax

Jenkins also provides you a way to generate the Declarative pipeline syntax by navigating to the below URL.

http://Jenkins-server:8080/directive-generator/
Declarative Pipeline syntax
Declarative Pipeline syntax

Jenkins variables

Let’s quickly look at the Jenkins Pipeline environmental variables that are supported.

  • BUILD_NUMBER: Displays the build number
  • BUILD_TAG: Displays the tag which is jenkins-${JOB_NAME}-${BUILD_NUMBER}
  • BUILD_URL: Displays the URL of the result of Build
  • JAVA_HOME: Path of Java home
  • NODE_NAME: It specifics the name of the node. For example, set it to master is for Jenkins controller
  • JOB_NAME: Name of the Job

You can set the environmental variables dynamically in the Jenkins pipeline as well.

    environment {
        AWS_ACCESS_KEY_ID     = credentials('jenkins-aws-secret-key-id')
        AWS_SECRET_ACCESS_KEY = credentials('jenkins-aws-secret-access-key')
        MY_KUBECONFIG = credentials('my-kubeconfig')
   }

Jenkinsfile example

Previously you learned about Jenkinsfile syntax and variables that can be included in Jenkinfile. In this section lets learn by seeing a basic example declarative pipeline. 

Below are all the arguments that are used within the Jenkinsfile and their function is specified below.

  • agent: Agent allows Jenkins to allocate an executor or a node. For example Jenkins slave
  • Stages: It include multiple tasks which Pipeline needs to perform. It can have a single task as well.
  • Stage: Stage is one single task under stages.
  • steps: These are steps which needs to be executed in every stage.
  • sh: sh is one of the step which executes shell command.
pipeline {
   agent any 
    stages {
        stage('Testing the Jenkins Version') {
            steps {
                echo 'Hello, Jenkins'
                sh 'service jenkins status'
               //  sh("kubectl --kubeconfig $MY_KUBECONFIG get pods")
            }
        }
    }
}

Jenkins Job example to run Jenkins Pipeline

In this section, lets quickly learn how you can execute jenkins job with Jenkinsfile and Jenkins Pipeline. Lets create Jenkins job quickly.

  • Navigate to Jenkins URL and click on New Item.
Click on new item in Jenkins Job
Click on new item in Jenkins Job
  • Next, choose Pipeline as the type of Jenkins job from the option as shown below, provide the of Jenkins Job as pipeline-demo and click OK.
Choosing the Pipeline and naming the Jenkins Job
Choosing the Pipeline and naming the Jenkins Job
  • Now in the Jenkins job provide the description as my demo pipeline.
  • Further in the script copy/paste the below code.
pipeline {
   agent any 
    stages {
        stage('Testing the Jenkins Version') {
            steps {
                echo 'Hello, Jenkins'
                sh 'service jenkins status'
            }
        }
    }
}
  • Finally click on save & click on Build Now button.
Running the Jenkins Job to run Jenkins Pipeline
Running the Jenkins Job to run Jenkins Pipeline

Now that you have successfully run your first Jenkins Job using Jenkins Pipeline. lets verify the code execution from console output of the Job and to do that click on the build number from build history.

Build History of Jenkins Job
Build History of Jenkins Job
Verifying the Jenkins Job using console output
Verifying the Jenkins Job using console output

Handling Parameters in Jenkins Pipeline

If you wish to use Build with Parameters , so those parameters are accessible using params keyword in pipeline.

Lets see a quick example. In below code we have Profile as a parameter and it can be accessed as ${params.Profile} .Lets paste the code in pipeline script as we did earlier

pipeline {
  agent any
  parameters {
    string(name: 'Profile', defaultValue: 'devops-engineer', description: 'I am devops guy') 
}
 stages {
    stage('Testing DEVOPS') {
       steps {
          echo "${params.Profile} is a cloud profile"
       }
     }
   }
}
  • Lets build the Jenkins pipeline now.
  • Next verify the console output
  • Similarly we can use different parameters such
pipeline {
    agent any
    parameters {
        string(name: 'PERSON', defaultValue: 'AutomateInfra', description: 'PERSON')
        text(name: 'BIOGRAPHY', defaultValue: '', description: 'BIOGRAPHY')
        booleanParam(name: 'TOGGLE', defaultValue: true, description: 'TOGGLE')
        choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'CHOICE')
        password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'PASSWORD')
    }
    stages {
        stage('All-Parameters') {
            steps {
                echo "I am ${params.PERSON}"
                echo "Biography: ${params.BIOGRAPHY}"
                echo "Toggle: ${params.TOGGLE}"
                echo "Choice: ${params.CHOICE}"
                echo "Password: ${params.PASSWORD}"
            }
        }
    }
}

How to work with Input Parameters

Input parameter allows you to provide an input using a input step. Unless input is provided the pipeline will be paused. Lets see a quick example in which Jenkins job will prompt for “should we continue” message. Unless we approve it will remain as it is else finally it will abort.

pipeline {
    agent any
    stages {
        stage('Testing input condition') {
            input {
                message "Should we continue?"
                ok "Yes, we should."
                submitter "automateinfra"
                parameters {
                    string(name: 'PERSON', defaultValue: 'Automate', description: 'Person')
                }
            }
            steps {
                echo "Hello, ${PERSON}, nice to meet you."
            }
        }
    }
}
  • Lets paste the content in Jenkins pipeline script and click on build now.
  • Let us verify by clicking on build Now

Conclusion

In this tutorial we learnt what is CI/CD and CI/CD open source tool Jenkins. We covered how to write pipeline and syntax of Jenkins pipeline using its language known as DSL ( domain specific language ) . Also we learnt in depth of Jenkins pipeline and created basic Jenkins pipeline and executed it.

Hope this tutorial will help you a kick start to how to work with Jenkins pipeline and execute them. If you like this please share it.