Updated on 2025-07-30 GMT+08:00

Building Images and Pushing Them to SWR

CodeArts Build provides a hybrid language build platform with simple configurations, supports one-click task creation, configuration, and execution, and automates activities such as code obtaining, building, and packaging.

Deployment failures often occur due to inconsistent environments. For example, the upgraded JDK in the R&D debugging environment is not marked in the environment list. As a result, the production environment is not upgraded accordingly, causing failures. To avoid problems caused by environment inconsistency, we package microservice applications and environments into images in this example to ensure that the environments (development and debugging, test, QA, and production) are consistent.

This section describes how developer Chris builds and archives images to SWR.

Preset Tasks

The sample project presets five build tasks.

Table 1 Preset tasks

Preset Task

Description

phoenix-sample-ci

Basic build task

phoenix-sample-ci-test

For building images in the test environment

phoenix-sample-ci-worker

For creating a Worker function image

phoenix-sample-ci-result

For creating a Result function image

phoenix-sample-ci-vote

For creating a Vote function image

This section uses the phoenix-sample-ci task as an example. The built-in actions and their functions are described in the table below. After configuring the task parameters, you can execute the task.

Table 2 Build actions

Build Action

Description

Build Vote Image and Push It to SWR

Create a Vote image using the vote/Dockerfile file, and push the image to SWR for storage.

Build Result Image and Push It to SWR

Create a Result image using the result/Dockerfile file, and push the image to SWR for storage.

Install Worker Dependency Using Maven

Use Maven to install the dependencies required by the Worker function.

Build Worker Image and Push It to SWR

Create a Worker image using the worker/Dockerfile file, and push the image to SWR for storage.

Create Postgres and Redis Dockerfiles

Use shell commands to generate a Dockerfile for creating Postgres (database) and Redis (cache) images.

Build Postgres Image and Push It to SWR

Create a Postgres image using the Dockerfile generated in action Create Postgres and Redis Dockerfiles, and push the image to SWR for storage.

Build Redis Image and Push It to SWR

Create a Redis image using the Dockerfile generated in action Create Postgres and Redis Dockerfiles, and push the image to SWR for storage.

Replace Image Version of Docker-Compose Deployment File

To ensure that the correct image can be pulled and then deployed on ECS, run shell commands to perform the following operations:

  • Run sed commands to replace the values of parameters in docker-compose-standalone.yml with those of dockerServer, dockerOrg, and BUILDNUMBER in the build task in sequence.
  • Run the tar command to compress the docker-compose-standalone.yml file into the docker-stack.tar.gz file. Compress the files required for deployment so that the files can be uploaded and archived in subsequent steps.

Replace Image Version of Kubernetes Deployment File

To ensure that the correct image can be pulled and then deployed on CCE, run shell commands to perform the following operations:

  • Run the sed commands to replace the values of docker-server and docker-org in all the files whose names end with deployment in the kompose directory with those of dockerServer and dockerOrg in the build task.
  • Run the sed commands to replace the value of image-version in result-deployment.yaml, vote-deployment.yaml, and worker-deployment.yaml with the value of BUILDNUMBER.

Upload Kubernetes Deployment File to Release Repo

Upload all .yaml files modified in action Replace Image Version of Kubernetes Deployment File to the release repo for archiving.

Upload Docker-Compose Deployment File to Release Repo

Upload the docker-stack.tar.gz package generated in action Replace Image Version of Docker-Compose Deployment File to the release repo for archiving.

Configuring and Executing a Task

  1. Go to the Phoenix Mall project, and choose CICD > Build from the navigation pane.
  2. In the Operation column of the phoenix-sample-ci task, click and choose Edit.
  3. Click the Parameters tab, and set default values for the parameters listed in the following table.

    Table 3 Setting parameters

    Name

    Default Value

    codeBranch

    master

    dockerOrg

    The name of the organization created in Configuring SWR

    version

    1.0.0

    dockerServer

    The SWR server address obtained in Configuring SWR

  4. Click Save and Execute. In the displayed dialog box, click OK to start the build task.

    If is displayed, the task is successfully executed. Record the character string starting with # (for example, ).

    If the task fails, rectify the fault based on the failed action and the error message in logs. For details, see CodeArts Build FAQs.

  5. Check the build artifact.

    1. In the navigation pane, choose Artifact > Release Repos.
    2. In the repository named after the project, find the docker-stack and phoenix-sample-ci folders.
      • In the docker-stack folder, find the folder named after the character string recorded in 4. Then find the build artifact docker-stack.tar.gz in this folder.
      • In the phoenix-sample-ci/1.0.0 folder, find the 10 archived .yaml files.
      Figure 1 Checking the release repo
    3. Go to the SWR console, choose Organizations from the navigation pane, and click the name of the organization created in Configuring SWR.

      Click the Images tab. Then find the five images (redis, postgres, worker, result, and vote) in the list.

      Figure 2 Viewing SWR
    4. Click the names of the five images in sequence to go to their details page. View the image tag on the Tags tab page.
      • The tag of redis is alpine.
      • The tag of postgres is 9.4.
      • The tags of worker, result, and vote are the same as those recorded in 4.

Configuring Auto Compilation upon Code Commits

After the following configuration is complete, code changes will automatically trigger the application build task, achieving continuous integration.

  1. Go to the Phoenix Mall project, and choose CICD > Build from the navigation pane.
  2. In the Operation column of the phoenix-sample-ci task, click and choose Edit.
  3. Click the Schedule tab, toggle on next to Run upon Code Commit, and click Save.

    Since the default value of codeBranch on the Parameters tab is master, the build is automatically run when the code in master is changed.

    Figure 3 Configuring the execution plan

  4. Modify the project code and commit it to master. Then check whether the build task is automatically executed.

Configuring Scheduled Execution

You can perform the following operations to schedule the build task for a specific time:

  1. On the details page of task phoenix-sample-ci, click Edit.
  2. Click the Schedule tab.
  3. Enable Scheduled Execution ( means enabled), select days and time, disable Run upon code change, and click Save.

    For this example, select All, and set the execution time to 12:00. (This example uses the default time zone. You can change it to the one you use.)

    Figure 4 Scheduling executions

  4. Check whether the build task is automatically executed at the configured time.