< img height="1" width="1" style="display:none;" alt="" src="https://px.ads.linkedin.com/collect/?pid=3131724&fmt=gif" />

Create a Multi-branch Pipeline with GitLab

GitLab is a web-based Git repository management tool that supports public and private repositories, and provides comprehensive DevOps functionalities including source code management, code review, issue tracking, continuous integration, and more. With GitLab, teams can collaborate efficiently on a single platform to complete the entire software development process from coding to deployment.

KubeSphere supports creating multi-branch pipelines using GitLab in DevOps projects. This document demonstrates how to create a multi-branch pipeline with GitLab.

Prerequisites

  • DevOps must have been installed and enabled.

  • A workspace, a DevOps project, and a user (e.g., project-regular) have been created, and the user has been invited to the DevOps project with the operator role. Refer to Role and Member Management.

  • You have a GitLab account and a Docker Hub account.

Step 1: Create Credentials

  1. Log in to the KubeSphere web console as the project-regular user.

  2. Click Workspace Management and enter your DevOps project, then create the following credentials under Credentials in DevOps Project Settings. For more information on how to create credentials, refer to Credential Management.

    Note

    If your account or password contains special characters such as @ and $, errors may occur during pipeline runs due to unrecognized characters. In such cases, encode your account or password on a third-party website (e.g., urlencoder) and then copy and paste the encoded result as your credential information.

    Credential IDTypeWhere to use

    dockerhub-id

    Username and password

    Docker Hub

    gitlab-id

    Username and password

    GitLab

  3. After creation, you will see the created credentials on the credentials page.

Step 2: Edit the Jenkinsfile in your GitLab Repository

  1. Log in to GitLab and create a public project. Click New Project > Import Project, select Import repository from URL, enter the URL of devops-maven-sample, choose the visibility level Public, and then click Create Project.

  2. In the newly created project, create a new branch from the v4.1.0 branch, named gitlab-demo.

  3. In the gitlab-demo branch, click the Jenkinsfile-online file in the root directory.

  4. Click Edit, change GITHUB_CREDENTIAL_ID, GITHUB_ACCOUNT, and @github.com to GITLAB_CREDENTIAL_ID, GITLAB_ACCOUNT, and @gitlab.com respectively, and edit the entries listed in the table below. Also, change the value of branch in push latest to gitlab-demo.

    EntryValueDescription

    GITLAB_CREDENTIAL_ID

    gitlab-id

    The name you set for your GitLab account in KubeSphere, used to push tags to your GitLab repository.

    DOCKERHUB_NAMESPACE

    your-dockerhub-id

    Replace with your Docker Hub account name, or the name of an organization under the account.

    GITLAB_ACCOUNT

    your-gitlab-id

    Replace with your GitLab account name, or the name of a user group under the account.

    Note

    For more information about environment variables in Jenkinsfile, refer to Create a Pipeline Using a Jenkinsfile.

  5. Click Commit changes to update the file.

Step 3: Create a Pipeline

  1. Log in to the KubeSphere web console as the project-regular user.

  2. Click Workspace Management and enter your DevOps project, then click Create on the Pipelines page.

  3. In the pop-up dialog, name it gitlab-multi-branch.

  4. Under Pipeline Category, select Multi-branch Pipeline.

  5. Under Code Repository, select a code repository and click Next to continue.

    If no code repository is available, click Create a code repository below. For more information, refer to Import Code Repositories.

    1. In the Import Code Repository dialog, enter a name for the code repository (customizable), then click to select the code repository.

    2. On the GitLab tab, select the default option https://gitlab.com under GitLab Server Address, enter the name of the group the GitLab project belongs to in Project Group/Owner, then select the devops-maven-sample repository from the dropdown menu under Code Repository. Click check in the bottom right corner, then click OK.

      Note

      To use a private GitLab repository, follow these steps:

      • Go to User Settings > Access Tokens on GitLab, create a personal access token with API and read_repository permissions.

      • Access Jenkins Dashboard, go to Manage Jenkins > Manage Credentials, create Jenkins credentials using your GitLab token for accessing GitLab. Then go to Manage Jenkins > Configure System, add the credentials in GitLab.

      • In the DevOps project, select DevOps Project Settings > Credentials, create a credential using your GitLab token. When creating the pipeline, specify this credential in the Credentials under the GitLab tab so that the pipeline can pull code from your private GitLab repository.

  6. On the Advanced Settings tab, change the Script Path to Jenkinsfile-online and then click Create.

    Note

    This field specifies the path of the Jenkinsfile in the code repository, which represents the root directory of the repository. If the file location changes, the script path also needs to be changed.

Step 4: Run the Pipeline

  1. After the pipeline is created, it will be displayed in the list. Click the pipeline name to view its details page.

  2. Click Run on the right. In the pop-up dialog, select gitlab-demo from the dropdown menu and add a tag number, such as v0.0.2. Click OK to trigger a new run.

  3. Wait a moment, then click the run record to view the details.

  4. If the pipeline reaches the Push with Tag stage, it will pause at this stage and require a user with approval permissions to click Proceed.

Step 5: Check Pipeline Status

  1. On the Pipeline tab of the run record, check the running status of the pipeline.

  2. Click the Run Logs tab to view the pipeline run logs. Click each stage to view its detailed logs. Click View Full Logs to troubleshoot and resolve issues based on the logs, or download the logs to your local machine for further analysis.

Step 6: Verify Results

  1. As defined in the Jenkinsfile, the Docker image built by the pipeline has also been successfully pushed to Docker Hub. In Docker Hub, you will see the image with the tag v0.0.2, which was specified before the pipeline run.

  2. Meanwhile, a new tag has been generated in GitLab.

Receive the latest news, articles and updates from KubeSphere


Thanks for the feedback. If you have a specific question about how to use KubeSphere, ask it on Slack. Open an issue in the GitHub repo if you want to report a problem or suggest an improvement.