diff --git a/.env.example b/.env.example index 1d231d8..5992cf8 100644 --- a/.env.example +++ b/.env.example @@ -9,9 +9,10 @@ POSTGRES_DB=planifetsDB # DATABASE_URL="postgresql://postgres:postgres@db:5432/planifetsDB?schema=public" # docker DATABASE_URL="postgresql://postgres:postgres@localhost:5432/planifetsDB?schema=public" # local dev + # Optional LOG_LEVELS="log,error,warn" # Log levels: "log,error,warn,debug,fatal,verbose" -APP_GIT_SHORT_SHA=localdev +APP_GIT_SHORT_SHA=$(git rev-parse --short=7 HEAD) # Monitoring POSTHOG_API_KEY="" diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 8af6c5b..a030d60 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -5,10 +5,20 @@ on: branches: - main tags: - - 'v*' + - "v*" pull_request: branches: - main + workflow_dispatch: + inputs: + environment: + description: Target environment + required: true + type: choice + options: + - staging + - dev + - prod env: REGISTRY: ghcr.io @@ -26,8 +36,8 @@ jobs: uses: actions/setup-node@v4 with: node-version: 22.x - cache: 'yarn' - cache-dependency-path: 'yarn.lock' + cache: "yarn" + cache-dependency-path: "yarn.lock" - name: Install dependencies run: yarn install --frozen-lockfile @@ -38,6 +48,7 @@ jobs: docker: runs-on: ubuntu-latest needs: [build] + environment: ${{ inputs.environment }} steps: - name: Checkout uses: actions/checkout@v4 @@ -51,8 +62,12 @@ jobs: type=ref,event=pr,prefix=pr- type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} - type=sha,prefix=main-,format=short,enable={{is_default_branch}} - type=raw,value=latest,enable={{is_default_branch}} + type=sha,prefix=main-,format=short,enable=${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} + type=sha,prefix=staging-,format=short,enable=${{ inputs.environment == 'staging' }} + type=sha,prefix=dev-,format=short,enable=${{ inputs.environment == 'dev' }} + type=raw,value=latest,enable=${{ inputs.environment == 'prod' }} + type=raw,value=staging,enable=${{ inputs.environment == 'staging' }} + type=raw,value=dev,enable=${{ inputs.environment == 'dev' }} - name: Login to Registry uses: docker/login-action@v3 diff --git a/Dockerfile b/Dockerfile index 5ba9471..c20bf8b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,11 +20,12 @@ RUN yarn build # Development FROM base AS dev WORKDIR /app +RUN apk add --no-cache git COPY prisma ./prisma ENV NODE_ENV=development ENV APP_ENV=development EXPOSE 3001 -CMD ["sh", "-c", "yarn prisma:generate && yarn start:dev"] +CMD ["sh", "-c", "export APP_GIT_SHORT_SHA=$(git rev-parse --short=7 HEAD 2>/dev/null || echo localdev) && yarn prisma:generate && yarn start:dev"] # Production FROM node:22.22.3-alpine3.22 AS production diff --git a/docker-compose.yml b/docker-compose.yml index 141cb60..324173f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -42,6 +42,7 @@ services: environment: DATABASE_URL: ${DATABASE_URL} APP_ENV: development + APP_GIT_SHORT_SHA: ${APP_GIT_SHORT_SHA:-} PORT: ${PORT:-3001} LOG_LEVELS: ${LOG_LEVELS:-log,error,warn} TZ: America/Toronto diff --git a/src/app.controller.ts b/src/app.controller.ts index c738689..20fef44 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -33,4 +33,17 @@ export class AppController { public getError() { throw new Error("Monitoring test error from /health/monitoring endpoint"); } + + @Get('info') + @ApiTags('Info') + @ApiOperation({ + summary: 'Application info', + description: 'Returns the git commit SHA and environment of the running instance', + }) + public getInfo(): { gitSha: string | null; environment: string } { + return { + gitSha: process.env.APP_GIT_SHORT_SHA ?? null, + environment: process.env.APP_ENV ?? 'development', + }; + } }