Help Center/ CodeArts Build/ Best Practices/ Creating a Docker Image with a Maven Artifact and Pushing the Image to SWR (Built-in Executors, GUI)
Updated on 2024-11-21 GMT+08:00

Creating a Docker Image with a Maven Artifact and Pushing the Image to SWR (Built-in Executors, GUI)

Scenario

In this practice, you use CodeArts Build to build a Maven project. Afterward, you create a Dockerfile that outlines the process of packaging the Maven build outputs into a container image. Then, you build a Docker image from the Dockerfile and push the image to the image repository in SoftWare Repository for Container (SWR). The build output contained within the image can be used for future compilation or deployment purposes.

Requirements

Procedure

Table 1 Steps

Step

Description

Creating a Project

Create a project.

Creating a CodeArts Repo Repository

Create the code file used for your build.

Creating a Build Task

Create, configure, and run a build task.

Viewing the Build Results

Check the build logs and result files to verify the build results.

Creating a Project

  1. Log in to the Huawei Cloud console with your Huawei Cloud account.
  2. Click in the upper left corner and choose Developer Services > CodeArts from the service list.
  3. Click Access Service. The homepage of CodeArts is displayed.
  4. Click Create Project, and select the Scrum template.
  5. Set the project name to build-bestpractice, and leave the other parameters as default.
  6. Click OK to access the project.

Creating a CodeArts Repo Repository

  1. In the navigation pane, choose Code > Repo.
  2. On the displayed page, click New Repository. Select Template, and click Next.
  3. On the template selection page, select the Java Maven Demo template and click Next.
  4. On the displayed page, set Repository Name to build_docker_image_repo, and leave the other parameters as default. Click OK. The details page of the new code repository is displayed.
  5. In the root directory of the code repository, choose Create and select Create File from the drop-down list.

    Figure 1 Creating a file

  6. Name the file Dockerfile, copy the following code to the file (as shown in Figure 3), and click Submit.

    FROM ubuntu:latest
    
    # set maintainer
    LABEL maintainer=build
    
    RUN mkdir /release_app 
    COPY ./target/javaMavenDemo-1.0.jar /release_app/maven_app.jar
    
    USER build

    javaMavenDemo-1.0.jar is the file name in the format of ${artifactId}-${version}.${packaging} defined in the pom.xml file, as shown in Figure 2.

    Figure 2 pom.xml file
    Figure 3 Dockerfile content

Creating a Build Task

  1. In the navigation pane, choose CICD > Build.
  2. Click Create Task. On the displayed Basic Information page, set parameters according to Table 2. Then, click Next.

    Table 2 Basic information

    Parameter

    Description

    Name

    Assign a custom name to the build task, for example, build_docker_image_task.

    Code Source

    Select the code source from which code will be pulled for your build. In this practice, select Repo.

    Repository

    Select build_docker_image_repo, the code repository created in Creating a CodeArts Repo Repository.

    Default Branch

    Keep the default value master.

  3. Select Blank Template and click OK to create the build task. The Build Actions page is displayed.
  4. On the Build Actions page, click the GUI tab. Then click Add Build Actions on the left and add the Build with Maven action. Leave the parameters as default.
  5. Click Add Action. In the right pane, click the Container related tab. Hover over action Build Image and Push to SWR and click Add on the card. Set the parameters according to Figure 4.

    Expand the Organization drop-down list and select hwstaff_codeci_gray created to meet the requirements. Type release_image in the Image Name text box, and v1.0 in Image Tag. Leave the other parameters as default.
    Figure 4 Configuring the build action

  6. Click Save and Run in the upper right corner. In the displayed dialog box, click Confirm. The build task run page is displayed.

Viewing the Build Results

  1. On the Actions tab, check that the console displays the logs of the build task as it runs. The Build Logs window automatically scrolls down to show new entries. As shown in Figure 5, the build log window displays the information about the product of the Maven build.

    Figure 5 Information about the Maven build product

  2. After the build task is successfully run, go to the SWR console. Choose My Images and click the Private Images tab (which is displayed by default). From there, click the name of the image (release_image) created in 5 to access its details page.

    Figure 6 Image list