Automate your deploys using gitlab runner CI/CD

Introduction

GitLab is a web-based DevOps lifecycle tool that provides a Git repository manager, continuous integration (CI), continuous delivery (CD), and a range of collaboration and project management features. It is used to manage the software development process, facilitating collaboration among team members, version control with Git, and automated build and deployment pipelines. In this blog, I will show you how to create an automated process capable of checking out, compiling, and deploying your Liferay code using GitLab.

 

What do we need to setup ?

You will need a GitLab repository, either from gitlab.com or your own local installation, where you can create and manage your projects.

Create a project inside GitLab with a name of your choice, such as 'liferay-cicd-demo-project.'

After creating the project, you need to push the Liferay workspace to your Git repository. For instance, you could use the same name for the Git project, like 'liferay-cicd-demo-project.'

Once the project is created and pushed, you will need to perform some configurations to enable communication between your machine and GitLab.

 

Install gitlab runner in your machine

You should navigate to your GitLab project, then go to Settings > CI/CD > Runners, and click on the expand button.

 

Next to the 'New project runner' button, you'll find a three-dots icon; click on it to view the expanded menu.

 

 

Click on "Show runner installation and registration instructions" and choose your operating system. For this example i will consider Linux.

 

You can copy all these lines and execute them on your machine one by one. In the last line where it says 'gitlab.com' and '<token>', the values associated are specific to your Git project.

 

Register your runner in your machine

When executing the last line of the previous command, it will prompt for some information. For the Registration name and tag, you can use something not autogenerated to make it easier to reference later in a pipeline.

sudo gitlab-runner register --url https://gitlab.com/ --registration-token GR1348941hNNk4382zPsyyM Runtime platform                                    arch=amd64 os=darwin pid=10894 revision=f86890c6 version=15.8.1 Running in system-mode.

Created missing unique system ID                    system_id=s_1e2e418230d4
Enter the GitLab instance URL (for example, https://gitlab.com/):
[https://gitlab.com/]:
Enter the registration token:
[GR1348941hNNk4382zPsyyM]:
Enter a description for the runner:
liferay-cicd-demo-project-runner
Enter tags for the runner (comma-separated):
liferay-cicd-demo-project-runner
Enter optional maintenance note for the runner:

WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org/gitlab/-/issues/380872
Registering runner... succeeded                     runner=GR1348941hNNkTppX
Enter an executor: instance, docker-ssh, parallels, shell, docker+machine, docker-ssh+machine, kubernetes, custom, docker, ssh, virtualbox:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

 

After successfully executing the command, it will create a new runner with a green signal on the same page in GitLab.

 

Create a pipeline

Now that you have already created the connection between you machine and gitlab, you need to create a pipeline to be executed whenever you what (every commit on a specific branch, every new git tag, every rule that you define)

To do this, you need to create a file in the root of your project called .gitlab-cy.yaml

The next example shows how to create a pipeline to be executed every commit in the main branch

stages:
  - build-liferay

before_script:
  - if [[ $CI_COMMIT_MESSAGE == *NOTCI* ]]; then exit 0; fi

#running in the runner
build-liferay:
  stage: build-liferay
  script: 
    - echo "Building lf"
    - cd liferay-cicd-demo-project && ./gradlew deploy && if [ -d ./bundles ]; then cp ./bundles/osgi/modules/* /opt/liferay/deploy/ && cp ./bundles/deploy/* /opt/liferay/deploy/; fi
  cache:
    key: $CI_COMMIT_REF_NAME
    policy: push
    paths:
      - build
      - .gradle
  tags:
    - liferay-cicd-demo-project-runner
  only:
    - main

 

Conclusion

With this setup and configuration, your machine will listen for commits in the main branch and then automatically check out your code, compile, and deploy it. This automated approach eliminates the need for manual compilation, making it well-suited for use in test or UAT environments and avoiding human interaction in the process of generating and copying the JAR files to the project. This entire process can also be accomplished using Docker containers running a GitLab runner. Personally, I find this option appealing as it provides a fully containerized system ready to build, deploy, and serve the project with all components interconnected.

 

If you are interested in learning more about this topic, please feel free to comment or send a message.

 

Danie Dias 
Head of Liferay Business Unit at PDMFC
daniel.dias@pdmfc.com