@trussle/ci

Continuous Integration scripts

Usage no npm install needed!

<script type="module">
  import trussleCi from 'https://cdn.skypack.dev/@trussle/ci';
</script>

README

Trussle CI

This package provides a suite of scripts used by Trussle's Continuous Integration system to build Docker images for testing and deployment.

Usage

Installation

Install this package as a devDependency:

npm install --save-dev @trussle/ci

Dockerfile

Next, set up your Dockerfile with the following constraints:

  • There should be a builder stage, which creates an image ready to build and test in the /builder directory. The ENTRYPOINT should run all unit tests (with coverage) and provide results in JUnit format to the directory /builder/test-results.
  • The final stage of the Dockerfile should create a runner image continaing only the files required to run the application. The ENTRYPOINT should run the application.
  • The host machine's NPM token will be injected into the image as the argument NPM_TOKEN, so you'll likely need the following lines at the top of your Dockerfile:
# Embed the NPM_TOKEN (passed in from the host machine) into the image.
ARG NPM_TOKEN
RUN echo -n ${NPM_TOKEN} > /root/.npmrc

Samples of Dockerfiles can be found in the templates.

Docker Compose

If you have integration tests (tests that require an external dependency to be running), you should include a Docker Compose file that looks like this:

version: "2.1"

services:
  integration:
    image: "878732450721.dkr.ecr.eu-west-1.amazonaws.com/[package-name]-builder:latest"
    # depends_on:
    #   - service1
    #   - service2
    entrypoint: npm run test:integration
    environment:
      - NODE_ENV=CI
  # Your other services go here!

Running

You can now run:

  • npx t-ci builder to make the build/test image.
  • npx t-ci test-unit runs unit tests in the builder.
  • npx t-ci test-integration sets up the Docker Compose environment and runs the integration tests.
  • npx t-ci runner to make the runner image.

These commands can be used to make your Jenkinsfile super short:

pipeline {
  agent any
  stages {
    stage("Setup") { steps { sh "npx t-ci builder" } }
    stage("Tests") {
      parallel {
        stage("Unit Tests") { steps { sh "npx t-ci test-unit" } }
        stage("Integration Tests") { steps { sh "npx t-ci test-integration" } }
      }
    }
    stage("Build") { steps { sh "npx t-ci runner" } }
  }
  post {
    always {
      junit "test-results/**/*.xml"
      sh "aws s3 sync test-results/ s3://qa-junit-test-reports/${env.JOB_NAME}/${env.BUILD_NUMBER}/test-results"
      cleanWs(
        cleanWhenAborted: true,
        cleanWhenFailure: true,
        cleanWhenNotBuilt: true,
        cleanWhenSuccess: true,
        cleanWhenUnstable: true,
        cleanupMatrixParent: true,
        deleteDirs: true
      )
    }
  }
}