Skip to content

Commit

Permalink
Add workflows and tofu files
Browse files Browse the repository at this point in the history
  • Loading branch information
Vylpes committed Nov 9, 2024
1 parent 6acc5a9 commit 6080330
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 0 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Deploy To Prod

on:
workflow_dispatch:
push:
branches:
- main

jobs:
build:
environment: prod

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: 18.x
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test

- name: RSync to Stage Server
uses: D3rHase/[email protected]
with:
HOST: ${{ secrets.SSH_HOST }}
PORT: ${{ secrets.SSH_PORT }}
USER: ${{ secrets.SSH_USER }}
PRIVATE_SSH_KEY: ${{ secrets.SSH_KEY }}
REPOSITORY_PATH: ${{ secrets.SSH_REPO_PATH }}
SERVER_PATH: ${{ secrets.SSH_SERVER_PATH }}

deploy:
environment: prod
needs: build
runs-on: ubuntu-latest
steps:
- uses: appleboy/[email protected]
env:
DB_NAME: ${{ secrets.DB_NAME }}
DB_AUTH_USER: ${{ secrets.DB_AUTH_USER }}
DB_AUTH_PASS: ${{ secrets.DB_AUTH_PASS }}
DB_HOST: ${{ secrets.DB_HOST }}
DB_PORT: ${{ secrets.DB_PORT }}
DB_ROOT_HOST: ${{ secrets.DB_ROOT_HOST }}
DB_SYNC: ${{ secrets.DB_SYNC }}
DB_LOGGING: ${{ secrets.DB_LOGGING }}
DB_DATA_LOCATION: ${{ secrets.DB_DATA_LOCATION }}
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
BOT_VER: ${{ vars.BOT_VER }}
BOT_AUTHOR: ${{ vars.BOT_AUTHOR }}
BOT_OWNERID: ${{ vars.BOT_OWNERID }}
BOT_CLIENTID: ${{ vars.BOT_CLIENTID }}
ABOUT_FUNDING: ${{ vars.ABOUT_FUNDING }}
ABOUT_REPO: ${{ vars.ABOUT_FUNDING }}
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
port: ${{ secrets.SSH_PORT }}
envs: DB_NAME,DB_AUTH_USER,DB_AUTH_PASS,DB_HOST,DB_PORT,DB_ROOT_HOST,DB_SYNC,DB_LOGGING,DB_DATA_LOCATION,BOT_TOKEN,BOT_VER,BOT_AUTHOR,BOT_OWNERID,BOT_CLIENTID,ABOUT_FUNDING,ABOUT_REPO
script: |
cd ${{ secrets.SSH_SERVER_PATH }} \
&& docker compose down \
&& (pm2 stop vylbot_prod || true) \
&& (pm2 delete vylbot_prod || true) \
&& docker compose up -d \
&& sleep 10 \
&& yarn run db:up \
&& pm2 start --name vylbot_prod dist/vylbot.js
72 changes: 72 additions & 0 deletions .github/workflows/stage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Deploy To Stage

on:
workflow_dispatch:
push:
branches:
- develop

jobs:
build:
environment: stage

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: 18.x
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test

- name: RSync to Stage Server
uses: D3rHase/[email protected]
with:
HOST: ${{ secrets.SSH_HOST }}
PORT: ${{ secrets.SSH_PORT }}
USER: ${{ secrets.SSH_USER }}
PRIVATE_SSH_KEY: ${{ secrets.SSH_KEY }}
REPOSITORY_PATH: ${{ secrets.SSH_REPO_PATH }}
SERVER_PATH: ${{ secrets.SSH_SERVER_PATH }}

deploy:
environment: stage
needs: build
runs-on: ubuntu-latest
steps:
- uses: appleboy/[email protected]
env:
DB_NAME: ${{ secrets.DB_NAME }}
DB_AUTH_USER: ${{ secrets.DB_AUTH_USER }}
DB_AUTH_PASS: ${{ secrets.DB_AUTH_PASS }}
DB_HOST: ${{ secrets.DB_HOST }}
DB_PORT: ${{ secrets.DB_PORT }}
DB_ROOT_HOST: ${{ secrets.DB_ROOT_HOST }}
DB_SYNC: ${{ secrets.DB_SYNC }}
DB_LOGGING: ${{ secrets.DB_LOGGING }}
DB_DATA_LOCATION: ${{ secrets.DB_DATA_LOCATION }}
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
BOT_VER: ${{ vars.BOT_VER }}
BOT_AUTHOR: ${{ vars.BOT_AUTHOR }}
BOT_OWNERID: ${{ vars.BOT_OWNERID }}
BOT_CLIENTID: ${{ vars.BOT_CLIENTID }}
ABOUT_FUNDING: ${{ vars.ABOUT_FUNDING }}
ABOUT_REPO: ${{ vars.ABOUT_FUNDING }}
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
port: ${{ secrets.SSH_PORT }}
envs: DB_NAME,DB_AUTH_USER,DB_AUTH_PASS,DB_HOST,DB_PORT,DB_ROOT_HOST,DB_SYNC,DB_LOGGING,DB_DATA_LOCATION,BOT_TOKEN,BOT_VER,BOT_AUTHOR,BOT_OWNERID,BOT_CLIENTID,ABOUT_FUNDING,ABOUT_REPO
script: |
cd ${{ secrets.SSH_SERVER_PATH }} \
&& docker compose down \
&& (pm2 stop vylbot_stage || true) \
&& (pm2 delete vylbot_stage || true) \
&& docker compose up -d \
&& sleep 10 \
&& yarn run db:up \
&& pm2 start --name vylbot_stage dist/vylbot.js
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Test

on:
push:
branches:
- feature/*
- hotfix/*

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ dist
# TernJS port file
.tern-port

# Terraform files
.terraform
*.tfvars
*.tfstate.*
s3key

config.json
.DS_Store
ormconfig.json
Expand Down
26 changes: 26 additions & 0 deletions infrastructure/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions infrastructure/cloud-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#cloud-config

users:
- default
- name: vylpes
primary-group: vylpes
shell: /bin/bash
groups: users, docker, sudo
ssh-authorized-keys:
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExjBL5StgItBG853s9ffDmdLEzssj8l2p1RdfhoT/vU [email protected]
sudo: ['ALL=(ALL) NOPASSWD:ALL']

apt:
sources:
docker.list:
source: deb [arch=amd64] https://download.docker.com/linux/debian $RELEASE stable
keyid: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88
nodejs.list:
source: deb [signed-by=$KEY_FILE] https://deb.nodesource.com/node_18.x $RELEASE main
keyid: 9FD3B784BC1C6FC31A8A0A1C1655A0AB68576280

packages:
- git
- rsync
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
- nodejs
- npm
- python3
- python3-pip

runcmd:
- ufw limit ssh
- ufw enable
- npm install -g yarn pm2
- git clone https://github.com/timothymiller/cloudflare-ddns.git /home/vylpes/ddns
- cp /home/vylpes/ddns/config-example.json /home/vylpes/ddns/config.json
- echo "*/15 * * * * /home/vylpes/ddns/start-sync.sh" > /home/vylpes/COPY-TO-CRONTAB.txt
41 changes: 41 additions & 0 deletions infrastructure/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Providers
terraform {
required_providers {
vultr = {
source = "vultr/vultr"
version = "2.16.1"
}
}

backend "local" {
path = var.BACKEND_LOCATION
}
}

provider "vultr" {
api_key = var.VULTR_API_KEY
rate_limit = 100
retry_limit = 3
}

# Resources
resource "vultr_instance" "vps-app" {
label = "vps-${var.INSTANCE_NAME}-${var.INSTANCE_ENV}-${var.INSTANCE_LOCATION}-app"
hostname = "vps-${var.INSTANCE_NAME}-${var.INSTANCE_ENV}-${var.INSTANCE_LOCATION}-app"
plan = "vc2-1c-1gb"
region = var.INSTANCE_LOCATION
os_id = "2136"
enable_ipv6 = false
user_data = file("./cloud-config.yml")
backups = "enabled"
backups_schedule {
type = "daily"
hour = 1
}
activation_email = false
}

# Outputs
output "instance_ip" {
value = vultr_instance.vps-app.main_ip
}
21 changes: 21 additions & 0 deletions infrastructure/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
variable "VULTR_API_KEY" {
description = "The Vultr API Key"
}

variable "INSTANCE_NAME" {
description = "The name of the project this instance is for"
}

variable "INSTANCE_ENV" {
description = "The environment this project will be running"
default = "prod"
}

variable "INSTANCE_LOCATION" {
description = "The location all instances will be generated in"
default = "lhr"
}

variable "BACKEND_LOCATION" {
description = "The path where the backend state will be stored"
}

0 comments on commit 6080330

Please sign in to comment.