Zeit Now Continuous Deployment
This guide shows you how to use Semaphore to set up continuous integration and deployment to ZEIT Now.
Semaphore provides a demo project:
The demo deploys a serverless function that replies “Hello World!” to all HTTP requests.
Overview of the pipelines
The pipeline performs the following tasks:
- Install dependencies.
- Run unit tests.
- Continuously deploy the master branch to the production site.
On manual approval:
- Deploy to the staging site.
The complete CI/CD workflow looks like this:
Continuous Integration Pipeline (CI)
In the repository, the
.semaphore directory contains the annotated pipeline configuration files.
The CI pipeline takes place in 2 blocks:
- npm install and cache:
- Downloads and installs the Node.js packages.
- Builds the app and saves it to the cache.
- npm test:
- Runs unit and coverage tests.
version: v1.0 name: Build and test # An agent defines the environment in which your code runs. # It is a combination of one of the available machine types and operating # system images. # See https://docs.semaphoreci.com/article/20-machine-types # and https://docs.semaphoreci.com/article/32-ubuntu-1804-image agent: machine: type: e1-standard-2 os_image: ubuntu1804 # Blocks are the heart of a pipeline and are executed sequentially. # Each block has a task that defines one or more jobs. Jobs define the # commands to execute. # See https://docs.semaphoreci.com/article/62-concepts blocks: - name: Install dependencies task: jobs: - name: npm install and cache commands: - checkout - nvm use - node --version - npm --version - cache restore - npm install - cache store - name: Run tests task: jobs: - name: npm test commands: - checkout - nvm use - cache restore - npm test promotions: # Deployment to staging can be triggered manually: - name: Deploy to staging pipeline_file: deploy-staging.yml # Automatically deploy to production on successful builds on the master branch: - name: Deploy to production pipeline_file: deploy-production.yml auto_promote_on: - result: passed branch: - master
Two promotions branch out of the CI pipeline:
- Deploy to production: automatically started once all tests are green for the master branch.
- Deploy to staging: can be manually initiated from a Semaphore workflow on any branch.
Continuous Deployment Pipeline (CD)
The CD pipeline consists of a block with a single job:
version: v1.0 name: Deploy to production agent: machine: type: e1-standard-2 os_image: ubuntu1804 blocks: - name: Deploy to production task: secrets: - name: now jobs: - name: Deploy to ZEIT Now commands: - checkout - nvm use - npm install now -g - now --token $ZEIT_TOKEN -n semaphore-demo-zeit-now
ZEIT Now is a cloud service for web services and serverless functions. Deployment is performed with the Now CLI. No configuration file is required as long as the project files are located in these special directories:
publicfor static files.
pages/apifor serverless functions.
In addition, ZEIT Now can automatically build many popular frameworks.
Both staging and production pipelines are almost identical. They only differ on the app name, which maps to the final deployment URL like this:
Run The Demo Yourself
You can get started right away with Semaphore. Running and deploying the demo by yourself takes only a few minutes:
Get a Token
- Create a ZEIT Now account.
- Open your account Settings
- Go to the Tokens tab.
- Click on the Create button.
- Enter a name for the token, something descriptive like: semaphore-zeit-now
- Copy the generated token and keep it safe.
Create the pipeline on Semaphore
Now, add the token to Semaphore:
- Create an account for Semaphore.
- On the left navigation bar, under Configuration click on Secrets.
- Hit the Create New Secret button.
- Create the secret, as shown below. Copy the token obtained earlier.
To run the project on Semaphore:
- Fork the Demo project on GitHub.
- Clone the repository on your local machine.
- In Semaphore, follow the link on the sidebar to create a new project.
- Edit any file and do a push to GitHub, Semaphore starts automatically.
Once the deployment is complete, the API service should be online. Browse the production URL:
$ curl -w "\n" https://semaphore-demo-zeit-now.YOUR_USERNAME.now.sh/api/hello Hello World!