Skip to content

Commit

Permalink
Add build/deployment workflows for Somnia (frzyc#2343)
Browse files Browse the repository at this point in the history
* Adjust Somnia to be deployable

* Add or update the Azure App Service build and deployment workflow config

* Add somnia build CI

* Fix build

* Add deployment step

* Fix issues

* Fix issue

* Fix usage

* Fix issues

* Simplify build

* Add yarnrc timeout override

* Remove eslint rules

* Fix issues

* Fix typo

* Fix lint

* Fix merge

* Add port listen

* Add comment

* Add PR comment

* Add other outputs

* Missed one reference to config.json [safe because it never exposed token in build]
  • Loading branch information
nguyentvan7 authored Aug 4, 2024
1 parent 00e5eb2 commit 500a860
Show file tree
Hide file tree
Showing 30 changed files with 3,311 additions and 396 deletions.
96 changes: 96 additions & 0 deletions .github/workflows/build-somnia.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: Build Somnia

on:
push:
branches:
- master
pull_request:
paths-ignore:
- 'apps/frontend/**'
- 'apps/frontend-e2e/**'
- 'apps/gi-frontend/**'
- 'apps/gi-frontend-e2e/**'
- 'apps/sr-frontend/**'
- 'apps/sr-frontend-e2e/**'
- 'libs/common/database-ui/**'
- 'libs/common/img-util/**'
- 'libs/common/react-util/**'
- 'libs/common/svgicons/**'
- 'libs/common/ui/**'
- 'libs/gi/art-scanner/**'
- 'libs/gi/assets/**'
- 'libs/gi/char-cards/**'
- 'libs/gi/db-ui/**'
- 'libs/gi/formula-ui/**'
- 'libs/gi/frontend-gql/**'
- 'libs/gi/i18n/**'
- 'libs/gi/page-*/**'
- 'libs/gi/prisma-schema/**'
- 'libs/gi/silly-wisher/**'
- 'libs/gi/silly-wisher-names/**'
- 'libs/gi/solver/**'
- 'libs/gi/solver-tc/**'
- 'libs/gi/supabase/**'
- 'libs/gi/svgicons/**'
- 'libs/gi/ui/**'
- 'libs/gi/uidata/**'
- 'libs/gi/wr/**'
- 'libs/sr/assets/**'
- 'libs/sr/i18n/**'
- 'libs/sr/page-*/**'
- 'libs/sr/solver/**'
- 'libs/sr/ui/**'
types: [opened, reopened, synchronize, labeled]

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- name: Get Cache Key
id: get-cache-key
run: |
echo "key=$(git ls-tree -r HEAD | grep '^160000' | sha256sum | cut -d " " -f 1)" >> $GITHUB_OUTPUT
- uses: actions/cache/restore@v4
with:
path: .git/modules
key: submodule-${{ steps.get-cache-key.outputs.key }}
restore-keys: |
submodule-
- uses: actions/checkout@v4
with:
persist-credentials: false
submodules: true
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'yarn'
- name: Yarn install
run: |
yarn install --immutable --immutable-cache
- name: Build Somnia
run: |
NX_DAEMON="false"
cp apps/somnia/src/config.empty.json apps/somnia/src/config.json
yarn run nx run somnia:build:production
- name: echos
run: |
if [[ "${{ github.ref }}" == "refs/heads/master" ]]; then
echo "somnia-prod" > environment
else
echo "somnia-test" > environment
fi
echo $(git rev-parse HEAD) > ref
echo ${{ contains(github.event.pull_request.labels.*.name, 'deploySomnia') }} > shouldDeploy
echo ${{ github.event.number }} > pr_number
- name: Archive build
uses: actions/upload-artifact@v4
with:
name: somnia_build
path: |
dist/apps/somnia
environment
ref
shouldDeploy
pr_number
79 changes: 79 additions & 0 deletions .github/workflows/deploy-somnia.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Deploy Somnia from artifact
run-name: Deploy Somnia from artifact created by "${{ github.event.workflow_run.display_title }}"

on:
workflow_run:
workflows:
- Build Somnia
types:
- completed

jobs:
set-env:
runs-on: ubuntu-latest
outputs:
env: ${{ steps.set_env.outputs.env }}
shouldDeploy: ${{ steps.set_env.outputs.shouldDeploy }}
prNumber: ${{ steps.set_env.outputs.prNumber }}
ref: ${{ steps.set_env.outputs.ref }}
steps:
- name: Download build artifact
uses: actions/download-artifact@v4
with:
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Set env
id: set_env
run: |
echo "env=$(cat somnia_build/environment)" >> $GITHUB_OUTPUT
echo "shouldDeploy=$(cat somnia_build/shouldDeploy)" >> $GITHUB_OUTPUT
echo "prNumber=$(cat somnia_build/prNumber)" >> $GITHUB_OUTPUT
echo "ref=$(cat somnia_build/ref)" >> $GITHUB_OUTPUT
deploy:
runs-on: ubuntu-latest
timeout-minutes: 5
needs: set-env
environment:
name: ${{ needs.set-env.outputs.env }}
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
permissions:
id-token: write # This is required for requesting the JWT
if: ${{ github.event.workflow_run.conclusion == 'success' && (needs.set-env.outputs.shouldDeploy == 'true' || needs.set-env.outputs.env == 'somnia-prod') }}
steps:
- name: Download build artifact
uses: actions/download-artifact@v4
with:
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Login to Azure
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Setup config.json and others
run: |
printf '%s' "$CONFIG" > somnia_build/dist/apps/somnia/apps/somnia/src/config.json
echo ${{ needs.set-env.outputs.ref }} > somnia_build/dist/apps/somnia/ref
echo ${{ needs.set-env.outputs.prNumber }} > somnia_build/dist/apps/somnia/prNumber
env:
CONFIG: ${{ secrets.SOMNIA_CREDENTIALS }}
- name: 'Deploy to Azure Web App'
id: deploy-to-webapp
uses: azure/webapps-deploy@v3
with:
app-name: ${{ vars.ENV_NAME }}
slot-name: 'Production'
package: somnia_build/dist/apps/somnia
- name: Output date
id: output-date
run: echo "date=$(date -u)" >> $GITHUB_OUTPUT
- name: Maintain comment
uses: actions-cool/maintain-one-comment@v3
if: ${{ needs.set-env.outputs.prNumber != '' }}
with:
token: ${{ secrets.GITHUB_TOKEN }}
number: ${{ needs.set-env.outputs.prNumber }}
body: |
[${{ needs.set-env.outputs.env }}] [${{ steps.output-date.outputs.date }}] - Deployed ${{ needs.set-env.outputs.ref }} to ${{ needs.set-env.outputs.env }}" >> $GITHUB_OUTPUT
body-include: '<!-- Created by actions-cool/maintain-one-comment -->'
update-mode: 'append'
1 change: 1 addition & 0 deletions apps/somnia/.yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
network-timeout 240000
7 changes: 7 additions & 0 deletions apps/somnia/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "somnia",
"version": "0.0.1",
"scripts": {
"start": "node main.js"
}
}
6 changes: 6 additions & 0 deletions apps/somnia/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"bundle": true,
"main": "apps/somnia/src/main.ts",
"tsConfig": "apps/somnia/tsconfig.app.json",
"minify": true,
"assets": [
"apps/somnia/src/assets",
"apps/somnia/src/data",
Expand Down Expand Up @@ -45,6 +46,11 @@
"input": "libs/gi/localization/assets/locales",
"output": "assets/gi/locales",
"glob": "**"
},
{
"input": "./apps/somnia/",
"output": "",
"glob": ".yarnrc"
}
],
"generatePackageJson": true,
Expand Down
11 changes: 3 additions & 8 deletions apps/somnia/src/commands/go/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ import {
convert,
genshinCalculatorWithEntries,
selfTag,
tagToStat,
teamData,
weaponData,
withMember,
} from '@genshin-optimizer/gi/formula'
import { getCharStat } from '@genshin-optimizer/gi/stats'

export function getAscensionStat(id: CharacterKey) {
return Object.keys(getCharStat(id).ascensionBonus)[3]
}

export function getFixed(key: StatKey) {
return key.endsWith('_') ? 1 : 0
Expand Down Expand Up @@ -54,9 +50,8 @@ export function baseCharStats(
atk: calc.compute(member0.base.atk).val,
def: calc.compute(member0.base.def).val,
}
stats[getAscensionStat(id)] = calc.compute(
calc.listFormulas(member0.listing.specialized)[0]
).val
const specialized = calc.compute(member0.char.specialized)
stats[tagToStat(specialized.meta.tag!)] = specialized.val
return stats
}

Expand Down
6 changes: 5 additions & 1 deletion apps/somnia/src/lib/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import type {
PermissionsBitField,
User,
} from 'discord.js'
import { clientid } from '../config.json'
import { readFileSync } from 'fs'
// So we can modify config.json after building, thereby not exposing credentials in our build drop
const { clientid } = JSON.parse(
readFileSync('./apps/somnia/src/config.json').toString()
)

export default {
//true if interaction author has permissions
Expand Down
24 changes: 20 additions & 4 deletions apps/somnia/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import {
REST,
Routes,
} from 'discord.js'
import { clientid, token } from './config.json'
import { readFileSync } from 'fs'
import * as http from 'http'

// So we can modify config.json after building, thereby not exposing credentials in our build drop
const { clientid, token } = JSON.parse(
readFileSync('./apps/somnia/src/config.json').toString()
)

const client = new Client({
intents: [
Expand Down Expand Up @@ -37,14 +43,15 @@ client.on(Events.MessageReactionAdd, (...args) =>
//collect commands
import * as archive from './commands/archive'
import * as button from './commands/button'
import * as databank from './commands/databank'
// TODO: Enable after i18n of databank
// import * as databank from './commands/databank'
export const Commands: Collection<string, any> = new Collection()
Commands.set(archive.slashcommand.name, archive)
Commands.set(databank.slashcommand.name, databank)
// Commands.set(databank.slashcommand.name, databank)
Commands.set(button.slashcommand.name, button)
const setcommands = [
archive.slashcommand.toJSON(),
databank.slashcommand.toJSON(),
// databank.slashcommand.toJSON(),
button.slashcommand.toJSON(),
]

Expand All @@ -56,3 +63,12 @@ rest
.catch((e: any) => console.log(e))

client.login(token)

// Listen on port 8080 for Azure Web App alive check
http
.createServer(function (_req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.write('Alive')
res.end()
})
.listen(8080)
9 changes: 9 additions & 0 deletions libs/gi/assets-data/executors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"executors": {
"gen-assets-data": {
"implementation": "./src/executors/gen-assets-data/executor",
"schema": "./src/executors/gen-assets-data/schema.json",
"description": "gen-assets-data executor"
}
}
}
6 changes: 6 additions & 0 deletions libs/gi/assets-data/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@genshin-optimizer/gi/assets-data",
"version": "0.0.1",
"type": "commonjs",
"executors": "./executors.json"
}
7 changes: 6 additions & 1 deletion libs/gi/assets-data/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
"projectType": "library",
"tags": [],
"// targets": "to see all targets run: nx show project gi-assets-data --web",
"targets": {}
"targets": {
"gen-file": {
"executor": "@genshin-optimizer/gi/assets-data:gen-assets-data",
"outputs": ["{projectRoot}/src/AssetsData_gen.json"]
}
}
}
Loading

0 comments on commit 500a860

Please sign in to comment.