{{announcement.body}}
{{announcement.title}}

Mule 4 CI/CD Using GitLab Pipelines and JFrog Artifactory

DZone 's Guide to

Mule 4 CI/CD Using GitLab Pipelines and JFrog Artifactory

A practical CI/CD strategy for automating deployments of Mule 4 applications into CloudHub using GitLab CI/CD pipelines and JFrog Artifactory.

· Integration Zone ·
Free Resource

Introduction to CI/CD Methodologies

Continuous integration (CI) and continuous delivery (CD) defines a practice that enables application development teams to deliver code changes more frequently and reliably. The implementation is also known as the CI/CD pipeline. CI/CD is one of the best practices for DevOps teams to implement. It involves continuously building, testing, and deploying code changes at every small cycle, reducing the chance of developing new code based on bugged or failed previous versions.

Mule 4 CI/CD Framework Tool Set

For this example we will use the below applications to implement the CI/CD framework for deploying Mule 4 applications into CloudHub:

1. GitLab Enterprise Edition 12.1.6 on Red Hat Enterprise Linux (RHEL), alternatively you can use gitlab.com free tier

2. Apache Maven 3.6.3

3. JFrog Artifactory (OSS) version 7.4.3

4. Mule Application version 4.3.0

Below diagram describes the CI/CD flow:

maven repositories

Installations

To install GitLab EE and JFrog in your workstation please follow the steps mentioned in the below links:

Maven Installation: http://maven.apache.org/install.html 

GitLab EE Installation: https://about.gitlab.com/install/ 

JFrog Artifactory (OSS) Installation: https://www.jfrog.com/confluence/display/JFROG/Installing+Artifactory 

Purpose of GitLab

GitLab acts as a code repository as well as the CI/CD execution engine, we will leverage the GitLab CI feature which allows the creation of pipeline scripts to deploy the application by cloning from the code repository, compiling it, running MUnit (optionally) and finally deploying into the CloudHub. The component which executes the pipelines is called a GitLab Runner.

Purpose of JFrog Artifactory

Artifactory is required for resolving dependencies required for building your application. For example, a Parent POM file, or a shared library. It is also used as a mirror for multiple remote repositories using a single virtual artifactory repository.

Step 1: Set Up Your Artifactory

1. Login to artifactory using the admin user (default password is 'password' which is recommended to be changed after you log in).

http://{artifactory_host}:{artifactory_port}/ui/ where default artifactory_port is 8083

2. Go to Administration -> Repositories -> Remote (Tab) 

Add below Remote Repositories:

3. Go to Administration -> Repositories -> Virtual (Tab)

Add these 2 remote repositories in the virtual repository libs-release which will be created by default. So hereon, the virtual repository URL — http://{artifactory_host}:{artifactory_port}/artifactory/libs-release will be used to resolve the dependencies using Maven mirror configurations.

JFrog

3. Now edit your <<User Home>>/.m2/settings.xml if the file doesn't exist copy from <<Maven Home>>/conf to resolve all your dependencies using the artifactory mirror:

XML
 




x
1


1
<mirror>
2
    <id>jfrog_mirror</id>
3
    <mirrorOf>*</mirrorOf>
4
    <name>JFrog Artifactory Mirror for resolving Remote Repositories</name>
5
    <url>http://{artifactory_host}:{artifactory_port}/artifactory/libs-release/</url>
6
</mirror>



4. Encrypt the passwords of the artifactory users in the Maven settings.xml following the steps mentioned in the Apache Maven website:

https://maven.apache.org/guides/mini/guide-encryption.html

Add the server credentials using the encrypted passwords.

XML
 




xxxxxxxxxx
1
17


1
<servers>
2
    <server>
3
        <id>jfrog_mirror</id>
4
        <username>admin</username>
5
        <password>{4SiWJu2DwqAHa4fryKSeXTj5P6PqUqwm1rXJKA/m0O8=}</password>
6
    </server>
7
    <server>
8
        <id>jfrog_snapshot</id>
9
        <username>admin</username>
10
        <password>{4SiWJu2DwqAHa4fryKSeXTj5P6PqUqwm1rXJKA/m0O8=}</password>
11
    </server>
12
    <server>
13
        <id>jfrog_release</id>
14
        <username>admin</username>
15
        <password>{4SiWJu2DwqAHa4fryKSeXTj5P6PqUqwm1rXJKA/m0O8=}</password>
16
    </server>
17
</servers>



Note that the jfrog_mirror is the id of the mirror configuration. The other two credentials will be used later when we mention the artifactory as the distribution management server in the POM files.

This settings.xml needs to be shared with the Developers so they use it in their local workspace so you may use a different user instead of the admin which has sufficient privileges.

Step 2: Set Up a GitLab Runner With Shell Executor

GitLab Runner is the open-source project that is used to run your jobs and send the results back to GitLab. It is used in conjunction with GitLab CI/CD, the open-source continuous integration service included with GitLab that coordinates the jobs. Main features of the GitLab runner are:

  • Allows:
    • Running multiple jobs concurrently.
    • Using multiple tokens with multiple servers (even per-project).
    • Limiting the number of concurrent jobs per-token. Tokens will be specific to the GitLab group or individual repositories. 
  • Jobs can be run:
    • Locally.
    • Using Docker containers.
    • Using Docker containers and executing jobs over SSH.
    • Using Docker containers with autoscaling on different clouds and virtualization hypervisors.
    • Connecting to the remote SSH server.

According to your platform follow the steps mentioned in the GitLab website to install the GitLab runner in your server.

https://docs.gitlab.com/runner/install/

  • It is important to install Maven in the server where the GitLab runner is installed, for this example we will use the shell executor for the GitLab runner so that it will allow you to execute shell commands like mvn (for Maven).
  • To use the GitLab runner you need to register it with the GitLab instance (either your GitLab EE installation or ). Follow the steps to register your GitLab runner, for this example, we will register it with the GitLab group so it will be shared across all the repositories so in that case, it is called the group or shared runner; however, you may register your runner with a particular repository so it will be a private runner.
  • In case you want your runner to execute concurrent jobs, you can configure the same in the config.toml file. Ref: https://docs.gitlab.com/runner/configuration/advanced-configuration.html

GitLab

  • Create the runner with the tag — mule-integration so it can be referred from the pipeline script.

Step 3: Create a Parent POM and Deploy Into Artifactory

Maven parent POM (or super POM) is used to structure the project to avoid redundancies or duplicate configurations using inheritance between pom files. It helps in easy maintenance in the long term. In our example, we will use it to maintain the Mule Maven Plugin configuration.

1. Mention your artifactory host and port in the parent pom file. Note the properties mentioned in the CloudHub maven deployment plugin such as ${MULE_ORG}, ${MULE_USER}, etc. These will be configured in GitLab and should not be hard-coded in the POM file.

2. In Step 1 we created the credentials for the server — jfrog_release so make sure the same is mentioned in the <id> element of the distribution management repository in the parent pom.

XML
 




x


1
<project
2
    xmlns="http://maven.apache.org/POM/4.0.0"
3
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
 
          
5
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
6
    <modelVersion>4.0.0</modelVersion>
7
    <groupId>com.myorg</groupId>
8
    <artifactId>my-parent-pom</artifactId>
9
    <version>1.0.0</version>
10
    <packaging>pom</packaging>
11
    <profiles>
12
        <profile>
13
            <id>artifactReleaseDeployment</id>
14
            <distributionManagement>
15
                <repository>
16
                    <id>jfrog_release</id>
17
                    <name>artirelease</name>
18
                    <url>http://artifactoryhost:artifactoryport/artifactory/libs-release-local/</url>
19
                </repository>
20
            </distributionManagement>
21
        </profile>
22
        <profile>
23
            <id>cloudHubDeployment</id>
24
            <build>
25
                <plugins>
26
                    <plugin>
27
                        <groupId>org.mule.tools.maven</groupId>
28
                        <artifactId>mule-maven-plugin</artifactId>
29
                        <version>${mule.maven.plugin.version}</version>
30
                        <extensions>true</extensions>
31
                        <configuration>
32
                            <cloudHubDeployment>
33
                                <uri>https://anypoint.mulesoft.com</uri>
34
                                <muleVersion>${MULE_VERSION}</muleVersion>
35
                                <username>${MULE_USER}</username>
36
                                <password>${MULE_PASSWORD}</password>
37
                                <environment>${MULE_ENV}</environment>
38
                                <applicationName>${MULE_PROJECT_APP_NAME}</applicationName>
39
                                <region>${MULE_REGION}</region>
40
                                <businessGroup>${MULE_ORG}</businessGroup>
41
                                <properties>
42
                                    <anypoint.platform.analytics_base_uri>https://analytics-ingest.anypoint.mulesoft.com</anypoint.platform.analytics_base_uri>
43
                                    <anypoint.platform.client_id>${MULE_ENV_CLIENT_ID}</anypoint.platform.client_id>
44
                                    <anypoint.platform.client_secret>${MULE_ENV_CLIENT_SECRET}</anypoint.platform.client_secret>
45
                                    <anypoint.platform.platform_base_uri>https://anypoint.mulesoft.com/apiplatform</anypoint.platform.platform_base_uri>
46
                                    <mule.env>${MULE_ENV}</mule.env>
47
                                    <mule.key>${MULE_PROJECT_KEY}</mule.key>
48
                                </properties>
49
                                <workers>${MULE_WORKERS}</workers>
50
                                <workerType>${MULE_WORKER_TYPE}</workerType>
51
                            </cloudHubDeployment>
52
                        </configuration>
53
                    </plugin>
54
                </plugins>
55
            </build>
56
        </profile>
57
    </profiles>
58
</project>



3. Deploy the Parent POM file into the artifactory using the below maven command.

mvn -PartifactReleaseDeployment deploy

Step 4: Develop a Mule Application for CloudHub

  • Create a Mule Application for runtime 4.x that is supported by CloudHub. In your application, you can use secured properties specific to CloudHub environments. Make sure you inject the Parent POM reference in the POM file of your application under the <project> element.
XML
 




xxxxxxxxxx
1


1
<parent>
2
    <groupId>com.au.optus.ows</groupId>
3
    <artifactId>ows-parent-pom</artifactId>
4
    <version>1.0.0</version>
5
</parent>



  • In Step 3, we deployed the Parent POM into the libs-release repository in the artifactory. Note the application properties mentioned in the Parent POM, some of them are specific to CloudHub applications to connect to the API Manager for the auto-discovery feature; however, the two specific properties — ${mule.env} and ${mule.key} are meant to be used in your Mule application to refer to the environment-specific properties file i.e. — ${mule.env}.properties and ${mule.key} property can be used as the key to creating the secured properties.
XML
 




xxxxxxxxxx
1


 
1
<secure-properties:config name="Secure_Properties_Config" doc:name="Secure Properties Config" doc:id="5f81129b-31a0-40cc-a10e-af51413a6db2" file="properties/${mule.env}.properties" key="${mule.key}" />



  • As a part of coding best practice it is recommended to mask the sensitive information in the CloudHub application properties, so make sure the mule-artifact.json deployment descriptor file has the below section specified:
JSON
 




xxxxxxxxxx
1


 
1
"secureProperties": [
2
    "mule.key", "anypoint.platform.client_id", "anypoint.platform.client_secret"
3
  ]



After the application is deployed the properties will be masked as shown below:

runtime properties

Step 5: Setup GitLab Repository

  • Create a group and organize all the projects (repositories) containing Mule applications within the group.
    Group ---> Mule
    Projects ---> Project 1
                   ---> Project 2
  •  Following Step 2 create a GitLab shared or group runner and pair it with the Group -> Mule. Only the GitLab users with Maintainer or Owner permissions in the group will have the right to view or edit the Group level variables.
  • Add the following variables at the Group level.  Go to the GitLab group, click Settings -> CI/CD from the left panel, expand the Variables section to add the variables. Group level variables will be shared between all the Mule applications (Projects).
MULE_VERSION 4.3.0 or any other Mule 4.x version supported by CloudHub
MULE_USER Anypoint Platform user, preferably a service account user which has CloudHub Admin permission
MULE_PASSWORD Anypoint Platform User's Password
MULE_REGION CloudHub region ex — ap-southeast-2
MULE_ORG Your CloudHub Business Group name
MULE_DEV_CLIENT_ID Your CloudHub Environment's Client ID this is required in application properties to connect to the API manager
MULE_DEV_CLIENT_SECRET Your CloudHub Environment's Client Secret

mule

  • Some of the variables which are specific to the Mule application will be created at the Project level. This includes the key used to encrypt the secured properties, if you want to use different keys for the different environments you may name the variables — ${MULE_DEV_KEY}, ${MULE_SIT_KEY} etc. Go to GitLab project -> Settings -> CI/CD -> Expand variables.
    GitLab runner

Step 6: Script and Execute Your GitLab Pipeline

  • In Step 2, we made sure Maven is installed in the server where GitLab runner will execute as the runner will execute the shell script which in turn will execute maven commands to deploy our Mule application into CloudHub.
  • The variables we created at the group level and the project level will be accessible in the shell script and we will pass those into the Maven plugin as input in the maven command options.
  • Create a shell script build.sh and keep it in a folder accessible by your GitLab runner.  For example — /opt/build/build.sh, this script will be triggered by the GitLab pipeline job. Alternatively, you can keep the shell script in GitLab or JFrog and unpack it using the Maven unpack goal.
  • Shell
     







  • Create a Group level variable to refer to the script:

MULE_BUILDER /opt/build/build.sh

GitLab-ci.yml

GitLab CI/CD pipelines are configured using a YAML file called .gitlab-ci.yml within each project. The .gitlab-ci.yml file defines the structure and order of the pipelines and determines:

  • What to execute using GitLab Runner.
  • What decisions to make when specific conditions are encountered. For example, when a process succeeds or fails.

Check the GitLab documentation for the details: https://docs.gitlab.com/ee/ci/yaml/

In our case, we will define the GitLab-ci.yml as below:

YAML
 




xxxxxxxxxx
1
20


 
1
stages:
2
clean
3
deploy_dev
4
 
          
5
clean_mule:
6
  stage: clean
7
  tags:
8
    — mule-integration
9
  script:
10
    — $MULE_BUILDER clean
11
  when: manual
12
 
          
13
deploy_dev_mule:
14
  stage: deploy_dev
15
  tags:
16
    — mule-integration
17
  script:
18
    — $MULE_BUILDER deploy dev
19
  when: manual  
20
 
          



Similarly, you can include subsequent stages to deploy your Mule application to SIT,  UAT, and Production environments.

  • To execute the pipeline, go to the Project repository in GitLab -> Pipeline.
  • Click the 'Run Pipeline' option and select the appropriate branch for which you want to execute the pipeline.
    CI/CD pipeline
  • It will show the stages mentioned in your GitLab-ci.yml which you can execute by clicking those respectively. Note that in this example we have mentioned the steps as manual if the stages are not manual those will get executed on every code commit.
    test commit
  • You can click each stage and see the trace of the pipeline execution.
    gitlab

Security Considerations

If the user is allowed to edit the pipeline script any shell command can be executed as a part of the pipeline execution which creates a security vulnerability, also the user can easily do an 'echo' of the Group variables where we have stored the CloudHub credentials for deployments to display the values in the pipeline execution console. To address this issue GitLab has a feature to restrict Developers from editing the pipeline script as a security measure however the feature is only available in the Premium version.

Ref: https://gitlab.com/gitlab-org/gitlab-foss/-/issues/20826

Conclusion

This is a practical example of a CI/CD framework using GitLab pipelines as well as JFrog artifactory. Using this approach we could demonstrate the use of GitLab both as a code repository as well as a CI/CD platform. Since we could utilize the pipeline feature of GitLab we didn't require an automation server like Jenkins to execute the CI/CD jobs. This framework can be further enhanced to execute MUnit test cases, perform code analysis, deploy compiled JARs into JFrog artifactory, or integrate with automated testing tools.

Topics:
ci/cd, ci/cd pipeline, framework, gitlab, integration, jfrog

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}