Skip to content

Commit

Permalink
wip makefile, github workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
gamesguru committed Mar 2, 2024
1 parent 4644288 commit c936d61
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
name: ci

'on':
push:
branches:
- '**'

permissions:
contents: write

jobs:
ci:
runs-on: [self-hosted, dev]
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Extras / Count Lines of Source Code
run: make extras/cloc

- name: Install requirements
run: make init

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Build [Dev] (and Deploy)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- name: Build (dev)
if: github.ref == 'refs/heads/dev'
run: pnpm build:dev

- name: Deploy (dev) [Copy static files over]
if: github.ref == 'refs/heads/dev'
run: rm -rf /var/www/app/* && mv build/* /var/www/app/

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Lint
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- name: Lint
run: pnpm lint

- name: Check
run: pnpm check

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Build [Prod] (and Upload binary)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- name: Check release version
id: check-release-version
if: github.ref == 'refs/heads/master'
env:
GH_TOKEN: ${{ github.token }}
run: |
RELEASE_TAG=v$(jq -r .version package.json)
echo RELEASE_TAG=$RELEASE_TAG
test -n "$RELEASE_TAG"
# Test that github-cli is working
gh --version
gh release list -L 1
# TODO: enhance this to be: if release_tag > current_prod_tag, deploy
gh release view $RELEASE_TAG || echo "PUBLISH=1" >> "$GITHUB_OUTPUT"
# yamllint disable rule:line-length
- name: Build (production release)
if: github.ref == 'refs/heads/master' && steps.check-release-version.outputs.PUBLISH
env:
GH_TOKEN: ${{ github.token }}
run: set -o pipefail; make build

- name: Upload artifacts (production release)
if: github.ref == 'refs/heads/master' && steps.check-release-version.outputs.PUBLISH
env:
GH_TOKEN: ${{ github.token }}
run: set -o pipefail; make deploy/upload
120 changes: 120 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
SHELL=/bin/bash

.DEFAULT_GOAL := _help

# NOTE: must put a <TAB> character and two pound "\t##" to show up in this list. Keep it brief! IGNORE_ME
.PHONY: _help
_help:
@grep -h "##" $(MAKEFILE_LIST) | grep -v IGNORE_ME | sed -e 's/##//' | column -t -s $$'\t'



# ---------------------------------------
# Install requirements
# ---------------------------------------

.PHONY: init
init: ## Install requirements (w/o --frozen-lockfile)
# Check version
[[ "$(shell pnpm --version)" =~ "8." ]]
# Remove old install
rm -rf node_modules/
# Install requirements
pnpm install
# Sync svelte kit (optional)
pnpm svelte-kit sync



# ---------------------------------------
# Run, lint & format
# ---------------------------------------

.PHONY: run
run:
pnpm dev

.PHONY: format
format: ## pnpm format
pnpm format

.PHONY: lint
lint: ## pnpm lint && pnpm check
pnpm lint
pnpm check



# ---------------------------------------
# Build & install
# ---------------------------------------

APP_VERSION ?= v$(shell jq -r .version package.json)
APP_BUNDLE ?= build-${APP_VERSION}.tar.xz
APP_RELEASE_DATE ?= $(shell date --iso)

.PHONY: build
build: clean
build: ## Build the release
./sql/build.sh
du -h ${APP_BUNDLE}

.PHONY: deploy/upload
deploy/upload: ## Upload to GitHub releases
test -n "${APP_VERSION}"
test -f ${APP_BUNDLE}
gh release create ${APP_VERSION} --generate-notes
gh release upload ${APP_VERSION} ${APP_BUNDLE}

.PHONY: deploy/delete
deploy/delete:
gh release delete ${APP_VERSION}
git push origin --delete ${APP_VERSION}
- git tag -d ${APP_VERSION}


REMOTE_HEAD ?= origin/master

.PHONY: _check-git-up-to-date
_check-git-up-to-date:
git branch --show-current
git fetch
# Check that we are in sync with ${REMOTE_HEAD}
git diff --quiet ${REMOTE_HEAD}

PROJECT_NAME ?= web
DEPLOY_URL ?= https://nutra.tk/

.PHONY: deploy/install-prod
deploy/install-prod: _check-git-up-to-date
deploy/install-prod: ## Install (on prod VPS)
# Check the version string was extracted from package.json
test -n "${APP_VERSION}"
# Download ${APP_VERSION}
curl -sSLO https://github.com/nutratech/${PROJECT_NAME}/releases/download/${APP_VERSION}/${APP_BUNDLE}
tar xf ${APP_BUNDLE}
rm -f ${APP_BUNDLE}
# Copy in place
rm -rf /var/www/app/* && mv build/* /var/www/app/
# Test live URL
curl -fI ${DEPLOY_URL}



# ---------------------------------------
# Clean & extras
# ---------------------------------------

CLEAN_LOCS_ROOT ?= *.tar.xz build/

.PHONY: clean
clean: ## Clean up leftover bits and stuff from build
rm -rf ${CLEAN_LOCS_ROOT}

.PHONY: purge
purge: ## Purge package-lock.json && node_modules/
rm -rf package-lock.json pnpm-lock.yaml node_modules/

.PHONY: extras/cloc
extras/cloc:
cloc HEAD --exclude-dir=svelte.config.js,pnpm-lock.yaml,package-lock.json

0 comments on commit c936d61

Please sign in to comment.