CircleCI

Architecture CI/CD

https://circleci.com/docs/2.0/about-circleci/#section=welcome

Créer son premier pipeline CI/CD

Pour commncer il faut se créer un compte sur le site CircleCI https://circleci.com/signup/. Pour faciliter l’authentification j’utilise l’authentification avec GitHub.

Par la suite je crée un projet sur GitHub https://github.com/.

Je crée un répertoire que j’appelle .circleci.

Dans ce répertoire je vais crée le fichier config.yml.

exemple config.yml

version: 2
jobs:
  build:
    docker:
      # 2019-10-23 BTH: had to fix the version as there is a breaking change in latest (=0.59)
      # See https://hub.docker.com/r/cibuilds/hugo, https://github.com/cibuilds/hugo
      - image: cibuilds/hugo:0.58.3

    steps:
      - checkout
      - run:
          name: Install git client
          command: apk update && apk add git
      - run:
          name: Load submodule
          command: git submodule sync && git submodule update --init
      - run:
          name: Build Hugo static website
          command: HUGO_ENV=production hugo
      - run:
          name: Install CF CLI
          command: |
            apk add wget
            wget https://cli.run.pivotal.io/stable?release=linux64-binary
            mv stable?release=linux64-binary /tmp/cf-cli.tgz
            mkdir -p /usr/local/bin
            tar -xzf /tmp/cf-cli.tgz -C /usr/local/bin
            cf --version
            rm -f /tmp/cf-cli.tgz
      - run:
          name: Deploy
          command: |
            cf login -a "$CF_API" -u "$CF_USERNAME" -p "$CF_PASSWORD" -o "$CF_ORG" -s "$CF_SPACE_PROD"
            cf push

Une fois mon job réalisé, je vais ajouter dans mon README.md un badge pour identifer l’état de mon pipeline.

exemple :

[![CircleCI](https://circleci.com/gh/ReachInfinity/base-de-connaissances.svg?style=svg&circle-token=dc4e57e148cb53d40551ff276685ed273b38797f)](https://circleci.com/gh/ReachInfinity/base-de-connaissances)

J’obteins directement cette ligne sur la plateforme CircleCI.

Si le projet est privé, il faut dans les paramètres du projet de CircleCI, se diriger dans API Permissions et créer un API token pour le projet. Ci-dessus le badge contient le token crée en amont.

Créer un pipeline avec un ordonnancement des tâches

exemple config.yml

version: 2.1

jobs:
  build:
    docker:
      # 2019-10-23 BTH: had to fix the version as there is a breaking change in latest (=0.59)
      # See https://hub.docker.com/r/cibuilds/hugo, https://github.com/cibuilds/hugo
      - image: cibuilds/hugo:0.58.3
    steps:
      - checkout
      - run:
          name: Install git client
          command: apk update && apk add git
      - run:
          name: Load submodule
          command: git submodule sync && git submodule update --init
      - run:
          name: Build Hugo static website
          command: HUGO_ENV=production hugo
      - run:
          name: Test generated HTML files
          command: |
            htmlproofer public/ --allow-hash-href --check-html \
            --empty-alt-ignore --disable-external
      - run:
          name: Install CF CLI
          command: |
            apk add wget
            wget https://cli.run.pivotal.io/stable?release=linux64-binary
            mv stable?release=linux64-binary /tmp/cf-cli.tgz
            mkdir -p /usr/local/bin
            tar -xzf /tmp/cf-cli.tgz -C /usr/local/bin
            cf --version
            rm -f /tmp/cf-cli.tgz
      - run:
          name: Deploy
          command: |
            cf login -a "$CF_API" -u "$CF_USERNAME" -p "$CF_PASSWORD" -o "$CF_ORG" -s "$CF_SPACE_PROD"
            cf push
  scan:
     docker:
       - image: sonarsource/sonarcloud-scan:1.0.1
     steps:
     - checkout
     - run:
         name: Quality Scan
         command: sonar-scanner -Dsonar.host.url=https://sonarcloud.io -Dsonar.login="$SONAR_LOGIN"
  test:
     machine: true
     steps:
     - checkout
     - run:
        name: Running k6 tests
        command: |
          docker pull loadimpact/k6:latest 
          docker run -i -e K6_CLOUD_TOKEN=$K6_CLOUD_TOKEN -v $PWD:/ci/ loadimpact/k6:latest cloud /ci/loadtests/performance-test.js
        
  testUI:
      docker:
        - image: circleci/node:13.10.1-browsers
      steps:
        - checkout
        - run:
            name: Browser Testing
            command: |
              npm i selenium-webdriver
              node browser-testing/tech-lunch.js

 
workflows:
  version: 2.1
  build_deploy_tests_and_scan:
    jobs:
      - build
      - scan:
          requires:
            - build
      - test:
          requires:
            - build
      - testUI:
          requires:
            - build

Créer un pipeline nocturne

exemple config.yml

workflows:
  version: 2.1
  build_deploy_tests_and_scan:
    jobs:
      - build
      - scan:
          requires:
            - build
      - test:
          requires:
            - build
      - testUI:
          requires:
            - build
 # Scheduled workflows may be delayed by up to 15 minutes.
 # This is done to maintain reliability during busy times such as 12:00am UTC. 
 # Scheduled workflows should not assume they are started with to-the-minute accuracy.
  nightly:
    triggers:
      - schedule:
          cron: "0 0 * * *"
          filters:
            branches:
              only:
                - master
    jobs:
      - build
      - scan

Liens utile

https://circleci.com/
https://circleci.com/docs/