Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#68 production dockerfiles builds pushing in ci #157

Merged
merged 15 commits into from
Dec 23, 2023
Merged
3 changes: 1 addition & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Makefile
docker
README.md
docs
.env
Expand All @@ -8,4 +7,4 @@ docs
.idea
node_modules
nx
.vscode
.vscode
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=secret
DATABASE_NAME=postgres
# If certificate is set, it will use ssl for connection. If it's not set, it will set ssl: false.
#DATABASE_CERT="-----BEGIN CERTIFICATE----- ...MAg==-----END CERTIFICATE-----"

REDIS_PASSWORD=redis

Expand Down
57 changes: 57 additions & 0 deletions .github/workflows/docker_build_push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Create and publish a Docker image

# Configures this workflow to run every time a change is pushed to the branch called `release`.
on:
workflow_dispatch:
push:
branches:
- 'main'
tags:
- 'v*'
pull_request:
branches:
- 'main'

# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu.
jobs:
build-and-push-image:
runs-on: ubuntu-latest
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4

# Uses the `docker/login-action` action to log in to the Container registry using the account and
# password that will publish the packages. Once published, the packages are scoped to the account defined here.
- name: Log in to the Container registry
uses: docker/login-action@v3
if: github.event_name != 'pull_request'
with:
username: ${{ secrets.DOCKER_HUB_USER }}
password: ${{ secrets.DOCKER_HUB_TOKEN }}

# This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and
# labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be
# referenced in a subsequent step. The `images` value provides the base name for the tags and labels.
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images:
${{ env.REGISTRY || 'docker.io' }}/${{ env.IMAGE_NAME || github.repository }}

# This step uses the `docker/build-push-action` action to build the image, based on your
# repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages.
# It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository.
# It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step.
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
file: docker/deployments/api.dockerfile
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
/.vscode
pnpm-lock.yaml
package.json
.github
3 changes: 2 additions & 1 deletion apps/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"build": {
"executor": "nx:run-commands",
"options": {
"commands": ["nest build -c apps/api/nest-cli.json"]
"commands": ["nest build -c apps/api/nest-cli.json"],
"generatePackageJson": true
},
"outputs": ["{options.outputPath}"]
},
Expand Down
8 changes: 7 additions & 1 deletion apps/api/src/config/typeorm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ export const config = {
entities: [__dirname + '/../**/*.entity.{js,ts}'],
migrations: [join(__dirname, '..', 'migrations', '*.{ts,js}')],
autoLoadEntities: true,
synchronize: false
synchronize: false,
ssl: process.env.DATABASE_CERT
? {
rejectUnauthorized: false,
ca: String(process.env.DATABASE_CERT)
}
: false
}

export const typeOrmConfig = registerAs('typeorm', () => config)
46 changes: 46 additions & 0 deletions docker/deployments/api.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
FROM ubuntu:22.04

ARG NODE_MAJOR=18
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
ENV TZ=UTC

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update
RUN apt-get install -y ca-certificates curl gnupg supervisor
RUN mkdir -p /etc/apt/keyrings
RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
RUN apt-get update
RUN apt-get install nodejs -y
RUN npm install -g npm
RUN npm install -g yarn

RUN apt-get install -y zip unzip supervisor libcap2-bin libpng-dev
RUN apt-get -y autoremove
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

WORKDIR /app

COPY ./package.json ./

RUN yarn install --production=false
RUN yarn global add nx

COPY . .

RUN nx repair

RUN nx run api:build:production

COPY ./docker/deployments/start-container /usr/local/bin/start-container
COPY ./docker/deployments/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
RUN chmod +x /usr/local/bin/start-container

EXPOSE 3000

#ENTRYPOINT ["start-container"]
ENTRYPOINT ["nx", "run", "api:serve"]
#ENTRYPOINT ["bash"]
3 changes: 3 additions & 0 deletions docker/deployments/start-container
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
18 changes: 18 additions & 0 deletions docker/deployments/supervisord.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[supervisord]
nodaemon=true
user=root
logfile=/var/log/supervisor/supervisord.log
pidfile=/var/run/supervisord.pid

[program:api]
command=node /app/dist/apps/api/src/main.js
autostart=true
autorestart=true
startretries=5
numprocs=1
startsecs=0
process_name=%(program_name)s_%(process_num)02d
stderr_logfile=/var/log/supervisor/%(program_name)s_stderr.log
stderr_logfile_maxbytes=10MB
stdout_logfile=/var/log/supervisor/%(program_name)s_stdout.log
stdout_logfile_maxbytes=10MB
5 changes: 4 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"build": {
"cache": true,
"dependsOn": ["^build"],
"inputs": ["production", "^production"]
"inputs": ["production", "^production"],
"options": {
"generatePackageJson": true
}
},
"lint": {
"cache": true,
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@
"@nestjs/cli": "^10.2.1",
"@nestjs/schematics": "^10.0.1",
"@nestjs/testing": "^10.0.2",
"@nx/cypress": "17.1.1",
"@nx/detox": "17.2.3",
"@nx/eslint": "17.0.3",
"@nx/cypress": "17.0.1",
"@nx/detox": "17.0.1",
"@nx/eslint": "17.0.1",
"@nx/eslint-plugin": "17.0.1",
"@nx/expo": "^17.2.3",
"@nx/jest": "17.2.3",
"@nx/js": "17.2.3",
"@nx/nest": "17.1.2",
"@nx/expo": "^17.0.1",
"@nx/jest": "17.0.1",
"@nx/js": "17.0.1",
"@nx/nest": "17.0.1",
"@nx/next": "^17.0.1",
"@nx/node": "17.0.1",
"@nx/react": "^17.0.1",
"@nx/webpack": "17.1.1",
"@nx/workspace": "17.1.1",
"@nx/webpack": "17.0.1",
"@nx/workspace": "17.0.1",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
"@svgr/webpack": "^8.0.1",
"@swc-node/register": "~1.6.7",
Expand Down
Loading