Deployment Automation with GitLab CI and Nitric

This guide will demonstrate how Nitric can be used, along with GitLab CI, to create a continuous deployment pipeline. We provide examples for deploying to AWS and Microsoft Azure, which you can adapt based on your preferred cloud provider.

This guide assumes basic knowledge about GitLab CI. If you're new to the feature you could start by reviewing GitLab's docs

Configuration

  1. Prepare Your Nitric Project
    Ensure you have a Nitric project ready to deploy. If you haven’t set up a project yet, refer to our quickstart guide.

  2. Add a GitLab CI/CD File
    Create a yaml file .gitlab-ci.yml at the root of your project. The file can be named how you like, however .gitlab-ci.yml is most common.

Here’s example content for each cloud provider:

deploy:
image: docker:27
# Enable Docker-in-Docker (DinD) to allow running Docker commands within the CI environment
services:
- docker:27-dind
# Define rules for when this job should run
rules:
# Run this job only if the pipeline is triggered by a merge request event
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
# Run this job if the commit branch matches the default branch
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
# Set environment variables for Pulumi and AWS credentials
variables:
PULUMI_CONFIG_PASSPHRASE: $PULUMI_ACCESS_TOKEN
PULUMI_ACCESS_TOKEN: $PULUMI_ACCESS_TOKEN
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
before_script:
# Update package list and install required packages
- apk update && apk add --no-cache curl bash
# Retrieve the IP address of the Docker host, necessary for Docker-in-Docker communication
- export NITRIC_DOCKER_HOST=$(ip -4 addr show eth0 | grep -o 'inet [0-9\.]*' | awk '{print $2}')
# Install Pulumi by downloading and executing the installation script
# Pulumi is a tool for managing infrastructure as code
- curl -fsSL https://get.pulumi.com | sh
- export PATH=$PATH:$HOME/.pulumi/bin
# Install Nitric by downloading and executing the installation script
- curl -L https://nitric.io/install?version=latest | bash
- export PATH=$PATH:$HOME/.nitric/bin
script:
# Execute the Nitric command to deploy infrastructure
# --ci flag is used for continuous integration environments
- nitric up --ci
Last updated on Oct 30, 2024