How to automate your CI/CD pipeline with Walrus and GitLab

Walrus file is a new feature released in Walrus 0.5. It allows you to describe applications and configure infrastructure resources using condensed YAML.

Then you can execute walrus apply in the Walrus CLI or import it to the Walrus UI. This will send the Walrus file to the Walrus server, which will deploy, configure and manage the applications and infrastructure resources. This makes them easy to reuse in multiple environments.

In this tutorial, we’ll show how to integrate the Walrus CLI with GitLab CI and release applications via a Walrus file to improve the CI/CD pipeline.

Prerequisites

Before starting, prepare the following:

  1. Create a new project on GitLab and import our demo project into it. First, make sure you have the GitHub type Import Project permission enabled. If not, watch the video below and enable it in the Admin Area.demoAlternatively, you can manually git clone project and push a new GitLab project into it.
  2. Install Walrus via docker runand expose additional ports 30000~30100 for NodePort of embedded k3s cluster workload, for more information: https://seal-io.github.io/docs/deploy/standalone
sudo docker run -d --privileged --restart=always -p 80:80 -p 443:443 -p 30000-30100:30000-30100 --name walrus sealio/walrus:v0.5.1

3. Access to Walrus. In the latest version, Walrus automatically creates a local environment in default project and adds embedded clusters of K3s or other K8s to the Walrus tank as a connector in this environment. For demonstration purposes, this example will use a K3s cluster.

4. Create an API key on Walrus for authentication to communicate between the Walrus CLI and the Walrus server in the following steps. Here are the guidelines:

  • Go to Account>User center>API Keys
  • Click New Keyname it and set an expiration
  • After configuration, copy the generated key and save it. If the key is lost in the future, it can be regenerated for a replacement.

new key

Configure the Walrus CLI and integrate it with GitLab CI

In this section we will show an example from CI to CD. Follow the steps below to integrate Walrus CLI with GitLab CI:

  1. Access GitLab and go to Admin Area > Settings > CI/CD > Variables
  2. Add the following variables and configure the sensitive data required to run GitLab CI:
  • CI_REGISTRY_USERNAME: Username of the CI build container image to send to Docker Hub; see the docker login.
  • CI_REGISTRY_PASSWORD: Password of CI build container image to send to Docker Hub; see the docker login.
  • CD_WALRUS_SERVER: URL accessed by Walrus, whose format is https://domain:port.
  • CD_WALRUS_TOKEN: Walrus API keys for authentication.

variables

3. Create .gitlab-ci.yml file in the GitLab project (exists by default in the sample project), which will define your CI/CD workflow. Below is a sample .gitlab-ci.yml sample project implementation file Game 2048. You can copy it and modify it as needed, such as changing the image sealdemo/game2048 in the image’s own name.

stages:
  - compile
  - build
  - deploy

variables:
  CI_PROJECT_DIR: ./
  CI_IMAGE_NAME: sealdemo/game2048
  CD_WALRUS_PROJECT: default
  CD_WALRUS_PROJECT_ENV: local

compile:
  stage: compile
  image: maven:3-openjdk-8
  artifacts:
    paths:
      - target/
  script:
    - mvn clean package

build:
  dependencies:
  - compile
  stage: build
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  artifacts:
    paths:
      - target/
  before_script:
    - mkdir -p /kaniko/.docker
    - echo "\"auths\":\"https://index.docker.io/v1/\": base64 " > /kaniko/.docker/config.json
  script:
    - /kaniko/executor
      --context "$CI_PROJECT_DIR"
      --dockerfile "$CI_PROJECT_DIR/Dockerfile"
      --destination "$CI_IMAGE_NAME:$CI_COMMIT_SHORT_SHA"

deploy:
  stage: deploy
  image: alpine
  before_script:
    - wget -O walrus --no-check-certificate "$CD_WALRUS_SERVER/cli?arch=amd64&os=linux"
    - chmod +x ./walrus
  script:
    - ./walrus login --insecure --server $CD_WALRUS_SERVER --token $CD_WALRUS_TOKEN
    - ./walrus apply -f ./walrus-file.yaml -p $CD_WALRUS_PROJECT -e $CD_WALRUS_PROJECT_ENV

4. Check out walrus-file.yamlwhich Walrus uses to deploy applications (it already exists by default in the sample project). Walrus file is summarized YAML a structure that describes the deployment configuration of the application. You can make any necessary changes to this file:

version: v1
resources:
- name: game2048
  type: containerservice
  attributes:
    containers:
    - profile: run
      image: $CI_IMAGE_NAME:$CI_COMMIT_SHORT_SHA
      ports:
      - schema: http
        external: 8080
        internal: 8080
        protocol: tcp
      resources:
        cpu: 0.25
        memory: 512

5. Access GitLab, go to Admin Area > CI/CD > Runnersmake sure GitLab Runner is properly online (how to install GitLab Runner), which is used to run the CI/CD pipeline defined by .gitlab-ci.yml:

racing

6. Go to 2048 Project > Build > Pipelineschoose Run pipeline

Pipelines

Wait for the pipeline to finish running and check the results of the pipeline operation:

See pipeline operation logs:

Review pipeline operation logs

The logs show that the pipeline was running successfully. GitLab CI completed CI/CD steps from Maven building, loading the container image and deploying the application to the K3s cluster using the Walrus CLI in that particular order.

7. After successfully running the pipeline, you can visit Walrus to see the deployed game2048 application!

distributed

Access the game 2048 using automatically recognized endpoints and /2048 path. Here is the full URL: http://domain:port/2048.

2048

We have now successfully integrated Walrus CLI with GitLab CI. By using Walrus file in Walrus 0.5.x, developers can now automate application deployment in a more user-friendly way when committing application code to GitLab.

With XaC (Everything is Code), Walrus can unify and manage the entire application lifecycle, from provisioning infrastructure resources to higher-level application release.

This guide only covers one scenario. If you are interested in more, you can explore other scenarios with Walrus, such as provisioning a Kubernetes cluster, creating RDS databases in the cloud, and configuring LB policies.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *