4.2.1 Continuous Deployment (Fly.io)

Learning Objectives

  1. Continuous Deployment (CD) is a software engineering approach in which our application code can be automatically released, and deployed onto our running environment whenever pushed onto main branch in our Github repositories

  2. Understand how to setup Github actions to set up a pipeline to connect our repository directly with our deployed fly.io environment

  3. How to ensure we protect our .env variables even when using github to automatically connect to our fly.io environment

Introduction

Continuous Deployment, often times linked closely with Continuous Integration (CI), automatically deploys all code changes that are pushed into a specific branch in your repository, designated by you or your team, through a pre-set up pipeline.

Just like CI, CD is utilised by almost every tech company to automatically deploy code to their testing, development or even production environments. However, CD is almost never done on its own with out having CI taking place.

Just like we did in 4.2 Continuous Integration, we will be utilising GitHub Actions to demonstrate how this works.

Pre-requisites

  1. Deployed Backend

Before setting up CD, the Backend would need to have already been deployed as laid out here. if not, it is recommended to have the deployment completed before carrying on.

  1. Setting up proper env variables.

As the fly.toml would now need to be pushed into github, we would need to set up our .env variables somewhere else as we would like to prevent our keys to the Backend to be uploaded into github. To do this, first we would need to open up our terminal to the root directory of our deployed backend project.

For each of the environmental variables, the following code would need to write:

fly secrets set <ENV NAME>=<env value>

Each time the code is run, the instance may restart and take a bit of time before you can enter the next variables

To see all the environmental variables on the instance, run:

fly ssh console -C "printenv"

Now that this is set up, it is important to remember to delete the [env] variables from the fly.toml file.

Setting up Continuous Deployment

To ensure that CD can be set up remotely (via GitHub), a token would need to be generated first. To do this, run open up your terminal to the root directory of your deployed project and run the following command:

fly tokens create deploy -x 999999h

Copy the generated token, and open up the GitHub repository and open up the setting tab.

In the setting tab, look for Secrets and variables and select Actions under it.

Create a new token and name it FLY_API_TOKEN and save the token generated under this secret.

Back in VScode, create a new file under .github/workflows/ called fly.yml

Copy and paste the following into the file

name: Fly Deploy
on:
  push:
    branches: [main]
     
jobs:
  deploy:
    name: Deploy app
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: superfly/flyctl-actions/setup-flyctl@master
      - run: flyctl deploy --remote-only
        env:
          FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

Commit changes, and check out the actions tab to see if the app is being deployed onto Fly.io!

Last updated