Skip to content

Commit afb5d9a

Browse files
committed
chore: configure docker continuous deployment
1 parent ef60d17 commit afb5d9a

File tree

11 files changed

+559
-2
lines changed

11 files changed

+559
-2
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "leq-devcontainer",
3-
"dockerComposeFile": "docker-compose.yml",
3+
"dockerComposeFile": "docker-compose.yaml",
44
"service": "app",
55
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}"
66
}
File renamed without changes.

.github/workflows/cd-publish.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Publish
2+
3+
on:
4+
pull_request:
5+
types:
6+
- closed
7+
branches:
8+
- main
9+
10+
jobs:
11+
publish:
12+
if: github.event.pull_request.merged == true && github.head_ref == 'changeset-release/main'
13+
name: Publish
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
with:
19+
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
20+
fetch-depth: 0
21+
22+
- name: Setup pnpm
23+
uses: pnpm/action-setup@v3
24+
25+
- name: Setup Node
26+
uses: actions/setup-node@v4
27+
with:
28+
node-version: 20
29+
registry-url: https://registry.npmjs.org
30+
cache: pnpm
31+
32+
- name: Install dependencies
33+
run: pnpm install
34+
35+
- name: Publish
36+
uses: changesets/action@v1
37+
id: changesets
38+
with:
39+
version: pnpm version
40+
publish: pnpm tag
41+
env:
42+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
43+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44+
outputs:
45+
ref: ${{ github.ref }}
46+
published: ${{ steps.changesets.outputs.published }}
47+
48+
deploy-to-registries:
49+
name: Deploy to registries
50+
uses: ./.github/workflows/cd-registries.yaml
51+
needs: publish
52+
with:
53+
ref: ${{ needs.publish.outputs.ref }}
54+
secrets: inherit
55+
if: ${{ needs.publish.outputs.published }}

.github/workflows/cd-registries.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Deploy to registries
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
ref:
7+
required: true
8+
type: string
9+
workflow_dispatch:
10+
11+
env:
12+
# Use docker.io for Docker Hub if empty
13+
REGISTRY: ghcr.io
14+
# github.repository as <account>/<repo>
15+
IMAGE_NAME: ${{ github.repository }}
16+
17+
jobs:
18+
build-and-deploy:
19+
name: Build and deploy
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Checkout repo
23+
uses: actions/checkout@v4
24+
with:
25+
ref: ${{ inputs.ref }}
26+
fetch-depth: 0
27+
28+
- name: Set up Docker Buildx
29+
uses: docker/setup-buildx-action@v3
30+
31+
- name: Log into registry ${{ env.REGISTRY }}
32+
uses: docker/login-action@v3
33+
with:
34+
registry: ${{ env.REGISTRY }}
35+
username: ${{ github.actor }}
36+
password: ${{ secrets.GITHUB_TOKEN }}
37+
38+
39+
- name: Setup pnpm
40+
uses: pnpm/action-setup@v3
41+
42+
- name: Setup Node
43+
uses: actions/setup-node@v4
44+
with:
45+
node-version: 20
46+
registry-url: https://registry.npmjs.org
47+
cache: pnpm
48+
49+
- name: Install dependencies
50+
run: pnpm install
51+
52+
- name: Derive appropriate SHAs for base and head for `nx affected` commands
53+
uses: nrwl/nx-set-shas@v3
54+
55+
- name: 'Build images'
56+
run: INPUT_GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} npx nx affected --base=$NX_BASE --head=$NX_HEAD --target=container --parallel=2
57+

apps/backend/Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Install dependencies only when needed
2+
FROM docker.io/node:lts-alpine AS deps
3+
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
4+
RUN apk add --no-cache libc6-compat
5+
WORKDIR /usr/src/app
6+
COPY dist/apps/backend/package*.json ./
7+
RUN npm install --omit=dev
8+
9+
# Production image, copy all the files and run nest
10+
FROM docker.io/node:lts-alpine AS runner
11+
RUN apk add --no-cache dumb-init
12+
ENV NODE_ENV=production
13+
ENV PORT=3000
14+
WORKDIR /usr/src/app
15+
COPY --from=deps /usr/src/app/node_modules ./node_modules
16+
COPY --from=deps /usr/src/app/package.json ./package.json
17+
COPY dist/apps/backend .
18+
RUN chown -R node:node .
19+
USER node
20+
EXPOSE 3000
21+
CMD ["dumb-init", "node", "main.js"]

apps/backend/package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,31 @@
6666
],
6767
"coverageDirectory": "../coverage",
6868
"testEnvironment": "node"
69+
},
70+
"nx": {
71+
"targets": {
72+
"container": {
73+
"executor": "@nx-tools/nx-container:build",
74+
"dependsOn": [
75+
"build"
76+
],
77+
"options": {
78+
"engine": "docker",
79+
"metadata": {
80+
"images": [
81+
"backend"
82+
],
83+
"load": true,
84+
"tags": [
85+
"type=schedule",
86+
"type=ref,event=branch",
87+
"type=ref,event=tag",
88+
"type=ref,event=pr",
89+
"type=sha,prefix=sha-"
90+
]
91+
}
92+
}
93+
}
94+
}
6995
}
7096
}

apps/frontend/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM docker.io/nginx:stable-alpine
2+
COPY dist/apps/frontend/* /usr/share/nginx/html/
3+
EXPOSE 80
4+
CMD ["nginx", "-g", "daemon off;"]

apps/frontend/package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,31 @@
2525
"typescript": "^5.5.3",
2626
"typescript-eslint": "^8.0.1",
2727
"vite": "^5.4.1"
28+
},
29+
"nx": {
30+
"targets": {
31+
"container": {
32+
"executor": "@nx-tools/nx-container:build",
33+
"dependsOn": [
34+
"build"
35+
],
36+
"options": {
37+
"engine": "docker",
38+
"metadata": {
39+
"images": [
40+
"frontend"
41+
],
42+
"load": true,
43+
"tags": [
44+
"type=schedule",
45+
"type=ref,event=branch",
46+
"type=ref,event=tag",
47+
"type=ref,event=pr",
48+
"type=sha,prefix=sha-"
49+
]
50+
}
51+
}
52+
}
53+
}
2854
}
2955
}

nx.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"targetDefaults": {
55
"build": {
66
"dependsOn": ["^build"],
7-
"cache": true
7+
"cache": true,
8+
"outputs": ["{workspaceRoot}/dist/{projectName}"]
89
},
910
"dev": {
1011
"dependsOn": ["^build"]

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"devDependencies": {
1818
"@changesets/cli": "^2.27.8",
1919
"@nestjs/cli": "^10.0.0",
20+
"@nx-tools/container-metadata": "^6.0.2",
21+
"@nx-tools/nx-container": "^6.0.2",
2022
"nx": "19.7.3"
2123
},
2224
"engines": {

0 commit comments

Comments
 (0)