CI/CD with Altostra and GitHub Actions

Integrating your Altostra projects with GitHub Actions
Yossi Ittach

October 26 2020 · 8 min read

SHARE ON

CI/CD with Altostra and GitHub Actions

CI/CD and Altostra

Altostra CLI is built for CI/CD - it uploads your images to your repository and deploys them to any environments you need. However, one of the topmost requests we've been getting from our users so far is a way to integrate Altostra with their existing CI/CD infrastructure. Their main culprit was the need to manually authenticate with Altostra's services.

With the API Keys feature, you can use an API key that frees you from the need to periodically authenticate with Altostra's services - just add your API key to your CI/CD workflow, and you're free to automatically deploy, push, sync, and invalidate while the API key handles the authentication.

In this post we'll learn how to add a simple GitHub actions CI/CD flow to a basic Altostra API service project.

You can read more about the basic API project here, and read more about GitHub Actions here.

Using Altostra API Key with GitHub Actions

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 GitHub Action workflow

In your project, at the project root, create a new file called altostra-ci.yaml, under .github/workflows/altostra-ci.yaml

Copy-paste the following code to the file:

name: Node.js CI

on:
  push:
    branches:
    - main

jobs:
  build:
    name: Alto Push
    runs-on: ubuntu-latest

    steps:
      - name: Set node version
        uses: actions/setup-node@v1
        with:
          node-version: '12.x'
      - name: Install Altostra CLI
        run: |
             npm install -g @altostra/cli
             alto --version
             alto api-key set ${{ secrets.ALTO_API_KEY }}
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Install dependencies
        run: npm install
      - name: Push to Altostra
        run: alto push v$(node -p -e "require('./package.json').version") --json

This GitHub Action workflow basically tells GitHub to run the "Alto Push" job whenever someone pushes to the main branch.

Each time 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.

Handling 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). However, GitHub has its own secrets service that allows you to store secrets per organization/repository (you can read more about it here).

Go to your GitHub repository, and:

  1. Click on "Settings"
  2. Select "Secrets" from the left side menu
  3. Click "New Secret"
  4. Name your secret ALTO_API_KEY
  5. Paste your secret into the input field.
add-api-key-to-github

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 GitHub workflow code, all we need to do is push a commit to our main.

On your project repo on GitHub, go to the "Action" Tab - you should see your first Altostra powered CI process running!

watch-action

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 GitHub Actions configuration after the push step:

      - name: Deploy to Prod
        run: 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, GitHub Actions on GET request, and push it to main.

add-hello-endpoint

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

github-action

Once the deployment has been initiated, we can see it on the Altostra web console:

alto-console-aws

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

curl-result

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 new 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 GitHub Actions 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.