Skip to content

Commit

Permalink
Add deploy (#367)
Browse files Browse the repository at this point in the history
* Clean up server from old logic

* Typo

* Clean up scripts for future ssdeploy CLI

* Dockernize application

* Add ssdeploy

* Fix CI

* Fix regions on preview

* Try to fix region build

* Fix client dependencies

* Fix imports

* Add deploy script

* Typo

* Simplify API url
  • Loading branch information
ai authored Aug 13, 2022
1 parent d84f65b commit 31e1efe
Show file tree
Hide file tree
Showing 16 changed files with 372 additions and 81 deletions.
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
node_modules/

server/test/
client/
!client/dist/

.*
README.md
LICENSE
nano-staged.json
simple-git-hooks.json
Dockerfile
165 changes: 165 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
name: CI
on:
push:
branches:
- main
pull_request:
env:
FORCE_COLOR: 2
jobs:

test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout the repository
uses: actions/checkout@v3
- name: Install asdf
uses: asdf-vm/actions/setup@v1
- name: Cache asdf
id: asdf-cache
uses: actions/cache@v3
with:
path: ~/.asdf
key: asdf-${{ hashFiles('**/.tool-versions') }}
- name: Install asdf tools
if: steps.asdf-cache.outputs.cache-hit != 'true'
uses: asdf-vm/actions/install@v1
- name: Cache pnpm modules
uses: actions/cache@v3
env:
cache-name: cache-pnpm-modules
with:
path: ~/.local/share/pnpm/
key: pnpm-${{ hashFiles('pnpm-lock.yaml') }}
- name: Install dependencies
run: pnpm install --frozen-lockfile --ignore-scripts
- name: Build regions
run: pnpm -r build:regions
- name: Run tests
run: pnpm test

deploy:
name: Deploy
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Notify about new deployment
uses: bobheadxi/deployments@v1
id: deployment
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
env: production
- name: Checkout the repository
uses: actions/checkout@v3
- name: Install asdf
uses: asdf-vm/actions/setup@v1
- name: Cache asdf
id: asdf-cache
uses: actions/cache@v3
with:
path: ~/.asdf
key: asdf-${{ hashFiles('**/.tool-versions') }}
- name: Cache pnpm modules
uses: actions/cache@v3
env:
cache-name: cache-pnpm-modules
with:
path: ~/.local/share/pnpm/
key: pnpm-production-${{ hashFiles('pnpm-lock.yaml') }}
- name: Install production dependencies
run: pnpm install --prod --frozen-lockfile --ignore-scripts
- name: Build regions
run: pnpm -r build:regions
- name: Build static files
run: pnpm build
- name: Auth Google Cloud
uses: google-github-actions/auth@v0
with:
credentials_json: ${{ secrets.GCLOUD_AUTH }}
- name: Install Google Cloud
uses: google-github-actions/setup-gcloud@v0
- name: Deploy files
id: deploy
run: ./node_modules/.bin/ssdeploy deploy --verbose
env:
GCLOUD_APP: ${{ secrets.GCLOUD_APP }}
GCLOUD_PROJECT: ${{ secrets.GCLOUD_PROJECT }}
CLOUDFLARE_ZONE: ${{ secrets.CLOUDFLARE_ZONE }}
CLOUDFLARE_TOKEN: ${{ secrets.CLOUDFLARE_TOKEN }}
- name: Update deployment status
uses: bobheadxi/deployments@v1
if: always()
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
env: ${{ steps.deployment.outputs.env }}
env_url: ${{ steps.deploy.outputs.url }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}

preview:
name: Preview
runs-on: ubuntu-latest
if: github.ref != 'refs/heads/main'
steps:
- name: Notify about new deployment
uses: bobheadxi/deployments@v1
id: deployment
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.head_ref }}
env: preview-${{ github.event.number }}
- name: Checkout the repository
uses: actions/checkout@v3
- name: Install asdf
uses: asdf-vm/actions/setup@v1
- name: Cache asdf
id: asdf-cache
uses: actions/cache@v3
with:
path: ~/.asdf
key: asdf-${{ hashFiles('**/.tool-versions') }}
- name: Install asdf tools
if: steps.asdf-cache.outputs.cache-hit != 'true'
uses: asdf-vm/actions/install@v1
- name: Cache pnpm modules
uses: actions/cache@v3
env:
cache-name: cache-pnpm-modules
with:
path: ~/.local/share/pnpm/
key: pnpm-production-${{ hashFiles('pnpm-lock.yaml') }}
- name: Install production dependencies
run: pnpm install --prod --frozen-lockfile --ignore-scripts
- name: Build regions
run: pnpm -r build:regions
- name: Build static files
run: pnpm build
- name: Auth Google Cloud
uses: google-github-actions/auth@v0
with:
credentials_json: ${{ secrets.GCLOUD_AUTH }}
- name: Install Google Cloud
uses: google-github-actions/setup-gcloud@v0
- name: Deploy files
id: deploy
run: ./node_modules/.bin/ssdeploy preview $PR --verbose
env:
PR: ${{ github.event.number }}
GCLOUD_APP: ${{ secrets.GCLOUD_APP }}
GCLOUD_PROJECT: ${{ secrets.GCLOUD_PROJECT }}
CLOUDFLARE_ZONE: ${{ secrets.CLOUDFLARE_ZONE }}
CLOUDFLARE_TOKEN: ${{ secrets.CLOUDFLARE_TOKEN }}
- name: Update deployment status
uses: bobheadxi/deployments@v1
if: always()
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
env: ${{ steps.deployment.outputs.env }}
env_url: ${{ steps.deploy.outputs.url }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
50 changes: 50 additions & 0 deletions .github/workflows/close.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Clean Preview
on:
pull_request:
types: [ closed ]
jobs:
close:
runs-on: ubuntu-latest
steps:
- name: Clean from GitHub
uses: bobheadxi/deployments@v1
with:
step: delete-env
token: ${{ secrets.GITHUB_TOKEN }}
env: preview-${{ github.event.number }}
- name: Checkout the repository
uses: actions/checkout@v3
- name: Install asdf
uses: asdf-vm/actions/setup@v1
- name: Cache asdf
id: asdf-cache
uses: actions/cache@v3
with:
path: ~/.asdf
key: asdf-${{ hashFiles('**/.tool-versions') }}
- name: Install asdf tools
if: steps.asdf-cache.outputs.cache-hit != 'true'
uses: asdf-vm/actions/install@v1
- name: Cache pnpm modules
uses: actions/cache@v3
env:
cache-name: cache-pnpm-modules
with:
path: ~/.local/share/pnpm/
key: pnpm-production-${{ hashFiles('pnpm-lock.yaml') }}
- name: Install dependencies
run: pnpm install --prod --frozen-lockfile --ignore-scripts
- name: Auth Google Cloud
uses: google-github-actions/auth@v0
with:
credentials_json: ${{ secrets.GCLOUD_AUTH }}
- name: Install Google Cloud
uses: google-github-actions/setup-gcloud@v0
- name: Clean from Google Cloud
run: ./node_modules/.bin/ssdeploy close $PR --verbose
env:
PR: ${{ github.event.number }}
GCLOUD_APP: ${{ secrets.GCLOUD_APP }}
GCLOUD_PROJECT: ${{ secrets.GCLOUD_PROJECT }}
CLOUDFLARE_ZONE: ${{ secrets.CLOUDFLARE_ZONE }}
CLOUDFLARE_TOKEN: ${{ secrets.CLOUDFLARE_TOKEN }}
40 changes: 0 additions & 40 deletions .github/workflows/test.yml

This file was deleted.

16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM node:18-alpine

ENV NODE_ENV production
WORKDIR /var/www
COPY --chown=node:node . /var/www

RUN corepack enable
RUN corepack prepare [email protected] --activate
COPY ./pnpm-workspace.yaml /var/www/
COPY ./package.json /var/www/
COPY ./pnpm-lock.yaml /var/www/
COPY ./server/ /var/www/server/
RUN pnpm install --filter ./server --prod --frozen-lockfile --ignore-scripts
COPY ./client/dist/ /var/www/client/dist/

CMD "node" "server/index.js"
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ the compatible browsers for a browsers query.


## Development
Can I Use

To run a local copy for development:

1. Install correct versions of Node.js and pnpm. There are two ways:
Expand Down Expand Up @@ -36,8 +36,8 @@ To run a local copy for development:
```

- Server production mode
```
pnpm production
```sh
pnpm ssdeploy run
```

We recommend to install Prettier and EditorConfig plugins to your text editor.
4 changes: 2 additions & 2 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
"dependencies": {
"@csstools/postcss-oklab-function": "^1.1.1",
"autoprefixer": "^10.4.8",
"caniuse-lite": "^1.0.30001375",
"jstransformer-markdown-it": "^3.0.0",
"postcss": "^8.4.16",
"postcss-media-minmax": "^5.0.0",
"postcss-nesting": "^10.1.10",
"postcss-opacity-percentage": "^1.1.2",
"vite": "^3.0.6",
"vite": "^3.0.7",
"vite-plugin-pug-transformer": "^1.0.2"
},
"devDependencies": {
"@size-limit/file": "^8.0.1",
"caniuse-lite": "^1.0.30001375",
"size-limit": "^8.0.1"
},
"engines": {
Expand Down
10 changes: 4 additions & 6 deletions client/scripts/build-regions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs'
import { writeFileSync, readdirSync } from 'node:fs'

const DATA_REGION_FILE = 'data/regions.json'
const REGIONS_LIST_PATH = 'node_modules/caniuse-lite/data/regions'
const REGIONS_LIST_PATH = './node_modules/caniuse-lite/data/regions'

const regions = {
continents: {
Expand All @@ -17,15 +17,13 @@ const regions = {
countryCodes: getCaniuseCountries()
}

fs.writeFileSync(DATA_REGION_FILE, JSON.stringify(regions))
writeFileSync(DATA_REGION_FILE, JSON.stringify(regions))
process.stdout.write(
`A file "client/${DATA_REGION_FILE}" with regions has been created\n`
)

function getCaniuseCountries() {
let regionCodes = fs
.readdirSync(REGIONS_LIST_PATH)
.map(file => file.split('.js')[0])
let regionCodes = readdirSync(REGIONS_LIST_PATH).map(f => f.split('.js')[0])

return regionCodes
.filter(regionCode => {
Expand Down
5 changes: 1 addition & 4 deletions client/view/Form/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
showStats
} from '../BrowserStats/browserStats.js'

const API_HOST = 'http://localhost:5000/api/'

const form = document.querySelector('[data-id=query_form]')
const textarea = document.querySelector('[data-id=query_text_area]')
const regionCoverage = document.querySelector('[data-id=region_coverage]')
Expand Down Expand Up @@ -134,8 +132,7 @@ async function updateStatsView(query, region) {
try {
form.classList.add('Form--loaded')
let urlParams = new URLSearchParams({ q: query, region })
let url = new URL(`browsers?${urlParams}`, `${API_HOST}`)
response = await fetch(url)
response = await fetch(`/api/browsers?${urlParams}`)
} catch (error) {
renderError(`Network error. Check that you are online.`)
form.classList.remove('Form--loaded')
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
},
"scripts": {
"start": "pnpm -r start",
"production": "pnpm -r build && NODE_ENV=production pnpm -r serve",
"build": "pnpm -r build",
"lint": "eslint . && stylelint **/*.css",
"test": "pnpm audit --prod && pnpm lint && pnpm -r test"
},
"dependencies": {
"ssdeploy": "^0.9.1"
},
"devDependencies": {
"@logux/eslint-config": "^47.2.0",
"@logux/stylelint-config": "^0.10.1",
Expand Down
Loading

0 comments on commit 31e1efe

Please sign in to comment.