Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.
Closed
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
180 changes: 136 additions & 44 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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') || (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'staged')) && 'test') || 'none' }}

jobs:
ci:
name: API 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 Down Expand Up @@ -99,7 +100,7 @@ jobs:
run: make generate

- name: Build Dev Portal Frontend
run: make ${{ (env.DEPLOY_PROD == 'true' && 'portal') || 'portal_stage' }}
run: make ${{ (env.DEPLOY == 'prod' && 'portal') || 'portal_stage' }}

- name: Run Linter
run: make lint
Expand All @@ -110,64 +111,155 @@ 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@v4
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
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' }}/api:latest
${{ steps.login-ecr.outputs.registry }}/${{ (env.DEPLOY_PROD == 'true' && '7tv') || '7tv-stage' }}/api:${{ github.sha }}
ghcr.io/seventv/api:${{ env.DEPLOY }}-${{ github.sha }}
ghcr.io/seventv/api:${{ env.DEPLOY }}-latest
push: true

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

- name: Setup Kubectl
uses: azure/setup-kubectl@v3.0
steps:
- name: Checkout code
id: ok
if: env.DEPLOY != 'none'
uses: actions/checkout@v3

- name: "Setup Terraform"
if: steps.ok.outcome == 'success'
uses: hashicorp/setup-terraform@v1
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

- name: Deploy to k8s
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
- 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
cat <<EOF > *.auto.tfvars
image_url="ghcr.io/seventv/api:${{ env.DEPLOY }}-${{ github.sha }}"
image_pull_policy="IfNotPresent"

EOF

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

- uses: actions/github-script@v6
if: steps.ok.outcome == 'success' && github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// 1. Retrieve existing bot comments for the PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
})
const botComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.includes('Terraform Format and Style')
})

// 2. Prepare format of the comment
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
<details><summary>Validation Output</summary>

\`\`\`\n
${{ steps.validate.outputs.stdout }}
\`\`\`

</details>

#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`

<details><summary>Show Plan</summary>

\`\`\`\n
${process.env.PLAN}
\`\`\`

</details>

*Actor: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workflow: \`${{ github.workflow }}\`*`;

// 3. If we have a comment, update it, otherwise create a new one
if (botComment) {
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: output
})
} else {
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
}

kubectl apply -f k8s/deploy.yaml
- name: "Terraform Apply"
if: steps.ok.outcome == 'success'
id: apply
run: terraform apply -no-color -auto-approve
continue-on-error: true
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ out/
tmp/
*.yaml
!example.config.yaml
!terraform/config.template.yaml
!docker-compose.yaml
!.github/**/*.yaml
*-gqlgen.go
Expand All @@ -16,3 +17,17 @@ internal/rest/v*/docs
go.work
go.work.sum
!.golangci.yaml

# Terraform local state files
**/.terraform/*
*.tfstate
*.tfstate.*
*.tfplan
crash.log
*.tfvars
override.tf
override.tf.json
*_override.tf
*_override.tf.json
.terraformrc
terraform.rc
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,9 @@ work:

dev:
go run cmd/main.go

terraform:
terraform -chdir=./terraform init

deploy:
terraform -chdir=./terraform apply -auto-approve
8 changes: 5 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,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,
Direct: config.Mongo.Direct,
Username: config.Mongo.Username,
Password: config.Mongo.Password,
})
if err != nil {
zap.S().Fatalw("failed to setup mongo handler",
Expand Down
14 changes: 10 additions & 4 deletions data/events/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ type AnyPayload interface {
}

type HelloPayload struct {
HeartbeatInterval uint32 `json:"heartbeat_interval"`
SessionID string `json:"session_id"`
SubscriptionLimit int32 `json:"subscription_limit"`
Actor *primitive.ObjectID `json:"actor,omitempty"`
HeartbeatInterval uint32 `json:"heartbeat_interval"`
SessionID string `json:"session_id"`
SubscriptionLimit int32 `json:"subscription_limit"`
Actor *primitive.ObjectID `json:"actor,omitempty"`
Instance HelloPayloadInstanceInfo `json:"instance,omitempty"`
}

type HelloPayloadInstanceInfo struct {
Name string `json:"name"`
Population int32 `json:"population"`
}

type AckPayload struct {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/json-iterator/go v1.1.12
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v1.14.0
github.com/seventv/common v0.0.0-20230528214454-1a842fd909aa
github.com/seventv/common v0.0.0-20230729093701-30b85f9e2678
github.com/seventv/compactdisc v0.0.0-20221006190906-ccfe99954e48
github.com/seventv/image-processor/go v0.0.0-20221128171540-d050701ac324
github.com/seventv/message-queue/go v0.0.0-20220721124044-9fd23bda9643
Expand Down
Loading