Skip to content

Commit

Permalink
- Update Dockerfile to build image cleanly
Browse files Browse the repository at this point in the history
- added secrets
- Align node version 20.11.0 with package.json
- docker push image and run
- added env debugging and removed some unused variables
- added SITE_ prefix for some env variables
- Added GitHub Workflow: build-push-and-deploy-ngx-ramblers-docker-image.yml
- Added Readme badge for docker build
  • Loading branch information
nbarrett committed Aug 29, 2024
1 parent 4ace20d commit 1179802
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
.angular/
.github/
.idea/
.dist/
.e2e/
non-vcs/
.git/
node_modules/
server/node_modules/
server/target/
server/ts-gen/
docker-compose*.yml
non-vcs/docker/docker-compose*.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Build and Deploy NGX-Ramblers Docker Image

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Log in to Docker Hub
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
- name: Build the Docker image
run: |
docker build . --file Dockerfile --tag ngx-ramblers:latest
- name: Tag the Docker image
run: docker tag ngx-ramblers:latest ${{ secrets.DOCKER_USERNAME }}/ngx-ramblers:latest
- name: Push the Docker image to Docker Hub
run: docker push ${{ secrets.DOCKER_USERNAME }}/ngx-ramblers:latest
- name: Deploy the Docker image
run: |
docker run -d -p 5000:5000 --name ngx-ramblers ${{ secrets.DOCKER_USERNAME }}/ngx-ramblers:latest \
-e AUTH_SECRET=${{ secrets.AUTH_SECRET }} \
-e AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }} \
-e AWS_BUCKET=${{ secrets.AWS_BUCKET }} \
-e AWS_REGION=${{ secrets.AWS_REGION }} \
-e AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }} \
-e CHROMEDRIVER_PATH=${{ secrets.CHROMEDRIVER_PATH }} \
-e CHROMEDRIVER_VERSION=${{ secrets.CHROMEDRIVER_VERSION }} \
-e CHROME_VERSION=${{ secrets.CHROME_VERSION }} \
-e DEBUG=${{ secrets.DEBUG }} \
-e DEBUG_COLORS=${{ secrets.DEBUG_COLORS }} \
-e GOOGLE_CHROME_BIN=${{ secrets.GOOGLE_CHROME_BIN }} \
-e GOOGLE_MAPS_APIKEY=${{ secrets.GOOGLE_MAPS_APIKEY }} \
-e MEETUP_ACCESS_TOKEN=${{ secrets.MEETUP_ACCESS_TOKEN }} \
-e MONGODB_URI=${{ secrets.MONGODB_URI }} \
-e NODE_ENV=${{ secrets.NODE_ENV }} \
-e NODE_OPTIONS=${{ secrets.NODE_OPTIONS }} \
-e WALKS_NPM_COMMAND=${{ secrets.WALKS_NPM_COMMAND }}
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Use the official Node.js image as the base image
FROM node:20.16.0
FROM node:20.11.0

# Set the working directory inside the container
WORKDIR /usr/src/app
Expand Down Expand Up @@ -37,5 +37,6 @@ RUN npm install
# Expose the port the application will run on
EXPOSE 5000

WORKDIR /usr/src/app
# Define the command to run the server application
CMD ["npm", "run", "server", "--prefix", "server"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Welcome to NGX-Ramblers!

[![NGX-Ramblers Docker Image](https://github.com/nbarrett/ngx-ramblers/actions/workflows/build-push-and-deploy-ngx-ramblers-docker-image.yml/badge.svg)](https://github.com/nbarrett/ngx-ramblers/actions/workflows/build-push-and-deploy-ngx-ramblers-docker-image.yml)
[![Heroku](https://img.shields.io/badge/heroku-%23430098.svg?style=&logo=heroku&logoColor=white)](https://github.com/nbarrett/ngx-ramblers/deployments/ngx-ramblers-staging)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![GitHub issues](https://img.shields.io/github/issues/nbarrett/ngx-ramblers)](https://github.com/nbarrett/ngx-ramblers/issues)
Expand Down
7 changes: 4 additions & 3 deletions server/lib/env-config/env-config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const APP_PREFIX: string = "NGX_RAMBLERS_";
const ENV_PREFIX: string = "SITE_";

function validatedEnvironmentVariable(variableName: string, prefixed?: boolean): string {
const resolvedName = prefixed ? APP_PREFIX + variableName : variableName;
const variableValue = process.env[resolvedName];
const resolvedName = prefixed ? ENV_PREFIX + variableName : variableName;
const variableValue = process.env[resolvedName] || process.env[variableName];
if (!variableValue) {
throw new Error("Environment variable '" + resolvedName + "' must be set");
} else {
console.info(`Environment variable '${resolvedName}' is set to '${variableValue}'`);
return variableValue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/lib/instagram/instagram-controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Instagram, SystemConfig } from "../../../projects/ngx-ramblers/src/app/
import { systemConfig } from "../config/system-config";

const debug = debugLib(envConfig.logNamespace("instagram"));
debug.enabled = true;
debug.enabled = false;

export async function configuredInstagram(): Promise<Instagram> {
const config: SystemConfig = await systemConfig();
Expand Down
2 changes: 1 addition & 1 deletion server/lib/instagram/recent-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Instagram } from "../../../projects/ngx-ramblers/src/app/models/system.
import { configuredInstagram } from "./instagram-controllers";

const debug = debugLib(envConfig.logNamespace("instagram:recent-media"));
debug.enabled = true;
debug.enabled = false;
const refreshOnEachCall = true;

async function recentMediaRequest(req: Request, res: Response) {
Expand Down
2 changes: 1 addition & 1 deletion server/lib/instagram/refresh-access-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Instagram } from "../../../projects/ngx-ramblers/src/app/models/system.
import { configuredInstagram } from "./instagram-controllers";

const debug = debugLib(envConfig.logNamespace("instagram:refresh-token"));
debug.enabled = true;
debug.enabled = false;

export async function refreshAccessToken(req: Request, res: Response) {
const instagram: Instagram = await configuredInstagram();
Expand Down
10 changes: 7 additions & 3 deletions server/lib/mongo/mongoose-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import debug from "debug";
import mongoose from "mongoose";
import transforms = require("./controllers/transforms");

const debugLog = debug(envConfig.logNamespace("local-database"));
debugLog.enabled = false;
let connected = false;

function createDebugFor(model: any): debug.Debugger {
Expand Down Expand Up @@ -45,15 +47,17 @@ export function create<T>(model: mongoose.Model<mongoose.Document>, data: T) {
}

export function connect(debug: debug.Debugger) {
return mongoose.connect(envConfig.mongo.uri, {
const mongoUri = envConfig.mongo.uri.replace(/^"|"$/g, ""); ;
debugLog("MongoDB URI:", mongoUri);
return mongoose.connect(mongoUri, {
useUnifiedTopology: true,
useNewUrlParser: true,
}).then(response => {
debug("Connected to database:", envConfig.mongo.uri, "configured models:", response.models);
debug("Connected to database:", mongoUri, "configured models:", response.models);
connected = true;
return true;
}).catch(error => {
debug("Connection failed:", envConfig.mongo.uri, "error:", error);
debug("Connection failed:", mongoUri, "error:", error);
throw error;
});
}
2 changes: 1 addition & 1 deletion server/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import memberResource = require("./mongo/routes/member-resource");

install();
const debugLog = debug(envConfig.logNamespace("server"));
debugLog.enabled = false;
debugLog.enabled = true;
const folderNavigationsUp = process.env.NODE_ENV === "production" ? "../../" : "";
const distFolder = path.resolve(__dirname, folderNavigationsUp, "../../dist/ngx-ramblers");
const currentDir = path.resolve(__dirname);
Expand Down

0 comments on commit 1179802

Please sign in to comment.