Automate Full CI/CD to Cube with GitLab Runner: Step‑by‑Step Guide
This tutorial walks you through integrating Cube's API into a GitLab CI pipeline, covering runner setup, custom update commands, YAML template preparation, and a complete .gitlab-ci.yml example to achieve fully automated deployment from code push to Cube.
In the previous episode we demonstrated how to package a Java application as an image and deploy it to Cube using a demo website. This episode shows how to integrate Cube’s API into a GitLab CI pipeline to achieve fully automated continuous deployment from code push to Cube.
Key Steps
Install and register a GitLab Runner with admin privileges; use the Shell executor and ensure the runner user can run Docker commands (sudoer rights are sufficient for non‑production).
Create a custom update_cube command (source code examples are available in the community group or via the upcoming UCLI tool).
Prepare a Cube YAML template: generate a Cube resource, remove runtime‑specific fields, replace the image tag with a placeholder, and store it in the repository, e.g. <PROJECT_ROOT>/yaml/my_cube_template.yaml.
Define a GitLab CI configuration that builds the Docker image, pushes it, and triggers the Cube update. Example .gitlab-ci.yml:
apiVersion: v1beta1
kind: Pod
metadata:
provider:
containerCount: 1
region: cn-sh2
resource:
cpu: 500
memory: 1024
subnetId: subnet-XXX
vpcId: uvnet-XXX
zone: cn-sh2-01
spec:
containers:
- env:
- name: <ENV_VAR_KEY>
value: <ENV_VAR_VALUE>
image: 'uhub.service.ucloud.cn/gary/cloudgame-image:<IMAGE_TAG>'
name: cube03
resources:
limits:
cpu: 500m
memory: 1Gi
volumeMounts:
- mountPath: /etc/appconfig
name: config
restartPolicy: Always variables:
MAVEN_CLI_OPTS: "--batch-mode"
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
cache:
paths:
- .m2/repository/
- target/
buildToIntegration:
stage: build
except:
- tags
script:
- mvn $MAVEN_CLI_OPTS package
- sudo docker build -t uhub.service.ucloud.cn/gary/cloudgame-image:SNAP-$CI_PIPELINE_ID .
- sudo docker push uhub.service.ucloud.cn/gary/cloudgame-image:SNAP-$CI_PIPELINE_ID
deployToIntegration:
stage: deploy
except:
- tags
script:
- cat yaml/my_cube_template.yaml | sed "s/<IMAGE_TAG>/SNAP-$CI_PIPELINE_ID/" > /tmp/cube_SNAP-$CI_PIPELINE_ID.yaml
- /cubeclient/update_cube -yamlfile /tmp/cube_SNAP-$CI_PIPELINE_ID.yaml -cube <cube_XXXXX>After pushing code to GitLab, the pipeline automatically builds the image, pushes it to the registry, substitutes the image tag in the Cube template, and updates the Cube instance.
Feel free to leave comments or questions for further assistance.
UCloud Tech
UCloud is a leading neutral cloud provider in China, developing its own IaaS, PaaS, AI service platform, and big data exchange platform, and delivering comprehensive industry solutions for public, private, hybrid, and dedicated clouds.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
