DevSecOps and Automation on Power

 View Only

Implement Red Hat OpenShift Pipelines as Code on GitLab on IBM Power

By Lakshmi Daruri posted 5 days ago

  

Welcome back to our blog series on Red Hat OpenShift Pipelines as Code! In our previous blogs, Red Hat OpenShift Pipelines as Code on IBM Power, Implement OpenShift Pipelines as Code with GitHub on IBM Power, and Implement OpenShift Pipelines as Code with Bitbucket we introduced you to the captivating world of OpenShift Pipelines as Code, gave you a glimpse of what's possible, and also provided detailed steps to make Pipelines as Code and GitHub/Bitbucket work together. In this blog, we will dive deeper into the partnership between OpenShift Pipelines as Code and GitLab, on IBM Power. 

Initially, Pipelines as Code was integrated with GitHub, and later extended to Bitbucket and GitLab. In this blog we will focus on implementation of Pipelines as Code on GitLab. 

The Pipelines as Code model creates automated processes that help developers build applications more efficiently. With Pipelines as Code, teams can configure builds, tests, and deployment in code that is trackable and stored in a centralized source repository.

GitLab supports open source repositories and offers self-hosted solutions but is more expensive than Bitbucket.

Implementing Pipelines as Code on IBM Power with GitLab

Before you are ready to roll, make sure you have a GitLab account. If you are not already a part of the GitLab community, head over to GitLab and follow the simple prompts to create your account.

OpenShift Pipelines as Code with GitLab is a continuous integration/continuous deliver (CI/CD) service built into GitLab. It allows you to automatically build, test, and deploy code based on a configuration file in the repository.

For more information, see  Pipelines-as-Code with GitLab  

Prerequisites

  • Install the latest version of OpenShift Pipelines Operator.
  • Deploy gosmee.

For detailed instructions on how to install the operator and deploy gosmee, check out our first blog in the series, Red Hat OpenShift Pipelines as Code on IBM Power.

Steps

Perform the following steps to implement Pipelines as Code with GitLab: 

  1. Create a GitLab repository using the GitLab UI. 
  2. Create a GitLab personal access token.
  3. Clone the GitLab repository to your local machine. 
  4. Configure webhook and create a repository.
  5. Commit your changes and push them to your remote repository.
  6. Raise a pull request or commit your changes to the main branch of your repository.

 Now, let us discuss each of these steps in detail. 

Step 1. Create a GitLab repository

The GitLab application integrates with OpenShift Pipelines and fetches GitLab workflow into Tekton pipelines.

You must ensure that the GitLab application's webhook points to your Pipelines as Code Controller route or the ingress endpoint which would listen to the GitLab events. 

As explained earlier, the very first step is to create a GitLab account. Go to https://gitlab.com/ and follow the simple prompts to create your account. Log in to your account and create a repository. 

  1. Go to GitLab repo and click New project/repository to create a new repository.
  1. Enter the name of the repository and click Create project.

Step 2. Create a personal access token

Perform the following steps to create a personal access token.

  1. In the upper-right corner, click your avatar.
  2. Click Edit profile.
  3. On the left navigation pane, click Access Tokens.
  4. Enter a name and expiry date (optional) for the token.
  5. Select api, read_user, and read_registry as the scopes.
  6. Click Create personal access token.

Note: Refer the following screenshot to select the scopes and create personal access token.

Step 3: Clone the GitLab repository to your local machine

 Clone the GitLab repo to your local machine using the following command:

# git clone <repo name> 
# cd <repo name> 

Step 4: Configure the webhook and create a repository 

Run the tkn pac create repo command to configure webhook and create the repository. Provide the necessary input when prompted. 

# tkn pac create repo
? Enter the Git repository url (default: https://gitlab.com:
? Please enter the namespace where the pipeline should run (default: pac-191): default
✓ Repository lakshmidaruri-pac-110 has been created in default namespace
✓ Setting up GitLab Webhook for Repository https://gitlab.com/
? Please enter the project ID for the repository you want to be configured,
  project ID refers to an unique ID (e.g. 34405323) shown at the top of your GitLab project : 4408xxx
👀 I have detected a controller url: https://pipelines-as-code-controller-OpenShift-pipelines.apps.ppc64le-qe21.psi.redhat.com
? Do you want me to use it? No
? Please enter your controller public route URL:  https://smee.io/LeB8PyT2gCGnsO9n
? Please enter the secret to configure the webhook for payload validation (default: GSYzLtAQFluu):  GSYzLtAQFluu
ℹ ️You now need to create a GitLab personal access token with `api` scope
ℹ ️Go to this URL to generate one https://gitlab.com/-/profile/personal_access_tokens, see https://is.gd/rOEo9B for documentation
? Please enter the GitLab access token:  **************************
? Please enter your GitLab API URL::  https://gitlab.com
✓ Webhook has been created on your repository
🔑 Webhook Secret lakshmidaruri-pac-110 has been created in the default namespace.
🔑 Repository CR lakshmidaruri-pac-110 has been updated with webhook secret in the default namespace
ℹ Directory .tekton has been created.
✓ A basic template has been created in /root/.tekton/pipelinerun.yaml, feel free to customize it.
ℹ You can test your pipeline by pushing generated template to your git repository

This creates .tekton folder with pipelinerun.yaml file in it
# ls -a
.  ..  .git  README.md  .tekton

This creates the ’.tekton’ folder, with the ‘pipelinerun.yaml’ file, which contains the default content to run the pipeline. It creates the GitLab repository and the secret in the given namespace on the OpenShift cluster. It also configures the webhook controller URL and the secret in the GitLab repo for you. 

# ls -a 
. .. .git .gitignore README.md .tekton 

Step 5: Commit the changes and push to the repository

Run the following commands to commit the changes locally and push the repository to the pac12 branch.

# git add .
# git commit -m”pac12”
[pipeline 98f780a] ”pac12”
 1 file changed, 89 insertions(+)
 create mode 100644 .tekton/pipelinerun.yaml

#git checkout -b pac12
fatal: A branch named 'pac12' already exists.
# git push origin  pac12
Username for 'https://gitlab.com': 
Password for 'https://@gitlab.com':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 20 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 1.31 KiB | 1.31 MiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To https://gitlab.com/
   6807a05..02593b0  pac12 -> pac12

 Step 6. Initiate a merge request/commit to the main branch of the repository

 To do so:

  1. Go to the repository and click Mergerequests in the left menu and click New Merge request.
  2. Select pac12 as the source branch and main as the destination branch and click Create Merge request.

This pull request triggers a pipeline. We can see pipeline execution in the GitLab repo as well as on the Red Hat OpenShift cluster.

When you click any of the tasks of the PipelineRun, you will be redirected to the Red Hat OpenShift cluster where the pipeline run takes place. 

On the Red Hat OpenShift cluster, you should be able to see as given in the following example:

Example:

In this is way, you can easily switch between the GitLab repository and the OpenShift cluster to observe the progress of pipelines.

Summary

This brings us to the end of this incredible blog series! We hope you are feeling more confident now that you've learned how to install OpenShift pipelines on the Red Hat OpenShift Cluster Platform on Power, create repositories on GitHub, GitLab, and Bitbucket. You have also learned to create the pipelinerun.yaml file in the Tekton folder and create pull requests to instantiate PipelineRun on the cluster.

Permalink