Zeit Now continuous deployment#
ZEIT Now is a cloud service for web services and serverless functions. This guide shows you how to use Semaphore to set up continuous integration and deployment to ZEIT Now.
Semaphore provides a demo project that you can use to get started.
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 deploys the master branch to the production site.
On manual approval:
- Deploys 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
The CI pipeline takes place in 2 blocks:
- npm install and cache:
- Downloads and installs 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/ci-cd-environment/machine-types/ # and https://docs.semaphoreci.com/ci-cd-environment/ubuntu-18.04-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/essentials/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: when: "result = 'passed' and 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
Deployment with ZEIT Now is performed with the Now CLI. No configuration file is required as long as the project files are located in the following special directories:
publicfor static files.
pages/apifor serverless functions.
In addition, ZEIT Now can automatically build using many popular frameworks.
The staging and production pipelines are almost identical. They only differ in the app's name, which maps to the final deployment URL as shown below:
Running the demo yourself#
You can get started right away with Semaphore. Running and deploying the demo by yourself takes only a few minutes:
Getting 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. It should be something descriptive like: semaphore-zeit-now
- Copy the generated token and keep it safe.
Creating the pipeline on Semaphore#
Now, add the token to Semaphore:
- Create a Semaphore account.
- In the left navigation bar, click on Secrets under Configuration.
- Hit the Create New Secret button.
- Create a secret, as shown below. Copy the token obtained earlier to it.
To run the project on Semaphore:
- Fork the Demo project on GitHub.
- Clone the repository to 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. You can browse the production URL with the following command:
$ curl -w "\n" https://semaphore-demo-zeit-now.YOUR_USERNAME.now.sh/api/hello Hello World!