Skip to content

Commit

Permalink
Twitch bot deployment (#258)
Browse files Browse the repository at this point in the history
* move go.mod to monorepo root

* feat(apps/twitch-bot): prepare for the fly.io deployment
  • Loading branch information
rasitds authored Jul 2, 2023
1 parent 78f1222 commit 7d68146
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 6 deletions.
56 changes: 56 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# flyctl launch added from .gitignore
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
**/node_modules
**/.pnp
**/.pnp.js

# testing
**/coverage

# database
**/prisma/db.sqlite
**/prisma/db.sqlite-journal

# next.js
**/.next
**/out

# production
**/build

# misc
**/.DS_Store
**/*.pem

# debug
**/npm-debug.log*
**/yarn-debug.log*
**/yarn-error.log*
**/.pnpm-debug.log*

# local env files
**/.env
**/.env*.local

# vercel
**/.vercel

# typescript
**/*.tsbuildinfo
**/pgdata

**/.turbo

# flyctl launch added from apps/discord-bot/.gitignore
apps/discord-bot/build
apps/discord-bot/node_modules
apps/discord-bot/**/.env
apps/discord-bot/**/package-lock.json

# flyctl launch added from packages/senchabot-prisma/.gitignore
packages/senchabot-prisma/**/node_modules
# Keep environment variables out of version control
packages/senchabot-prisma/**/.env
fly.toml
95 changes: 95 additions & 0 deletions .github/workflows/twitch-bot-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Deploy Twitch Bot
on:
push:
paths:
- apps/twitch-bot/**
branches:
- initial
pull_request: {}

permissions:
actions: write
contents: read

jobs:
build:
name: Build
# only build/deploy main branch on pushes
if: ${{ (github.ref == 'refs/heads/initial') && github.event_name == 'push' }}
runs-on: ubuntu-latest
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]

- name: Checkout repo
uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Fly Registry Auth
uses: docker/login-action@v2
with:
registry: registry.fly.io
username: x
password: ${{ secrets.TWITCH_BOT_FLY_API }}

- name: Docker build
uses: docker/build-push-action@v3
with:
context: .
file: ./apps/twitch-bot/Dockerfile
push: true
tags: registry.fly.io/senchabot0524tw:${{ github.ref_name }}-${{ github.sha }}
build-args: |
COMMIT_SHA=${{ github.sha }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new

# This ugly bit is necessary if you don't want your cache to grow forever
# till it hits GitHub's limit of 5GB.
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
deploy:
name: Deploy
runs-on: ubuntu-latest
needs: [build]
# only build/deploy initial branch on pushes
if: ${{ (github.ref == 'refs/heads/initial') && github.event_name == 'push' }}

steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]

- name: Checkout repo
uses: actions/checkout@v3

- name: Deploy Initial
if: ${{ github.ref == 'refs/heads/initial' }}
uses: superfly/[email protected]
with:
args: "deploy --app senchabot0524tw --image registry.fly.io/senchabot0524tw:${{ github.ref_name }}-${{ github.sha }} --config ./apps/twitch-bot/fly.toml"
env:
FLY_API_TOKEN: ${{ secrets.TWITCH_BOT_FLY_API }}

# - name: Deploy Production
# if: ${{ github.ref == 'refs/heads/main' }}
# uses: superfly/[email protected]
# with:
# args: "deploy --image registry.fly.io/${{ steps.app_name.outputs.value }}:${{ github.ref_name }}-${{ github.sha }}"
# env:
# FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
15 changes: 15 additions & 0 deletions apps/twitch-bot/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.20-alpine AS builder

WORKDIR /app
COPY . .

ARG SERVICE_NAME

RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-s -w" -o /app/bin ./apps/twitch-bot/cmd/main/*.go

FROM scratch AS prod
COPY --from=builder /app/bin bin

EXPOSE 8080

ENTRYPOINT ["./bin"]
9 changes: 4 additions & 5 deletions apps/twitch-bot/cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ import (
"os"

"github.com/gempir/go-twitch-irc/v3"
"github.com/joho/godotenv"
"github.com/senchabot-dev/monorepo/apps/twitch-bot/client"
"github.com/senchabot-dev/monorepo/apps/twitch-bot/internal/handler"
"github.com/senchabot-dev/monorepo/apps/twitch-bot/internal/service"
)

func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
//err := godotenv.Load()
//if err != nil {
// log.Fatal("Error loading .env file")
//}

twitchClient := twitch.NewClient("senchabot", os.Getenv("OAUTH"))

Expand Down
14 changes: 14 additions & 0 deletions apps/twitch-bot/fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# fly.toml app configuration file generated for twitchbottest on 2023-07-02T22:29:27+03:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "senchabot0524tw"
primary_region = "ams"

[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = false
auto_start_machines = false
min_machines_running = 1
2 changes: 1 addition & 1 deletion apps/twitch-bot/go.mod → go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/senchabot-dev/monorepo/apps/twitch-bot
module github.com/senchabot-dev/monorepo

go 1.19

Expand Down
File renamed without changes.

0 comments on commit 7d68146

Please sign in to comment.