Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/terraform heroku #153

Merged
merged 12 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/actions/build-push-image/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Build and push Docker image to Heroku
description: Build Docker image for server, swaps tracker and maker mock and push to Heroku Container Registry

inputs:
heroku_api_key:
description: "Heroku API key"
required: true
heroku_app_name:
description: "Heroku app name"
required: true
heroku_app_type:
description: "Heroku app type"
required: true

runs:
using: "composite"
steps:
- name: Login to Heroku Container Registry
shell: bash
run: heroku container:login
env:
HEROKU_API_KEY: ${{ inputs.heroku_api_key }}

- name: Build Docker Image for Server
shell: bash
run: |
docker build --build-arg APP_PATH=./cmd/order-book -t registry.heroku.com/$HEROKU_APP_NAME/$HEROKU_APP_TYPE -f cmd/order-book.Dockerfile .
env:
HEROKU_APP_NAME: ${{ inputs.heroku_app_name }}
HEROKU_APP_TYPE: ${{ inputs.heroku_app_type }}

- name: Push Docker Image for Server to Registry
shell: bash
run: docker push registry.heroku.com/$HEROKU_APP_NAME/$HEROKU_APP_TYPE
env:
HEROKU_API_KEY: ${{ inputs.heroku_api_key }}
HEROKU_APP_NAME: ${{ inputs.heroku_app_name }}
HEROKU_APP_TYPE: ${{ inputs.heroku_app_type }}
109 changes: 109 additions & 0 deletions .github/actions/terraform/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
name: AWS Terraform
description: Runs Terraform against AWS
inputs:
terraform_state_s3_bucket:
description: "S3 bucket for Terraform state"
required: true
terraform_state_s3_key_prefix:
description: "S3 key prefix for Terraform state"
required: true
terraform_state_dynamodb_table:
description: "DynamoDB table for Terraform state"
required: true
build_path:
description: "Build path that contains the source"
required: true
environment:
description: "Name of the environemnt e.g. dev, staging, prod"
required: true
branch:
description: "Git branch being ran against"
required: true
image_tag:
description: "Docker image tag to deploy"
required: false

runs:
using: "composite"
steps:
- name: Check permissions
id: permissions
run: |
chmod 777 *.sh
chmod +x *.sh
echo -e "\n"
shell: bash
working-directory: "${{ github.action_path }}"

- name: Setup build environment
id: setup
run: |
${{ github.action_path }}/box.sh "Setting up build environment"
echo -e "Installing TFLint"
curl -L "$(curl -Ls https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" -o tflint.zip && unzip tflint.zip && rm tflint.zip
curl -L "$(curl -Ls https://api.github.com/repos/terraform-linters/tflint-ruleset-aws/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" -o tflint-ruleset.zip && unzip tflint-ruleset.zip && rm tflint-ruleset.zip
mkdir -p ./.tflint.d/plugins/
mv tflint-ruleset-aws ./.tflint.d/plugins/
echo -e "\n"
shell: bash
working-directory: "${{ inputs.build_path }}/infrastructure"

- name: Terraform Init
id: init
run: |
${{ github.action_path }}/box.sh "Running Terraform init"
terraform init -backend-config="region=eu-west-1" -backend-config="dynamodb_table=${{ inputs.terraform_state_dynamodb_table }}" -backend-config="encrypt=true" -backend-config="workspace_key_prefix=${{ inputs.terraform_state_s3_key_prefix }}" -backend-config="bucket=${{ inputs.terraform_state_s3_bucket }}" -backend-config="key=${{ inputs.terraform_state_s3_key_prefix }}/${{ inputs.environment }}/terraform.tfstate"
echo -e "Terraform S3 bucket: ${{ inputs.terraform_state_s3_bucket }}"
echo -e "Terraform state file: ${{ inputs.terraform_state_s3_key_prefix }}/${{ inputs.environment }}/terraform.tfstate"
echo -e "\n"
shell: bash
working-directory: "${{ inputs.build_path }}/infrastructure"

- name: Terraform Validate
id: validate
run: |
${{ github.action_path }}/box.sh "Running Terraform validate"
terraform validate
echo -e "\n"
shell: bash
working-directory: "${{ inputs.build_path }}/infrastructure"

- name: TFLint
id: lint
run: |
${{ github.action_path }}/box.sh "Running TFLint"
./tflint --init
export TFLINT_LOG=info
./tflint --var-file='./environments/${{ inputs.environment }}/${{ inputs.environment }}.tfvars' --module --config=./.tflint.hcl || true
# echo -e "\n"
shell: bash
working-directory: "${{ inputs.build_path }}/infrastructure"

- name: Terraform Plan
id: plan
run: |
export TF_VAR_environment=${environment}
${{ github.action_path }}/box.sh "Running Terraform plan"
plan_command="terraform plan -var-file='./environments/${{ inputs.environment }}/${{ inputs.environment }}.tfvars' -var 'region=${{ inputs.aws_deploy_region }}' -var 'image_tag=${{ inputs.image_tag }}' -input=false -out=plan.out"
if [ "${{ inputs.branch }}" == "main" ]; then
eval $plan_command
else
plan_command+=" -lock=false"
eval $plan_command
fi
echo -e "\n"
shell: bash
working-directory: "${{ inputs.build_path }}/infrastructure"

- name: Terraform Apply
id: apply
run: |
if [ "${{ inputs.branch }}" == "main" ]; then
${{ github.action_path }}/box.sh "Running Terraform apply"
terraform apply -auto-approve -input=false plan.out
else
echo -e "Not on dev, staging or main branch, so skipping Terraform apply."
fi
echo -e "Terraform run completed successfully."
shell: bash
working-directory: "${{ inputs.build_path }}/infrastructure"
4 changes: 4 additions & 0 deletions .github/actions/terraform/box.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
echo $1 | sed -e 's/^/../' -e 's/$/../' -e 's/./*/g'
echo $1 | sed -e 's/^/* /' -e 's/$/ */'
echo $1 | sed -e 's/^/../' -e 's/$/../' -e 's/./*/g'
142 changes: 142 additions & 0 deletions .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
name: Build and deploy
env:
terraform_state_s3_bucket: "orbs-terraform-tfstate"
terraform_state_s3_key_prefix: "order-book"
terraform_state_dynamodb_table: "orbs-terraform-locks"
build_path: "."
on:
push:
branches: ["main"]
paths:
- "**"
jobs:
build-server:
runs-on: ubuntu-latest
steps:
- name: Check Out Repo
uses: actions/checkout@v3

- name: Build server image and push to Heroku
uses: ./.github/actions/build-push-image
with:
heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
heroku_app_name: "ob-server-development"
heroku_app_type: "web"

build-swaps-tracker:
runs-on: ubuntu-latest
steps:
- name: Check Out Repo
uses: actions/checkout@v3

- name: Build swaps tracker image and push to Heroku
uses: ./.github/actions/build-push-image
with:
heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
heroku_app_name: "ob-swaps-tracker-development"
heroku_app_type: "worker"

build-maker-mock:
runs-on: ubuntu-latest
needs: [build-server, build-swaps-tracker]
steps:
- name: Check Out Repo
uses: actions/checkout@v3

- name: Login to Heroku Container Registry
run: heroku container:login
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}

- name: Build and Push Docker Image
run: |
docker build -t registry.heroku.com/ob-maker-mock/worker -f e2e/maker/Dockerfile .
docker push registry.heroku.com/ob-maker-mock/worker
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}

- name: Release Image
run: heroku container:release worker --app ob-maker-mock
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}

deploy-development:
runs-on: ubuntu-latest
needs: [build-server, build-swaps-tracker, build-maker-mock]
environment: development

steps:
- name: Login to Heroku Container Registry
run: heroku container:login
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}

- name: Pull and Release Image for Server Nonprod
run: |
heroku container:release web --app ob-server-development
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}

- name: Pull and Release Image for Swaps Tracker Nonprod
run: |
heroku container:release worker --app ob-swaps-tracker-development
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}

deploy-staging:
runs-on: ubuntu-latest
needs: deploy-development
environment: staging

steps:
- name: Login to Heroku Container Registry
run: heroku container:login
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}

- name: Pull and Release Image for Server Staging
run: |
docker pull registry.heroku.com/ob-server-development/web
docker tag registry.heroku.com/ob-server-development/web registry.heroku.com/ob-server-staging/web
docker push registry.heroku.com/ob-server-staging/web
heroku container:release web --app ob-server-staging
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}

- name: Pull and Release Image for Swaps Tracker Staging
run: |
docker pull registry.heroku.com/ob-swaps-tracker-development/worker
docker tag registry.heroku.com/ob-swaps-tracker-development/worker registry.heroku.com/ob-swaps-tracker-staging/worker
docker push registry.heroku.com/ob-swaps-tracker-staging/worker
heroku container:release worker --app ob-swaps-tracker-staging
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}

deploy-production:
runs-on: ubuntu-latest
needs: [deploy-development, deploy-staging]
environment: production

steps:
- name: Login to Heroku Container Registry
run: heroku container:login
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}

- name: Pull and Release Image for Server Production
run: |
docker pull registry.heroku.com/ob-server-staging/web
docker tag registry.heroku.com/ob-server-staging/web registry.heroku.com/ob-server-production/web
docker push registry.heroku.com/ob-server-production/web
heroku container:release web --app ob-server-production
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}

- name: Pull and Release Image for Swaps Tracker Production
run: |
docker pull registry.heroku.com/ob-swaps-tracker-staging/worker
docker tag registry.heroku.com/ob-swaps-tracker-staging/worker registry.heroku.com/ob-swaps-tracker-production/worker
docker push registry.heroku.com/ob-swaps-tracker-production/worker
heroku container:release worker --app ob-swaps-tracker-production
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
2 changes: 2 additions & 0 deletions .github/workflows/deploy-mm-mock-heroku.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# NOTE: This pipeline is deprecated and will be removed in the near future

name: Maker Mock - Deploy to Heroku

on:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/deploy-server-heroku.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# NOTE: This pipeline is deprecated and will be removed in the near future

name: Build and Deploy to Heroku

on:
Expand Down
12 changes: 5 additions & 7 deletions cmd/order-book/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,22 @@ import (
"github.com/orbs-network/order-book/transport/rest"
)

// TODO: handle build version better
const VERSION = "1.0.1"

func main() {
setup()
}

func setup() {
log.Print("Order book version: ", VERSION)

redisAddress, found := os.LookupEnv("REDIS_URL")
if !found {
panic("REDIS_URL not set")
redisAddress, found = os.LookupEnv("REDISCLOUD_URL")
if !found {
panic("Neither REDIS_URL nor REDISCLOUD_URL is set")
}
}

opt, err := redis.ParseURL(redisAddress)
if err != nil {
panic(fmt.Errorf("failed to parse redis url: %v", err))
panic(fmt.Errorf("failed to parse redis URL: %v", err))
}

log.Printf("Redis address: %s", opt.Addr)
Expand Down
Loading
Loading