Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

terraform #7

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
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
137 changes: 87 additions & 50 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CD Lint, Build, Test, Deploy
name: CompactDisc Lint, Build, Test, Deploy

on:
push:
Expand All @@ -14,26 +14,27 @@ on:
workflow_dispatch:
inputs:
deploy:
description: "Deploy location"
description: "Which environment to deploy to"
required: true
default: "none"
type: choice
options:
- production
- staging
- prod
- test
- none

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
DEPLOY: ${{ (inputs.deploy != 'none' && inputs.deploy) || ((github.event_name == 'workflow_dispatch' && github.event.inputs.deploy == 'prod') || (github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'master') && 'prod') || ((github.event_name == 'workflow_dispatch' && github.event.inputs.deploy == 'test') || (github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'dev' && 'test') || (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'staged')) && 'test') || 'none' }}

jobs:
ci:
name: CD Lint, Build, Test, Deploy
name: CompactDisc Lint, Build, Test, Deploy
runs-on: aws-runner
env:
DEPLOY_PROD: ${{ (github.event_name == 'workflow_dispatch' && github.event.inputs.deploy == 'production') || (github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'master') }}
DEPLOY_STAGE: ${{ (github.event_name == 'workflow_dispatch' && github.event.inputs.deploy == 'staging') || (github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'dev') || (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'staged')) }}
GOLANGCI_LINT_CACHE: /home/runner/.cache/golangci-lint
concurrency:
group: ${{ github.workflow }}-ci-${{ github.ref }}
Expand All @@ -43,7 +44,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18.3
go-version: 1.18

- uses: actions/setup-node@v3
with:
Expand All @@ -59,8 +60,8 @@ jobs:

- id: cache-paths
run: |
echo "::set-output name=go-build::$(go env GOCACHE)"
echo "::set-output name=go-mod::$(go env GOMODCACHE)"
echo "go-build=$(go env GOCACHE)" >> $GITHUB_OUTPUT
echo "go-mod=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT

- name: Go Build Cache
uses: actions/cache@v3
Expand All @@ -86,6 +87,9 @@ jobs:
path: node_modules
key: ${{ runner.os }}-node_modules-${{ hashFiles('yarn.lock') }}

- name: Make
run: sudo apt-get install make

- name: Install Dependencies
run: make deps

Expand All @@ -101,64 +105,97 @@ jobs:
- name: Build App
run: make build

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
uses: aws-actions/amazon-ecr-login@v1

- name: Make build context
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
if: env.DEPLOY != 'none'
run: |
docker context create builders

- name: Setup buildx
uses: docker/setup-buildx-action@v2
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
if: env.DEPLOY != 'none'
with:
install: true
endpoint: builders

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}

- name: Build docker image
uses: docker/build-push-action@v3
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
uses: docker/build-push-action@v4
if: env.DEPLOY != 'none'
with:
context: .
file: docker/partial.Dockerfile
# cache-from: |
# type=registry,ref=gha
# cache-to: |
# type=registry,ref=gha,mode=max
tags: |
${{ steps.login-ecr.outputs.registry }}/${{ (env.DEPLOY_PROD == 'true' && '7tv') || '7tv-stage' }}/compactdisc:latest
${{ steps.login-ecr.outputs.registry }}/${{ (env.DEPLOY_PROD == 'true' && '7tv') || '7tv-stage' }}/compactdisc:${{ github.sha }}
ghcr.io/seventv/compactdisc:${{ env.DEPLOY }}-${{ github.sha }}
ghcr.io/seventv/compactdisc:${{ env.DEPLOY }}-latest
push: true

- name: Update deployment template
uses: danielr1996/[email protected]
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
env:
IMAGE: ${{ steps.login-ecr.outputs.registry }}/${{ (env.DEPLOY_PROD == 'true' && '7tv') || '7tv-stage' }}/compactdisc:${{ github.sha }}
with:
input: k8s/${{ (env.DEPLOY_PROD == 'true' && 'production') || 'staging' }}.template.yaml
output: k8s/deploy.yaml
validate:
name: CompactDisc Deploy Validation
needs: ci
runs-on: ubuntu-latest
permissions:
pull-requests: write
defaults:
run:
working-directory: ./terraform

- name: Setup Kubectl
uses: azure/[email protected]
steps:
- name: Checkout code
id: ok
if: env.DEPLOY != 'none'
uses: actions/checkout@v3

- name: Deploy to k8s
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
- name: "Setup Terraform"
if: steps.ok.outcome == 'success'
uses: hashicorp/setup-terraform@v1
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

- name: "Terraform Init"
if: steps.ok.outcome == 'success'
id: init
env:
KUBE_CONFIG_DATA: ${{ (env.DEPLOY_PROD == 'true' && secrets.KUBECONFIG) || secrets.KUBECONFIG_STAGE }}
TF_WORKSPACE: ${{ env.DEPLOY }}
run: terraform init
continue-on-error: true

- name: "Terraform Workspace"
if: steps.ok.outcome == 'success'
run: terraform workspace select -or-create=true ${{ env.DEPLOY }}

- name: Terraform fmt
if: steps.ok.outcome == 'success'
id: fmt
run: terraform fmt -check
continue-on-error: true

- name: Terraform Validate
if: steps.ok.outcome == 'success'
id: validate
run: terraform validate -no-color

- name: Terraform Variables
if: steps.ok.outcome == 'success'
run: |
mkdir -p ~/.kube
(echo $KUBE_CONFIG_DATA | base64 -d) >> ~/.kube/config

kubectl apply -f k8s/deploy.yaml
cat <<EOF > *.auto.tfvars
image_url="ghcr.io/seventv/compactdisc:${{ env.DEPLOY }}-${{ github.sha }}"
image_pull_policy="IfNotPresent"

EOF

- name: "Terraform Plan"
if: steps.ok.outcome == 'success'
id: plan
run: terraform plan -no-color

- name: "Terraform Apply"
if: steps.ok.outcome == 'success'
id: apply
run: terraform apply -no-color -auto-approve
continue-on-error: true
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ out/

go.work*
config.yaml

terraform/.*
10 changes: 6 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ func main() {

{
gctx.Inst().Mongo, err = mongo.Setup(gctx, mongo.SetupOptions{
URI: config.Mongo.URI,
DB: config.Mongo.DB,
Direct: config.Mongo.Direct,
URI: config.Mongo.URI,
DB: config.Mongo.DB,
Username: config.Mongo.Username,
Password: config.Mongo.Password,
Direct: config.Mongo.Direct,
})
if err != nil {
zap.S().Fatalw("failed to setup mongo handler",
Expand All @@ -119,7 +121,7 @@ func main() {
}

{
gctx.Inst().Query = query.New(gctx.Inst().Mongo, gctx.Inst().Redis)
gctx.Inst().Query = query.New(gctx.Inst().Mongo, gctx.Inst().Redis, nil)
}

wg := sync.WaitGroup{}
Expand Down
70 changes: 36 additions & 34 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,68 @@ go 1.18

require (
github.com/bugsnag/panicwrap v1.3.4
github.com/bwmarrin/discordgo v0.26.1
github.com/prometheus/client_golang v1.13.0
github.com/seventv/api v0.0.0-20221211205820-8dc363685efd
github.com/seventv/common v0.0.0-20230219165435-8b3b037a12ce
github.com/bwmarrin/discordgo v0.27.1
github.com/prometheus/client_golang v1.14.0
github.com/seventv/api v0.0.0-20231203001706-6ba569a151e9
github.com/seventv/common v0.0.0-20231202052307-1d4dd4479600
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.12.0
go.uber.org/zap v1.23.0
github.com/spf13/viper v1.15.0
go.uber.org/zap v1.24.0
)

require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/montanaflynn/stats v0.6.6 // indirect
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/meilisearch/meilisearch-go v0.26.0 // indirect
github.com/montanaflynn/stats v0.7.0 // indirect
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
golang.org/x/net v0.19.0 // indirect
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fasthttp/router v1.4.11
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fasthttp/router v1.4.16
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/go-redsync/redsync/v4 v4.6.0 // indirect
github.com/go-redsync/redsync/v4 v4.8.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.39.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/valyala/fasthttp v1.39.0
github.com/subosito/gotenv v1.4.2 // indirect
github.com/valyala/fasthttp v1.44.0
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.mongodb.org/mongo-driver v1.10.2
go.mongodb.org/mongo-driver v1.13.0
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be // indirect
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 // indirect
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 // indirect
golang.org/x/text v0.3.7 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading
Loading