From e43370c451c5d05370259980884f77837ed6a0ba Mon Sep 17 00:00:00 2001 From: Iacami Gevaerd Date: Sat, 9 Dec 2023 23:35:33 -0300 Subject: [PATCH 1/7] minor improvements to DX: jest running on src files; tweaks to GH actions (#364) * chore: make jest tests work in src (not build) files * chore: unify gh workflows with docker build and argo deploy --- .github/workflows/docker.yml | 44 ------------- .github/workflows/nodejs.yml | 123 +++++++++++++++++++++++++++++++---- jest.config.cjs | 15 ++++- package.json | 2 +- tsconfig.json | 3 +- 5 files changed, 127 insertions(+), 60 deletions(-) delete mode 100644 .github/workflows/docker.yml diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml deleted file mode 100644 index d2ed337d..00000000 --- a/.github/workflows/docker.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: 'docker publish' -on: - push: - branches: - - develop - -jobs: - build: - runs-on: 'ubuntu-latest' - - steps: - - name: Setup Node.js environment - uses: actions/setup-node@v3 - with: - node-version: '16' - - - name: 'Checkout Project' - uses: 'actions/checkout@v3' - with: - fetch-depth: 1 - - - name: Install Dependencies and lint code - run: yarn install && yarn lint - - - name: Test build - run: yarn build-release - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 - - - name: Login to DockerHub - uses: docker/login-action@v2 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Build docker image - uses: docker/build-push-action@v2 - with: - context: ./ - file: ./Dockerfile - builder: ${{ steps.buildx.outputs.name }} - push: true - tags: vnguyen/openbeta-graph-api:latest diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 88cde9a8..9d4c773a 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -4,25 +4,120 @@ on: push: branches: - develop + pull_request: + release: + types: [published] + jobs: - build: - runs-on: 'ubuntu-latest' + # deploy to staging by commiting to api-server-deployment repo + deploy-staging: + runs-on: ubuntu-latest + if: github.ref_name == 'develop' + needs: docker + steps: + - name: 'Checkout Project' + uses: 'actions/checkout@v4' + with: + fetch-depth: 1 + repository: 'api-server-deployment' + token: ${{ secrets.GH_DEPLOYMENT_REPO_TOKEN }} + - uses: imranismail/setup-kustomize@v2 + - run: | + kustomize edit set image vnguyen/openbeta-graph-api:sha-${GITHUB_SHA} + - name: Check if there are changes + id: changes + uses: UnicornGlobal/has-changes-action@v1.0.11 + - name: Push if tag has changes + if: steps.changes.outputs.changed == 1 + run: | + cd api-server-deployment/stage + git config user.name github-actions + git config user.email github-actions@github.com + git commit -am "Update deployment. Code=${GITHUB_SHA}" + git push + # deploy to prod by commiting to api-server-deployment repo + deploy-prod: + runs-on: ubuntu-latest + if: startsWith(github.ref, 'refs/tags/v') + needs: docker steps: - - name: Setup Node.js environment - uses: actions/setup-node@v3 + - name: 'Checkout Project' + uses: 'actions/checkout@v4' with: - node-version: '16.15' + fetch-depth: 1 + repository: 'api-server-deployment' + token: ${{ secrets.GH_DEPLOYMENT_REPO_TOKEN }} + - uses: imranismail/setup-kustomize@v2 + - run: | + kustomize edit set image vnguyen/openbeta-graph-api:${GITHUB_REF} + - name: Check if there are changes + id: changes + uses: UnicornGlobal/has-changes-action@v1.0.11 + - name: Push if tag has changes + if: steps.changes.outputs.changed == 1 + run: | + git config user.name github-actions + git config user.email github-actions@github.com + git commit -am "Update deployment. Code=${GITHUB_REF}" + git push + + # build docker image and push to registry + docker: + runs-on: ubuntu-latest + needs: test + steps: + - name: 'Checkout Project' + uses: 'actions/checkout@v3' + with: + fetch-depth: 1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to DockerHub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: | + vnguyen/openbeta-graph-api + tags: | + type=ref,event=branch + type=semver,pattern={{raw}} + type=ref,event=pr + type=sha,format=long + + - name: Build docker image + uses: docker/build-push-action@v2 + with: + context: ./ + file: ./Dockerfile + builder: ${{ steps.buildx.outputs.name }} + push: true + tags: ${{ steps.meta.outputs.tags }} + # setup basic machine to run all kinds of tests: lint, unit, integration, types + test: + runs-on: 'ubuntu-latest' + steps: - name: 'Checkout Project' uses: 'actions/checkout@v3' with: fetch-depth: 1 - - name: Install Dependencies and lint code - run: yarn install && yarn lint + - name: Setup Node.js environment + uses: actions/setup-node@v3 + with: + node-version: '16.15' + cache: 'yarn' - name: Start MongoDB uses: supercharge/mongodb-github-action@1.8.0 @@ -30,6 +125,12 @@ jobs: mongodb-version: 5 mongodb-replica-set: rs0 + - name: Install dependencies + run: yarn install --immutable + + - name: Lint code + run: yarn lint + - name: Wait for the database to start run: wget -qO- https://raw.githubusercontent.com/eficode/wait-for/$WAIT_FOR_VERSION/wait-for | sh -s -- localhost:27017 -- echo "Database is up" env: @@ -44,8 +145,8 @@ jobs: run: | docker container restart mongodb - - name: Build and run tests - run: yarn test - env: - NODE_OPTIONS: --experimental-vm-modules + - name: Run tests + run: yarn test --ci + - name: Build sources + run: yarn build-release --noEmit diff --git a/jest.config.cjs b/jest.config.cjs index ea6b6706..18bb8139 100644 --- a/jest.config.cjs +++ b/jest.config.cjs @@ -1,7 +1,18 @@ module.exports = { - preset: 'ts-jest', + moduleNameMapper: { + '^(\\.{1,2}/.*)\\.js$': '$1' + }, + extensionsToTreatAsEsm: ['.ts'], + transform: { + '^.+\\.(mt|t|cj|j)s$': [ + 'ts-jest', + { + useESM: true + } + ] + }, testEnvironment: 'node', testMatch: [ - '/build/**/__tests__/*.js' + '/**/__tests__/*.ts' ] } diff --git a/package.json b/package.json index fbbfcbde..a83b795f 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,7 @@ "scripts": { "lint": "yarn ts-standard", "fix": "yarn ts-standard --fix", - "test": "yarn build && cross-env NODE_OPTIONS=\"--experimental-vm-modules\" jest --runInBand", + "test": "cross-env NODE_OPTIONS=\"--experimental-vm-modules\" jest --runInBand", "build": "tsc -p tsconfig.json", "build-release": "tsc -p tsconfig.release.json", "clean": "tsc -b --clean && rm -rf build/*", diff --git a/tsconfig.json b/tsconfig.json index 871ed609..3b503295 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,6 @@ "module": "ESNext", "esModuleInterop": true, "moduleResolution": "Node", - "rootDir": "./src", "outDir": "build", "sourceMap": true, "strictNullChecks": true, @@ -21,4 +20,4 @@ "include": [ "src/**/*.ts", ] -} \ No newline at end of file +} From dbc5659cb24e5d0438a480cce93bd1296fc94f1f Mon Sep 17 00:00:00 2001 From: Viet Nguyen <3805254+vnugent@users.noreply.github.com> Date: Sat, 9 Dec 2023 20:43:53 -0700 Subject: [PATCH 2/7] fix: build break (#366) * fix: build break * don't push docker img on pr --- .github/workflows/nodejs.yml | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 9d4c773a..95989644 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -13,15 +13,15 @@ on: jobs: # deploy to staging by commiting to api-server-deployment repo deploy-staging: - runs-on: ubuntu-latest - if: github.ref_name == 'develop' + runs-on: ubuntu-22.04 + # if: github.ref_name == 'develop' needs: docker steps: - name: 'Checkout Project' uses: 'actions/checkout@v4' with: fetch-depth: 1 - repository: 'api-server-deployment' + repository: 'openbeta/api-server-deployment' token: ${{ secrets.GH_DEPLOYMENT_REPO_TOKEN }} - uses: imranismail/setup-kustomize@v2 - run: | @@ -40,7 +40,7 @@ jobs: # deploy to prod by commiting to api-server-deployment repo deploy-prod: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 if: startsWith(github.ref, 'refs/tags/v') needs: docker steps: @@ -48,7 +48,7 @@ jobs: uses: 'actions/checkout@v4' with: fetch-depth: 1 - repository: 'api-server-deployment' + repository: 'openbeta/api-server-deployment' token: ${{ secrets.GH_DEPLOYMENT_REPO_TOKEN }} - uses: imranismail/setup-kustomize@v2 - run: | @@ -66,7 +66,8 @@ jobs: # build docker image and push to registry docker: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 + if: github.event_name != 'pull_request' needs: test steps: - name: 'Checkout Project' @@ -106,7 +107,7 @@ jobs: # setup basic machine to run all kinds of tests: lint, unit, integration, types test: - runs-on: 'ubuntu-latest' + runs-on: 'ubuntu-22.04' steps: - name: 'Checkout Project' uses: 'actions/checkout@v3' From 937f8252277736c2495c8d7f6a6161dc02d3c3a8 Mon Sep 17 00:00:00 2001 From: Viet Nguyen <3805254+vnugent@users.noreply.github.com> Date: Sat, 9 Dec 2023 20:48:35 -0700 Subject: [PATCH 3/7] Pr 364 followup (#367) * fix: build break * don't push docker img on pr * forgot to uncomment original code --- .github/workflows/nodejs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 95989644..93b8f37a 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -14,7 +14,7 @@ jobs: # deploy to staging by commiting to api-server-deployment repo deploy-staging: runs-on: ubuntu-22.04 - # if: github.ref_name == 'develop' + if: github.ref_name == 'develop' needs: docker steps: - name: 'Checkout Project' From d7fad9ffd1491ba33bb0f942a91937cc7e708bc0 Mon Sep 17 00:00:00 2001 From: Iacami Gevaerd Date: Sun, 10 Dec 2023 12:20:26 -0300 Subject: [PATCH 4/7] build: always build dockerfile but skip pushing if PR (#368) * build: always build dockerfile but skip pushing if PR * build: add default branch for deployment in order to skip looking it up * build: replace has-changes action with just a command --- .github/workflows/nodejs.yml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 93b8f37a..73d050e7 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -23,14 +23,15 @@ jobs: fetch-depth: 1 repository: 'openbeta/api-server-deployment' token: ${{ secrets.GH_DEPLOYMENT_REPO_TOKEN }} + ref: main - uses: imranismail/setup-kustomize@v2 - run: | kustomize edit set image vnguyen/openbeta-graph-api:sha-${GITHUB_SHA} - - name: Check if there are changes - id: changes - uses: UnicornGlobal/has-changes-action@v1.0.11 + - name: Check if there is any change + id: get_changes + run: echo "changed=$(git status --porcelain | wc -l)" >> $GITHUB_OUTPUT - name: Push if tag has changes - if: steps.changes.outputs.changed == 1 + if: steps.get_changes.outputs.changed != 0 run: | cd api-server-deployment/stage git config user.name github-actions @@ -50,14 +51,15 @@ jobs: fetch-depth: 1 repository: 'openbeta/api-server-deployment' token: ${{ secrets.GH_DEPLOYMENT_REPO_TOKEN }} + ref: main - uses: imranismail/setup-kustomize@v2 - run: | kustomize edit set image vnguyen/openbeta-graph-api:${GITHUB_REF} - - name: Check if there are changes - id: changes - uses: UnicornGlobal/has-changes-action@v1.0.11 + - name: Check if there is any change + id: get_changes + run: echo "changed=$(git status --porcelain | wc -l)" >> $GITHUB_OUTPUT - name: Push if tag has changes - if: steps.changes.outputs.changed == 1 + if: steps.get_changes.outputs.changed != 0 run: | git config user.name github-actions git config user.email github-actions@github.com @@ -67,7 +69,6 @@ jobs: # build docker image and push to registry docker: runs-on: ubuntu-22.04 - if: github.event_name != 'pull_request' needs: test steps: - name: 'Checkout Project' @@ -93,7 +94,6 @@ jobs: tags: | type=ref,event=branch type=semver,pattern={{raw}} - type=ref,event=pr type=sha,format=long - name: Build docker image @@ -102,7 +102,7 @@ jobs: context: ./ file: ./Dockerfile builder: ${{ steps.buildx.outputs.name }} - push: true + push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} # setup basic machine to run all kinds of tests: lint, unit, integration, types From 030ebaa1a5391881f8c77a7a94def8e24cd390b2 Mon Sep 17 00:00:00 2001 From: Iacami Gevaerd Date: Sun, 10 Dec 2023 12:33:48 -0300 Subject: [PATCH 5/7] build/deploy: kustomize path fix stage/prod (#369) --- .github/workflows/nodejs.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 73d050e7..c7736367 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -26,6 +26,7 @@ jobs: ref: main - uses: imranismail/setup-kustomize@v2 - run: | + cd stage kustomize edit set image vnguyen/openbeta-graph-api:sha-${GITHUB_SHA} - name: Check if there is any change id: get_changes @@ -33,7 +34,6 @@ jobs: - name: Push if tag has changes if: steps.get_changes.outputs.changed != 0 run: | - cd api-server-deployment/stage git config user.name github-actions git config user.email github-actions@github.com git commit -am "Update deployment. Code=${GITHUB_SHA}" @@ -54,6 +54,7 @@ jobs: ref: main - uses: imranismail/setup-kustomize@v2 - run: | + cd prod kustomize edit set image vnguyen/openbeta-graph-api:${GITHUB_REF} - name: Check if there is any change id: get_changes From 6d3039624b96a23cd25a0026cd48aeb1540f980c Mon Sep 17 00:00:00 2001 From: Iacami Gevaerd Date: Sun, 10 Dec 2023 13:01:28 -0300 Subject: [PATCH 6/7] fix: build kustomize step consuming right ref reference for tag (#370) --- .github/workflows/nodejs.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index c7736367..21b5ad48 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -55,7 +55,7 @@ jobs: - uses: imranismail/setup-kustomize@v2 - run: | cd prod - kustomize edit set image vnguyen/openbeta-graph-api:${GITHUB_REF} + kustomize edit set image vnguyen/openbeta-graph-api:${GITHUB_REF_NAME} - name: Check if there is any change id: get_changes run: echo "changed=$(git status --porcelain | wc -l)" >> $GITHUB_OUTPUT @@ -64,7 +64,7 @@ jobs: run: | git config user.name github-actions git config user.email github-actions@github.com - git commit -am "Update deployment. Code=${GITHUB_REF}" + git commit -am "Update deployment. Code=${GITHUB_REF_NAME}" git push # build docker image and push to registry From cfada19cad563b8ae2ddd3cb3ec9b01ca9106894 Mon Sep 17 00:00:00 2001 From: Iacami Gevaerd Date: Tue, 12 Dec 2023 12:02:51 -0300 Subject: [PATCH 7/7] build: skip dockerhub login if it's a PR (#373) --- .github/workflows/nodejs.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 21b5ad48..e59427a7 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -82,6 +82,7 @@ jobs: - name: Login to DockerHub uses: docker/login-action@v2 + if: github.event_name != 'pull_request' with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }}