CI/CD with Altostra and CircleCI

Integrating your Altostra projects with CircleCI
Yossi Ittach

October 29 2020 · 8 min read

SHARE ON

CI/CD with Altostra and CircleCI

CI/CD and Altostra

In our last post, we discussed how to integrate Altostra with GitHub Actions to create a simple yet effective CI/CD process. Cool as GitHub Actions is, it's still the new kid on the block, and our users have rightfully demanded the CircleCI edition. So with no further ado, let's learn how to add a simple CircleCI CI/CD flow to a basic Altostra API service project.

You can read more about the basic API project we'll use here, and more about CircleCI here.

Using Altostra API Key with CircleCI

Getting the Altostra API Key

First of all, we'll need to get an API key from Altostra.

Go to the Account Settings page in the Altostra Web Console

  1. Click "Create Token"
  2. Give your token a meaningful name so you'll recognize it later
  3. Keep your token somewhere safe - this is the last time you'll see this token!
create-api-key

Creating a CircleCI workflow

Log in to your CircleCI account with your GitHub account.

  1. Under the "Projects" tab, find your project and click "Set Up Project"
  2. You'll be prompted to add a configuration - add the configuration below.
  3. CircleCI will create a branch called circleci-project-setup for you in your repository with the appropriate config file.
version: 2
jobs:
  build:
    docker:
      - image: circleci/node:12.13

    working_directory: ~/repo

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run:
          name: Install Altostra CLI and dependencies
          command: |
            sudo npm i -g @altostra/cli
            alto api-key set $ALTO_API_KEY
            npm install

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-

      # run tests! - you can add this step to match your tests.
      # - run:
      #     name: Run tests with coverage
      #     command: npm test --coverage

      - run:
          name: Push image to repo
          command: alto push v$(node -p -e "require('./package.json').version") --json
setup-circle-ci

This CircleCI workflow basically tells CircleCI to run the job whenever someone pushes anything to any branch that contains the .circleci/config.yml file.

Each time it runs it will checkout the code, install dependencies, config the Altostra CLI, and use the Altostra API key to push the latest version to the Altostra repository.

After you've set the configuration file, you'll see that CircleCI has already started running the workflow for the new branch circleci-project-setup - but it will fail, because we still haven't added our API key to the workflow. Let's fix that.

Connecting the secret API Key

Now to what is usually the tricky part - how to safely use your API key without jeopardizing it.

Usually in CI/CD frameworks you'd have to store your secrets either on a secure server or in some kind of vault (like AWS Secrets Manager). CircleCI enables you to store your API key as an environment variable that can not be viewed - only updated or deleted.

Go to your CircleCI project, and:

  1. Click on "Project Settings"
  2. Select "Environment Variables" from the left side menu
  3. Click "Add Environment Variable"
  4. Name your environment variable ALTO_API_KEY
  5. Paste your secret into the input field.
add-api-key-circle-ci

Your secret is not visible to anyone - it can only be updated or deleted.

Let's try it out!

Now that we have the API Key safely stored and connected to the CircleCI workflow, all we need to do is push a commit to any branch that contains a .circleci/config.yml file. In our case, it's circleci-project-setup branch. (You can also just merge the circleci-project-setup branch into master and work on it instead)

run npm version major; git push on your local machine, and open the CircleCI console.

push-to-repo

Now you can go back to your terminal and list your images:

list-images

Turning the CI to CI/CD

The move from CI to CI/CD is not trivial - it usually requires some kind of canary testing, automated integration and sanity tests. If you already have these in place, deploying your code to Production (or any other env) is as simple as adding the following line to your CircleCI configuration after the push step:

      - run:
          name: Deploy to Production
          command: alto deploy production:v$(node -p -e "require('./package.json').version") --env Prod --json

Let's see it in action:

First, we'll add a hello endpoint to our API Gateway and a function to return Hello, CircleCI on GET request.

add-hello-endpoint

Now, let's make sure we add & push all the required changes to our circleci-project-setup branch

git-push-deploy

Then, let's make sure the CircleCI has triggered appropriately:

circle-ci-deploy-workflow

Once the deployment has been initiated, we can see it on the Altostra Web Console and check it's status on AWS CloudFormation stack:

alto-console-aws

Now that everything is up, we can test our new endpoint with a basic curl request:

curl --request GET '${YOUR_API_GATEWAY_ENDPOINT}/hello'

curl-request

Voila! We added a feature to our code, pushed to git, and had it deployed to our Production env on AWS automatically!

Wrapping up

Using the Altostra's API keys librates you to integrate your Altostra projects with any CI/CD framework you work with. What we've seen here is a simple example of utilizing CircleCI to create a basic CI/CD process, but it can also be integrated with many more testing and deployment tools - from Docker images to canary testing.

By submitting this form, you are accepting our Terms of Service and our Privacy Policy

Thanks for subscribing!

Ready to Get Started?

Request a Demo

Copyright © 2020 Altostra. All rights reserved.