Semaphore 2 setup

Whenever you push your code, Semaphore will automatically run your build, test and deploy pipeline

Initialize semaphore in your project

Install the sem CLI and connect to your organization.

curl https://storage.googleapis.com/sem-cli-releases/get.sh | bash
sem connect ORGANIZATION.semaphoreci.com ACCESS_TOKEN

Then run sem init to create a semaphore.yml file.

Configure semaphore.yml

Blocks are used to define what to do at each step in the pipeline. For most of our projects, we have 3-4 blocks: setup, linters, tests and build.

All the commands needed to run your app should be in the first block. For example:

blocks:
  - name: Setup
    task:
      prologue:
        commands:
          - checkout // clones the github repo of the project to the job's VM environment.
          - cache restore node-modules-$SEMAPHORE_GIT_BRANCH-$(checksum mix.lock)
      jobs:
        - name: Install dependencies
          commands:
            - yarn install
      epilogue:
        commands:
          - cache delete node-modules-$SEMAPHORE_GIT_BRANCH-$(checksum mix.lock)
          - cache store node-modules-$SEMAPHORE_GIT_BRANCH-$(checksum mix.lock) node_modules

Put the main task for the block under jobs. Use prologue and epilogue for caching and other commands.

Caching dependencies

Since jobs each have their own VM environment, we need to cache dependencies to be able to reuse them and to prevent slowing down the pipeline.

Parallel jobs

While blocks run one after the other, jobs can run in parallel. This is useful for linter commands. For example:

- name: "Linter"
  task:
    prologue:
      commands:
        - checkout
        - cache restore node-modules-$(checksum yarn.lock)
    jobs:
    - name: Stylelint
      commands:
        - yarn stylelint
    - name: Eslint
      commands:
        - yarn eslint
    - name: Prettier.js
      commands:
        - yarn prettier
    - name: Flow
      commands:
        - yarn flow

Deploy with promotions

Promotions are used to move to a different pipeline. Add one under the blocks in your semaphore.yml file.

promotions:
  - name: Production deploy
    pipeline_file: production-deploy.yml

Deployment can be done manually or automatically. For example:

promotions:
  - name: Staging deploy // Auto
    pipeline_file: staging-deploy.yml
    auto_promote_on:
      - result: passed
      branch:
        - develop
  - name: Production deploy // Manual
    pipeline_file: production-deploy.yml