Skip to content

Commit

Permalink
Continuous deployment to fly.io (#41)
Browse files Browse the repository at this point in the history
* Add script to register app name

* Set scale to 1

* Add pre-commit to check app name registered or not

* Add deploy job and use flyctl command

* Rename workflow file
  • Loading branch information
kumaranvpl authored Nov 18, 2024
1 parent 709a8fd commit a3852fd
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 7 deletions.
3 changes: 3 additions & 0 deletions {{cookiecutter.project_slug}}/.devcontainer/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pip install --upgrade pip
# install dev packages
pip install -e ".[dev]"

# install pre-commit hooks
pre-commit install

# install fly.io CLI and set fly.io CLI PATH in bashrc and zshrc
curl -L https://fly.io/install.sh | sh
echo 'export FLYCTL_INSTALL="/home/vscode/.fly"' | tee -a ~/.bashrc ~/.zshrc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% raw %}
name: Fly Deploy

on:
push:
branches:
- main
workflow_dispatch:

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: superfly/flyctl-actions/setup-flyctl@master

- name: Check if the app name is registered in fly.io
run: ./scripts/deploy_to_fly_io.sh

- name: Deploy to Fly
run: |
flyctl deploy --remote-only --config fly.toml --yes
flyctl scale count 1 --config fly.toml --yes
flyctl secrets set OPENAI_API_KEY=$OPENAI_API_KEY
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}{% endraw %}
9 changes: 9 additions & 0 deletions {{cookiecutter.project_slug}}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ repos:
hooks:
- id: detect-secrets
args: ["--baseline", ".secrets.baseline"]

- repo: local
hooks:
- id: check-registered-app
name: Check if the app name is registered in fly.io
entry: "scripts/check-registered-app-pre-commit.sh"
language: python
require_serial: true
verbose: true
6 changes: 3 additions & 3 deletions {{cookiecutter.project_slug}}/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,19 @@ This `FastAgency` project includes a `fly.toml` file for deployment to [fly.io](
1. Login into fly.io:
```bash
fly auth login
flyctl auth login
```
2. Launch the fly.io app:
```bash
fly launch --config fly.toml --copy-config --yes
flyctl launch --config fly.toml --copy-config --yes
```
3. Set necessary LLM API key(for example, OPENAI_API_KEY) as a secret:
```bash
fly secrets set OPENAI_API_KEY=paste_openai_api_key_here
flyctl secrets set OPENAI_API_KEY=paste_openai_api_key_here
```
## What's Next?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

# taken from: https://jaredkhan.com/blog/mypy-pre-commit

# A script for running mypy,
# with all its dependencies installed.

set -o errexit

# Change directory to the project root directory.
cd "$(dirname "$0")"/..

./scripts/check-registered-app.sh
8 changes: 8 additions & 0 deletions {{cookiecutter.project_slug}}/scripts/check-registered-app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# Check file registered_app_domain.txt exists. If it does not exists, echo and exit.
if [ ! -f registered_app_domain.txt ]; then
echo -e "\033[0;33mWarning: App name is not registered.\033[0m"
echo -e "\033[0;33mGithub Actions may fail if you push without registering.\033[0m"
echo -e "\033[0;33mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m"
fi
22 changes: 18 additions & 4 deletions {{cookiecutter.project_slug}}/scripts/deploy_to_fly_io.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
#!/bin/bash

echo -e "\033[0;32mLogging into fly.io\033[0m"
fly auth login
# Check file registered_app_domain.txt exists. If it does not exists, echo and exit.
if [ ! -f registered_app_domain.txt ]; then
echo -e "\033[0;31mError: App name is not registered.\033[0m"
echo -e "\033[0;31mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m"
echo -e "\033[0;31mExiting.\033[0m"
exit 1
fi

echo -e "\033[0;32mChecking if already logged into fly.io\033[0m"
if ! flyctl auth whoami > /dev/null 2>&1; then
echo -e "\033[0;32mLogging into fly.io\033[0m"
flyctl auth login
else
echo -e "\033[0;32mAlready logged into fly.io\033[0m"
fi

echo -e "\033[0;32mDeploying to fly.io\033[0m"
fly launch --config fly.toml --copy-config --yes
flyctl deploy --config fly.toml --yes
flyctl scale count 1 --config fly.toml --yes

echo -e "\033[0;32mSetting secrets\033[0m"
fly secrets set OPENAI_API_KEY=$OPENAI_API_KEY
flyctl secrets set OPENAI_API_KEY=$OPENAI_API_KEY
32 changes: 32 additions & 0 deletions {{cookiecutter.project_slug}}/scripts/register_to_fly_io.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash


# Check file registered_app_domain.txt exists. If it does, echo and exit.
if [ -f registered_app_domain.txt ]; then
echo -e "\033[1;33mWarning: App name is already registered.\033[0m"
echo -e "\033[0;32mRegistered app name is:\033[0m"
cat registered_app_domain.txt
exit 1
fi

echo -e "\033[0;32mChecking if already logged into fly.io\033[0m"
if ! flyctl auth whoami > /dev/null 2>&1; then
echo -e "\033[0;32mLogging into fly.io\033[0m"
flyctl auth login
else
echo -e "\033[0;32mAlready logged into fly.io\033[0m"
fi

export FLY_APP_NAME={{ cookiecutter.project_slug.replace("_", "-") }}

echo -e "\033[0;32mRegistering app name in fly.io\033[0m"
if flyctl apps create $FLY_APP_NAME; then
echo "$FLY_APP_NAME.fly.dev" > registered_app_domain.txt
echo -e "\033[0;32mApp name registered successfully\033[0m"
echo -e "\033[0;32mRegistered app name is:\033[0m"
cat registered_app_domain.txt
else
echo -e "\033[1;31mError: App name is not available.\033[0m"
echo -e "\033[1;31mPlease change the app name in fly.toml and scripts/register_to_fly_io.sh and run this script again.\033[0m"
exit 1
fi

0 comments on commit a3852fd

Please sign in to comment.