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`
  runner:
    os_image: ubuntu # use Linux-VM build environment to run the tasks
  variables: # environment variables to be injected into each task
  - CGO_ENABLED=0
  - GOOS=linux
  - GOARCH=amd64
  - GOFLAGS=-mod=vendor
  - LD_FLAGS=-w -s

tasks:
  unit-tests: # first task
    steps:
    - 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
      commands:
      - 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)
    steps:
    # 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, Dockerfile.ci]

  deploy:
    when: branch == 'develop' # only run if code is pushed to develop branch
    depends: [unit-tests, compile] # wait for compile and unit-tests to finish
    steps:
    # 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:
        image: us.gcr.io/demo/api
        tags: ["${CI_COMMIT_SHA:0:8}", "${CI_REPO_BRANCH}", "latest"]
        dockerfile: Dockerfile.ci
        context: hack
        push: true

    # deploy on kubernetes cluster `example`, You need to add the credentials in Dashboard
    - name: Update image using kubectl 
      commands:
      - DOCKER_IMAGE=us.gcr.io/demo/api:${CI_COMMIT_SHA:0:8}

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