Skip to content

Pipeline YAML example

This guide illustrates an example pipeline code (.razorops.yaml) to cover various types of steps for those who want to jump right in. It contains two parallel tasks to build/test the code and a task to deploy it on a Kubernetes cluster.

To know more about the YAML spec, please refer here.

Tasks execution order defined below:
    | UNIT-TESTS |-------------
    --------------             \       |--------|
                                |------| DEPLOY |
    ----------                 /       |--------|
    | COMPILE |----------------
# spec version (v1/v2)
version: v2
global: # defines default properties for the `tasks`
    os_image: ubuntu # use Linux-VM build environment to run the tasks
  variables: # environment variables to be injected into each task
  - GOOS=linux
  - GOARCH=amd64
  - GOFLAGS=-mod=vendor
  - LD_FLAGS=-w -s

  unit-tests: # first task
    - checkout  # clone the git repo

    # It tries to pull cached content by calculating an unique key.
    - cache/pull: go-dep-{{ checksum "go.sum" }}

    # fetch code dependencies ( uses go modules )
    - name: Download dependencies
      run: go mod vendor

    - name: Execute test suite
      - GO_PACKAGES=$(go list ./... | grep -v vendor)
      ## run tests cases and generate reports
      - go test -coverprofile=coverage.out $GO_PACKAGES
      - mkdir coverage && go tool cover -html=coverage.out -o coverage/index.html

    # publish code dependencies with an unique key to speedup the next build 
    - cache/push:
        key: go-dep-{{ checksum "go.sum" }}
        paths: [~/go/pkg/mod]
    # upload coverage report to view it later in Dashboard
    - reports/html:
        name: cover
        dir: coverage/index.html

  compile: # second task (compile and unit-tests will start in parallel)
    # clone git repo
    - checkout

    # restore cache if available
    - cache/pull: go-dep-{{ checksum "go.sum" }}

    # download missing dependencies
    - run: go mod vendor

    # compile package
    - commands:
      - go build -ldflags "$LD_FLAGS" -o hack/dist/api ./cmd/server

    # persist files needed for deploy task
    - workspace/persist:
        paths: [hack,]

    when: branch == 'develop' # only run if code is pushed to develop branch
    depends: [unit-tests, compile] # wait for compile and unit-tests to finish
    # download the files persisted from upstream tasks (compile)
    - workspace/attach

    # build and push a Docker image. You will need to add the credentials in Dashboard
    - docker/build:
        tags: ["${CI_COMMIT_SHA:0:8}", "${CI_REPO_BRANCH}", "latest"]
        context: hack
        push: true

    # deploy on kubernetes cluster `example`, You need to add the credentials in Dashboard
    - name: Update image using kubectl 

      - kubectl config use-context example
      - kubectl set image deployment/api api=$DOCKER_IMAGE