From f928b75678a5f4211ff925b99ea69ba2719f1809 Mon Sep 17 00:00:00 2001 From: enricobuehler Date: Sun, 12 Jan 2025 20:33:10 +0100 Subject: [PATCH] add ci containerize --- .dockerignore | 1 + .gitea/workflows/deploy.yml | 49 +++++++++++++++++++++++++++++++++++++ Dockerfile | 37 ++++++++++++++++++++++++++++ package.json | 3 ++- src/lib/utils.test.ts | 13 ++++++++++ 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 .gitea/workflows/deploy.yml create mode 100644 Dockerfile create mode 100644 src/lib/utils.test.ts diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +node_modules diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 0000000..d509872 --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -0,0 +1,49 @@ +name: release-tag + +on: + push + +jobs: + release-image: + runs-on: ubuntu-latest + container: + image: catthehacker/ubuntu:act-latest +# env: +# DOCKER_ORG: teacup +# DOCKER_LATEST: nightly +# RUNNER_TOOL_CACHE: /toolcache + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker BuildX + uses: docker/setup-buildx-action@v2 + + - name: Login to DockerHub + uses: docker/login-action@v2 + with: + registry: git.unom.io # replace it with your local IP + username: ${{ vars.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_TOKEN }} + + - name: Get Meta + id: meta + run: | + echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}') >> $GITHUB_OUTPUT + echo REPO_VERSION=$(git describe --tags --always | sed 's/^v//') >> $GITHUB_OUTPUT + + - name: Build and push + uses: docker/build-push-action@v4 + with: + context: . + file: ./Dockerfile + platforms: | + linux/amd64 + linux/arm64 + push: true + tags: | # replace it with your local IP and tags + git.unom.io/${{ env.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.REPO_VERSION }} + git.unom.io/${{ env.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}:${{ env.DOCKER_LATEST }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b487ff7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +# use the official Bun image +# see all versions at https://hub.docker.com/r/oven/bun/tags +FROM oven/bun:1 AS base +WORKDIR /usr/src/app + +# install dependencies into temp directory +# this will cache them and speed up future builds +FROM base AS install +RUN mkdir -p /temp/dev +COPY package.json bun.lockb /temp/dev/ +RUN cd /temp/dev && bun install --frozen-lockfile + +# install with --production (exclude devDependencies) +RUN mkdir -p /temp/prod +COPY package.json bun.lockb /temp/prod/ +RUN cd /temp/prod && bun install --frozen-lockfile --production + +# copy node_modules from temp directory +# then copy all (non-ignored) project files into the image +FROM base AS prerelease +COPY --from=install /temp/dev/node_modules node_modules +COPY . . + +# [optional] tests & build +ENV NODE_ENV=production +RUN bun test +#RUN bun run build + +# copy production dependencies and source code into final image +FROM base AS release +COPY --from=install /temp/prod/node_modules node_modules +COPY --from=prerelease /usr/src/app/ . + +# run the app +USER bun +EXPOSE 3000/tcp +ENTRYPOINT [ "bun", "run", "index.ts" ] \ No newline at end of file diff --git a/package.json b/package.json index 8d2c927..53d6aeb 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,8 @@ "module": "src/index.ts", "type": "module", "scripts": { - "dev": "bun --watch src/index.ts" + "dev": "bun --watch src/index.ts", + "test": "bun test" }, "devDependencies": { "@types/bun": "^1.1.14", diff --git a/src/lib/utils.test.ts b/src/lib/utils.test.ts new file mode 100644 index 0000000..57b9479 --- /dev/null +++ b/src/lib/utils.test.ts @@ -0,0 +1,13 @@ +import { describe, expect, it } from "bun:test"; +import { getRandomInt } from "./utils.ts"; + +describe("utils", () => { + it("gets a random int", () => { + const randomInt = getRandomInt(0, 10); + + expect(randomInt).toBeDefined(); + expect(randomInt).toBeNumber(); + expect(randomInt).toBeLessThanOrEqual(10); + expect(randomInt).toBeGreaterThanOrEqual(0); + }); +});