Created
November 29, 2023 06:57
-
-
Save minho-comcom-ai/44872c16c826603fdef37ac3ed67a666 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FROM _ | |
| LABEL maintainer="_" | |
| # > Install and check versions | |
| # > Install | |
| # > Install jq | |
| # > Install packages | |
| # > Copy file | |
| # > Build Args | |
| ARG PORT=4080 | |
| # production / development | |
| ARG NODE_ENV=production | |
| # Inject DOCKER_HOST for (Local) Docker (`-v /var/run/docker.sock:/var/run/docker.sock`) | |
| ARG DOCKER_HOST=unix:///var/run/docker.sock | |
| # (Additional) Inject KUBECONFIG for (Local) Kubernetes (`-v ~/.kube/config:/root/.kube/config`) | |
| ARG KUBECONFIG=/root/.kube/config | |
| # (Additional) Inject SERVICE_ACCOUNT_CREDENTIALS (`-e SERVICE_ACCOUNT_CREDENTIALS=$SERVICE_ACCOUNT_CREDENTIALS`) | |
| ARG SERVICE_ACCOUNT_CREDENTIALS | |
| # (Additional) Inject MINIMIZE_FIREBASE_CONFIG_JSON (`-e MINIMIZE_FIREBASE_CONFIG_JSON=$MINIMIZE_FIREBASE_CONFIG_JSON`) | |
| ARG MINIMIZE_FIREBASE_CONFIG_JSON | |
| # (Additional) Connect with GKE Cluster | |
| #- required: SERVICE_ACCOUNT_CREDENTIALS | |
| #- conflict: KUBECONFIG | |
| ARG GKE_CLUSTER_NAME | |
| ARG GKE_CLUSTER_ZONE | |
| # (Additional) Feature Flags | |
| ARG FLAGS | |
| # 8. Additional Metadata | |
| ARG BUILD_DATE | |
| ARG VCS_REF | |
| ARG VERSION | |
| ARG VCS_BRANCH | |
| ARG VCS_DESCRIBED | |
| ARG HOSTNAME | |
| LABEL org.label-schema.schema-version="1.0" \ | |
| org.label-schema.name="minimize-api-server" \ | |
| org.label-schema.vendor="" \ | |
| org.label-schema.description="" \ | |
| org.label-schema.build-date=$BUILD_DATE \ | |
| org.label-schema.url="https://minimize.ai/" \ | |
| org.label-schema.vcs-ref=$VCS_REF \ | |
| org.label-schema.vcs-url="" \ | |
| org.label-schema.version=$VERSION \ | |
| ai.minimize.current-branch="$VCS_BRANCH" \ | |
| ai.minimize.current-branch-described="$VCS_DESCRIBED" \ | |
| ai.minimize.build-hostname="$HOSTNAME" | |
| # 9. Run | |
| ENV GKE_CLUSTER_NAME $GKE_CLUSTER_NAME | |
| ENV NODE_ENV $NODE_ENV | |
| ENV DOCKER_HOST $DOCKER_HOST | |
| ENV KUBECONFIG $KUBECONFIG | |
| ENV SERVICE_ACCOUNT_CREDENTIALS $SERVICE_ACCOUNT_CREDENTIALS | |
| ENV MINIMIZE_FIREBASE_CONFIG_JSON $MINIMIZE_FIREBASE_CONFIG_JSON | |
| ENV GKE_CLUSTER_ZONE $GKE_CLUSTER_ZONE | |
| ENV FLAGS $FLAGS | |
| ENV PORT $PORT | |
| EXPOSE $PORT | |
| RUN node version.js $BUILD_DATE $VCS_REF $VCS_BRANCH $VCS_DESCRIBED $HOSTNAME | |
| CMD ["npm", "start"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SHELL := /bin/bash | |
| BUILD_DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") | |
| VERSION := $(shell python3 -c 'import json;print(json.load(open("package.json"))["version"])') | |
| VCS_REF := $(shell git rev-parse --short HEAD) | |
| VCS_BRANCH := $(shell git rev-parse --abbrev-ref HEAD) | |
| VCS_DESCRIBED := $(shell git describe --tags --dirty) | |
| HOSTNAME := $(shell hostname) | |
| docker_build: | |
| docker build \ | |
| --build-arg BUILD_DATE="$(BUILD_DATE)" \ | |
| --build-arg VCS_REF="$(VCS_REF)" \ | |
| --build-arg VERSION="$(VERSION)" \ | |
| --build-arg VCS_BRANCH="$(VCS_BRANCH)" \ | |
| --build-arg VCS_DESCRIBED="$(VCS_DESCRIBED)" \ | |
| --build-arg HOSTNAME="$(HOSTNAME)" \ | |
| -t minimize-api-server . | |
| docker_tag_prod: | |
| docker tag minimize-api-server asia.gcr.io/minimize-cloud-run/internal/minimize-api-server:latest | |
| docker tag minimize-api-server asia.gcr.io/minimize-cloud-run/internal/minimize-api-server:$(VCS_REF) | |
| docker_tag_staging: | |
| docker tag minimize-api-server asia.gcr.io/minimize-staging/internal/minimize-api-server:latest | |
| docker tag minimize-api-server asia.gcr.io/minimize-staging/internal/minimize-api-server:$(VCS_REF) | |
| docker_tag_dev: | |
| docker tag minimize-api-server asia.gcr.io/minimize-dev-1/internal/minimize-api-server:latest | |
| docker tag minimize-api-server asia.gcr.io/minimize-dev-1/internal/minimize-api-server:$(VCS_REF) | |
| docker_push_prod: | |
| docker push asia.gcr.io/minimize-cloud-run/internal/minimize-api-server:latest | |
| docker push asia.gcr.io/minimize-cloud-run/internal/minimize-api-server:$(VCS_REF) | |
| docker_push_staging: | |
| docker push asia.gcr.io/minimize-staging/internal/minimize-api-server:latest | |
| docker push asia.gcr.io/minimize-staging/internal/minimize-api-server:$(VCS_REF) | |
| docker_push_dev: | |
| docker push asia.gcr.io/minimize-dev-1/internal/minimize-api-server:latest | |
| docker push asia.gcr.io/minimize-dev-1/internal/minimize-api-server:$(VCS_REF) | |
| test__cleanup: | |
| rm -rf coverage/ test-reports/ | |
| test__report: | |
| ./node_modules/.bin/nyc report -r html -r lcov | |
| test_simple: | |
| # ExitCode Check Prepare | |
| mkdir -p test-reports/ | |
| echo 0 > test-reports/jest.exit_code | |
| # Test! | |
| NODE_ENV=development \ | |
| FORCE_COLOR=true \ | |
| ./node_modules/.bin/nyc --no-clean -t ./coverage --silent \ | |
| ./node_modules/.bin/jest --coverage --verbose --runInBand --testResultsProcessor="jest-junit" \ | |
| test/*.test.js \ | |
| test/build/*.test.js \ | |
| || echo $$? > test-reports/jest.exit_code \ | |
| && mv coverage/coverage-final.json coverage/test_simple.json \ | |
| && mv junit.xml test-reports/test_simple.xml | |
| # TODO: add | |
| # test/auth/*.test.js \ | |
| # test/github/*.test.js \ | |
| # ExitCode Check | |
| if [[ $$(< test-reports/jest.exit_code) != "0" ]]; then exit $$(cat test-reports/jest.exit_code); fi | |
| test_integration: | |
| # Precondition | |
| test -n "$$MIN_CLUSTER_INFO" | |
| test -n "$$MIN_CONNECT_CONFIG_JSON" | |
| test -n "$$MINIMIZE_FIREBASE_CONFIG_JSON" | |
| test -n "$$SERVICE_ACCOUNT_CREDENTIALS" | |
| ./wait-for localhost:4000 | |
| # ExitCode Check Prepare | |
| mkdir -p test-reports/ | |
| echo 0 > test-reports/jest.exit_code | |
| # Test! | |
| NODE_ENV=development \ | |
| FIRESTORE_EMULATOR_HOST=localhost:8080 \ | |
| FUNCTIONS_EMULATOR_HOST=localhost:5001 \ | |
| EMULATOR_PROJECT_ID=minimize-dev-1 \ | |
| TEST_PURPOSE_TOKEN=1 \ | |
| FORCE_COLOR=true \ | |
| ./node_modules/.bin/nyc --no-clean -t ./coverage --silent \ | |
| ./node_modules/.bin/jest --coverage --verbose --runInBand --testResultsProcessor="jest-junit" \ | |
| test/MIN_connect/ \ | |
| test/deployment_process/ \ | |
| -t $$PARTIAL_TEST_CONDITION \ | |
| || echo $$? > test-reports/jest.exit_code \ | |
| && mv coverage/coverage-final.json coverage/test_integration.json \ | |
| && mv junit.xml test-reports/test_integration.xml | |
| # ExitCode Check | |
| if [[ $$(< test-reports/jest.exit_code) != "0" ]]; then exit $$(cat test-reports/jest.exit_code); fi | |
| .PHONY: docker_build docker_tag_prod docker_tag_staging docker_tag_dev docker_push_prod docker_push_staging docker_push_dev test coverage test__cleanup test__report test_simple test_integration | |
| # TODO: Docker Content Trust |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const _package = require('./package') // .json | |
| let docker | |
| try { | |
| docker = require('./.version.json') | |
| } catch (_) { | |
| // NOTE(minho): Try to get the labels from docker.sock? | |
| // https://stackoverflow.com/questions/37439887/how-to-access-the-metadata-of-a-docker-container-from-a-script-running-inside-th | |
| docker = {} | |
| } | |
| const _getVersion = () => { | |
| const versionJson = { | |
| npm: { | |
| name: _package.name, | |
| version: _package.version, | |
| }, | |
| docker, | |
| } | |
| const vcsString = JSON.stringify(docker) | |
| const versionString = `${_package.name} ${_package.version} ${vcsString}` | |
| return { | |
| versionJson, | |
| versionString, | |
| } | |
| } | |
| const xPoweredBy = () => { | |
| const { versionString } = _getVersion() | |
| return (_, res, next) => { | |
| res.setHeader('X-Powered-By', versionString) // TODO | |
| next() | |
| } | |
| } | |
| const getVersion = () => { | |
| const { versionJson } = _getVersion() | |
| return (_, res) => { | |
| res.status(200).json(versionJson) | |
| } | |
| } | |
| module.exports = { | |
| xPoweredBy, | |
| getVersion, | |
| } | |
| if (require.main === module) { | |
| // XXX: Entrypoint | |
| console.log('# version.js write .version.json') | |
| const [ | |
| __node, | |
| __versionJs, | |
| BUILD_DATE, | |
| VCS_REF, | |
| VCS_BRANCH, | |
| VCS_DESCRIBED, | |
| HOSTNAME, | |
| ] = process.argv | |
| const version = { | |
| BUILD_DATE, | |
| VCS_REF, | |
| VCS_BRANCH, | |
| VCS_DESCRIBED, | |
| HOSTNAME, | |
| } | |
| require('fs').writeFileSync('.version.json', JSON.stringify(version)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment