☁️Using GitHub Access Token for CodeArts Pipeline Automation

Ertugrul Basar
Huawei Developers
Published in
9 min readMar 20, 2024
CodeArts — GitHub

Introduction

Hi everyone, in today’s article, we’ll be focusing on using the infamous code repository GitHub and Huawei Cloud’s DevSecOps platform, CodeArts. In this exercise, we will attempt to automate processes using the CodeArts Pipeline, much like we have already demonstrated in previous articles. However, this time, we will utilize a GitHub repository as our source code, and the commits we make there will trigger the pipeline. Also, I want to emphasize the GitHub trigger aspect in this article, rather than other aspects such as building and deploying the code.

Before we begin the demo, although I expect you to be already familiar with these services, I will briefly overview CodeArts and GitHub.

1️⃣ ECS: Elastic Cloud Server (ECS) is a versatile and scalable cloud computing service that empowers users to create and manage virtual servers in the cloud.

2️⃣ CodeArts: CodeArts is a one-stop DevSecOps platform that provides out-of-the-box cloud services for requirement delivery, code commit, check, build, verification, deployment, and release throughout the entire software lifecycle. It consists of the following services: Req (Project Management), Repo (Code Hosting), Check (Code Quality), Build (Code Build), Artifact (Build Hosting), Deploy (Build Deployment), Pipeline (Continuous Delivery), TestPlan (Deployment Test).

3️⃣ GitHub: GitHub is a cloud-based platform where you can store, share, and work together with others to write code. Storing your code in a “repository” on GitHub allows you to showcase or share your work, track and manage changes to your code, and collaborate on a shared project with others.

Demo

Preliminaries

⚡️ CodeArts project with ECS added to basic resources

We need an ECS instance onto which we are going to deploy our application. Additionally, if we want our application to be accessible from the internet, we need to associate an Elastic IP (EIP) so that the server will have a public IP address. If you’re unsure how to procure an ECS instance, you can refer to this guide.

ECS Instance

To utilize the ECS within our project, we need to incorporate it as a basic resource. After creating the project, navigate to ‘Settings > General > Basic Resources’ to establish a host cluster. This process enables us to include the ECS instances we’ve generated in the project. Once the cluster is created, proceed to add the ECS as a basic resource within this project.

Basic Resources

⚡️ GitHub repository

As we will be inspecting the commits made in GitHub, we require a repository to store our code. Here is the repository I’ll be utilizing for this exercise, which you may also choose to use. (You can also locate it as a template in the CodeArts Repo under the name ‘Java Web Demo’)

GitHub Repository

Step 1️⃣: Generate a GitHub access token and add it as a service endpoint in a CodeArts project

This step is perhaps the most crucial for this exercise. Typically, CodeArts cannot access your private repositories on other Git providers. However, by using a token as a service endpoint, CodeArts can gain access to your repositories on GitHub. Therefore, we need to navigate to ‘Settings > Developer settings > Personal access tokens > Fine-grained tokens’ on GitHub. Once there, click on ‘Generate new token’.

Access Token — 1

Next, we’ll give our token a name. By default, the expiration time for a token is 30 days, which I’ll maintain, but you can also customize it. Finally, we’ll adjust the repository access settings, which determine the token’s attributes on the account. Here, you can specify whether the token should have access to only public repositories, all repositories, or only selected ones. For this exercise, I’ll select only the repository we’ll be using. Also, ensure that you grant all the necessary permissions to the token.

Access Token — 2

After the token is generated, you’ll be redirected to a page where your token is displayed. It’s crucial to copy and securely note down your token at this point because GitHub won’t allow you to view it again.

Access Token — 3

Now that we have generated the token, we can utilize it in our CodeArts project. Navigate to ‘Settings > General > Service Endpoints’ in your CodeArts project, then click on ‘Create Endpoint’. A dropdown menu will appear; select ‘GitHub’ from the options.

Service Endpoints — 1

A pop-up will appear where we’ll select ‘Access token’ as the authentication method and use the token we generated earlier. Then, click on ‘Verify and OK’.

Service Endpoints — 2

After it’s verified, as depicted in the screenshot below, you’ll be able to see the endpoint.

Service Endpoints — 3

Step 2️⃣: Create a build task

Now that we’ve established the connection to our GitHub repository within our CodeArts project, we can proceed to build the application itself. Navigate to ‘CICD > Build’ and then click on ‘Create Task’.

Build Task — 1

Then, we need to name the task and select its source code. Since we’re importing the code from GitHub, we need to choose GitHub as the code source. Next, we’ll select the endpoint we created earlier. After that, we can choose the source code repository and its branch.

Build Task — 2

Next, we will select the Maven template and click on ‘Next’. This template will automatically add the required steps to the task.

Build Task — 3

Here we can see the automatically configured steps for a Maven project. This eliminates the need to configure each step individually, simplifying the process. Now, we can simply click on ‘Create and Run’.

Build Task — 4

Then, a new page will appear where you can observe the logs in real time. As you can see, our task successfully finished in under a minute.

Build Task — 5

You can also view the build result in CodeArts Artifact. Navigate to ‘Artifact > Release Repos’ to see the package created from the build task.

Build Package

Step 3️⃣: Create a deploy task

We’ve completed the build task and obtained the package. Now, let’s create a deploy task to make our application accessible from the internet. Navigate to ‘CICD > Deploy’ and click on ‘Create Application’.

Deploy Task — 1

Here, set the name of the task and then click on ‘Next’.

Deploy Task — 2

Next, we will select a template called ‘Deploy a Spring Boot Application’, and similar to the build task, it will automatically configure some of the steps in our task.

Deploy Task — 3

After the deploy task page appears, click on ‘Environment Management’.

Deploy Task — 4

On the environment management page, click on ‘Create Environment’ to create an environment and add the ECS instance to that environment. Once added, it should resemble the screenshot below. After configuring, click on ‘Parameters’.

Deploy Task — 5

In this Spring Boot template, some parameters need to be filled in. Adjust the parameter settings as indicated in the screenshot below. Then, click on ‘Deployment Actions’.

Deploy Task — 6

Here in these steps, make the same configurations as me step by step.

Install JDK: We don’t make any changes here

Deploy Task — 7

Select a Deployment Source: Here, we will choose ‘Build task’ as the source. Then, select the build task we created earlier. For the download path, we can use ‘/usr/local’.

Deploy Task — 8

Stop SpringBoot: Make sure that we have the same absolute path in this step.

Deploy Task — 9

Start SpringBoot: Make sure that we have the same absolute path in this step.

Deploy Task — 10

Health Test Through URLs: We will disable this step in this exercise.

After all of the configurations, we can click on ‘Save & Deploy’, and a pop-up will appear. We will fill out the blank form as shown in the screenshot below and then click on ‘OK’.

Deploy Task — 11

As we did with the build task, we can observe real-time logs here as well. As you can see, the task finished successfully in just around a minute.

Deploy Task — 12

You can also refer to the screenshot below and observe that we can access the application from the Internet.

Application Page — 1

Step 4️⃣: Create a pipeline task

The application is live, and now let’s see how we can create a pipeline using these build and deploy tasks. Navigate to ‘CICD > Pipeline’ and click on ‘Create Pipeline’.

Pipeline Task — 1

Here, we’ll name the pipeline and then select the repository, similar to what we did when creating the build task.

Pipeline Task — 2

Here, we will continue with the blank template.

Pipeline Task — 3

The pipeline is ready, and now we will configure it. Configure the pipeline as shown in the screenshot below, and then click on ‘Execution Parameters’.

Pipeline Task — 4

Here, we will enable the code commit option so that when we make a commit in our GitHub repository, this pipeline will run. Click on ‘Save’, and then let’s navigate to GitHub.

Pipeline Task — 5

Next, I’ll make a small change and then commit it.

GitHub Commit

After committing, the pipeline starts automatically without any delay. This is exactly what we wanted to achieve with this exercise.

Pipeline Task — 6

Now, let’s examine the changes from the browser. The pipeline has successfully finished, but is there any difference? Let’s find out… As depicted in the screenshot below, the differences are visible, and our exercise is now complete.

Application Page — 2

Conclusion

In this exercise, we’ve generated a GitHub access token and incorporated it as a service endpoint in the CodeArts project. This enabled us to utilize the repository within CodeArts and monitor commits to trigger the pipeline. This scenario can be particularly useful when you need to maintain your source code in GitHub while also leveraging CodeArts services. (I’d recommend keeping your code in the CodeArts Repo as well for additional trigger options, etc.) This article can serve as a fundamental guide, and you can adapt and customize this task to suit your specific tasks and business requirements. Until the next article, take care!

References

--

--