How to deploy to Aptible with CI/CD
Overview
To make it easier to deploy on Aptible—whether you’re migrating from another platform or deploying your first application—we offer integrations with several continuous integration services.
If your team is already using a Git-based deployment workflow, deploying your app to Aptible should be relatively straightforward.
Deploying with Git
Prerequisites
To deploy to Aptible via Git, you must have a public SSH key associated with your account. We recommend creating a robot user to manage your deployment:
-
Create a
Robots
custom Aptible role in your Aptible organization. Grant it “Read” and “Manage” permissions for the environment where you would like to deploy. -
Invite a new robot user with a valid email address (for example,
deploy@yourdomain.com
) to theRobots
role. -
Sign out of your Aptible account, accept the invitation from the robot user’s email address, and set a password for the robot’s Aptible account.
-
Generate a new SSH key pair to be used by the robot user, and don’t set a password:
ssh-keygen -t ed25519 -C "your_email@example.com"
-
Register the SSH Public Key with Aptible for the robot user.
Configuring SSH
To deploy to Aptible via GitHub Actions, you must first create an encrypted secret for your repository with the following values:
- Name:
APTIBLE_GIT_SSH_KEY
- Value: The contents of the SSH Private Key created in the previous step.
Configuring the Environment
You also need to set environment variables on your repository with the name of your Aptible environment and app, in APTIBLE_ENVIRONMENT
and APTIBLE_APP
, respectively. You can configure these the same way that the SSH key was added to the workflow, via workflow secrets on the GitHub Workflow.
Configuring the Workflow
Finally, you must configure the workflow to deploy your application to Aptible:
on:
push:
branches: [ main ]
env:
APTIBLE_ENVIRONMENT: ${{ secrets.APTIBLE_ENVIRONMENT }}
APTIBLE_APP: ${{ secrets.APTIBLE_APP }}
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: webfactory/ssh-agent@v0.5.4
with:
ssh-private-key: ${{ secrets.APTIBLE_GIT_SSH_KEY }}
- name: Deploy to Aptible
run: |
ssh-keyscan beta.aptible.com >> ~/.ssh/known_hosts
git remote add aptible git@beta.aptible.com:${APTIBLE_ENVIRONMENT}/${APTIBLE_APP}.git
git push aptible ${GITHUB_SHA}:master
Let’s break down how this works. We begin by defining when the workflow should run (when a push is made to the main
branch), and the environment variables we need (our Aptible environment and app names):
on:
push:
branches: [ main ]
env:
APTIBLE_ENVIRONMENT: ${{ secrets.APTIBLE_ENVIRONMENT }}
APTIBLE_APP: ${{ secrets.APTIBLE_APP }}
Next, we run a pair of common steps that check out a shallow clone of our repository and configure the environment’s SSH agent using the private key we provided.
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: webfactory/ssh-agent@v0.5.4
with:
ssh-private-key: ${{ secrets.APTIBLE_GIT_SSH_KEY }}
Finally, we add our SSH private key to the workflow environment, configure a new remote for our repository on Aptible’s platform, and push our branch to Aptible:
steps:
. . .
- name: Deploy to Aptible
run: |
ssh-keyscan beta.aptible.com >> ~/.ssh/known_hosts
git remote add aptible git@beta.aptible.com:${APTIBLE_ENVIRONMENT}/${APTIBLE_APP}.git
git push aptible ${GITHUB_SHA}:master
From there, the procedure for a Dockerfile-based deployment remains the same!
Deploying with Docker
Prerequisites
To deploy to Aptible with a Docker image via a CI integration, you should create a robot user to manage your deployment:
-
Create a
Robots
custom Aptible role in your Aptible organization. Grant it “Read” and “Manage” permissions for the environment where you would like to deploy. -
Invite a new robot user with a valid email address (for example,
deploy@yourdomain.com
) to theRobots
role. -
Sign out of your Aptible account, accept the invitation from the robot user’s email address, and set a password for the robot’s Aptible account.
Some of the below instructions and more information can also be found on the Github Marketplace page for the Deploy to Aptible Action.
Configuring the Environment
To deploy to Aptible via GitHub Actions, you must first create encrypted secrets for your repository with Docker registry and Aptible credentials:
DOCKERHUB_USERNAME
and DOCKERHUB_TOKEN
The credentials for your private Docker registry (in this case, DockerHub).
APTIBLE_USERNAME
and APTIBLE_PASSWORD
The credentials for the robot account created to deploy to Aptible.
Configuring the Workflow
Additionally, you will need to set some environment variables within the GitHub Actions workflow:
IMAGE_NAME
The Docker image you wish to deploy from your Docker registry.
APTIBLE_ENVIRONMENT
The name of the Aptible environment acting as the target for this deployment.
APTIBLE_APP
The name of the app within the Aptible environment we are deploying with this workflow.
Configuring the Workflow
Finally, you must configure the workflow to deploy your application to Aptible:
on:
push:
branches: [ main ]
env:
IMAGE_NAME: user/app:latest
APTIBLE_ENVIRONMENT: "my_environment"
APTIBLE_APP: "my_app"
jobs:
deploy:
runs-on: ubuntu-latest
# Allow multi platform builds.
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
# Allow use of secrets and other advanced docker features.
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
# Log into Docker Hub
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# Build image using default dockerfile.
- name: Build and push
uses: docker/build-push-action@v3
with:
push: true
tags: ${{ env.IMAGE_NAME }}
- name: Deploy to Aptible
uses: aptible/aptible-deploy-action@v1
with:
username: ${{ secrets.APTIBLE_USERNAME }}
password: ${{ secrets.APTIBLE_PASSWORD }}
environment: ${{ env.APTIBLE_ENVIRONMENT }}
app: ${{ env.APTIBLE_APP }}
docker_img: ${{ env.IMAGE_NAME }}
private_registry_username: ${{ secrets.DOCKERHUB_USERNAME }}
private_registry_password: ${{ secrets.DOCKERHUB_TOKEN }}
From there, you can review our resources for Direct Docker Image Deployments!