add Docker build and push scripts; update versioning and environment configuration

This commit is contained in:
Schneider Roland 2025-02-27 21:50:20 +01:00
parent a19070c767
commit e2709541ba
6 changed files with 101 additions and 7 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ node_modules
data data
*.code-workspace *.code-workspace
.vscode .vscode
.env

58
build.docker.image.sh Normal file
View File

@ -0,0 +1,58 @@
#!/bin/bash
# --- Configuration ---
IMAGE_NAME="172.22.102.100:40016/team-mmkb/simple-http-upload"
PACKAGE_JSON="package.json"
# --- Functions ---
# Function to get the current version from package.json
get_current_version() {
if [ ! -f "$PACKAGE_JSON" ]; then
echo "Error: $PACKAGE_JSON not found."
exit 1
fi
jq -r '.version' "$PACKAGE_JSON"
}
# Function to increment the major version
increment_major_version() {
local current_version="$1"
local major=$(echo "$current_version" | cut -d'.' -f1)
local minor=$(echo "$current_version" | cut -d'.' -f2)
local patch=$(echo "$current_version" | cut -d'.' -f3)
minor=$((minor + 1))
echo "$major.$minor.$patch"
}
# Function to update the package.json with the new version
update_package_json() {
local new_version="$1"
jq ".version = \"$new_version\"" "$PACKAGE_JSON" > temp.json && mv temp.json "$PACKAGE_JSON"
}
# --- Main Script ---
# Get the current version
current_version=$(get_current_version)
echo "Current version: $current_version"
# Increment the major version
new_version=$(increment_major_version "$current_version")
echo "New version: $new_version"
# Update package.json
update_package_json "$new_version"
echo "Updated $PACKAGE_JSON with version: $new_version"
# Build the Docker image with the new version
docker build -t "$IMAGE_NAME:$new_version" .
if [ $? -eq 0 ]; then
echo "Docker image built successfully: $IMAGE_NAME:$new_version"
else
echo "Error building docker image"
exit 1
fi
echo "Done!"

View File

@ -1,3 +1,4 @@
require('dotenv').config()
const express = require('express'); const express = require('express');
const fileUpload = require('express-fileupload'); const fileUpload = require('express-fileupload');
const fs = require('fs'); const fs = require('fs');
@ -6,9 +7,11 @@ const app = express();
const port = 3000; const port = 3000;
const uploadPath = process.env.UPLOAD_PATH || '/tmp'; const uploadPath = process.env.UPLOAD_PATH || '/tmp';
// const config = process.env.CONFIG || "{ \"uploadGroups\": [] }"; const config = process.env.CONFIG || "{ \"uploadGroups\": [] }";
//const config = process.env.CONFIG || "{ \"uploadGroups\": [{ \"matcher\": \"^mmkb-android-.*-test\\.apk$\", \"groupSize\": 3},{ \"matcher\": \"^mmkb-android-.*-int\\.apk$\", \"groupSize\": 3},{ \"matcher\": \"^mmkb-android-.*-prod\\.apk$\", \"groupSize\": 3}] }"; //const config = process.env.CONFIG || "{ \"uploadGroups\": [{ \"matcher\": \"^mmkb-android-.*-test\\.apk$\", \"groupSize\": 3},{ \"matcher\": \"^mmkb-android-.*-int\\.apk$\", \"groupSize\": 3},{ \"matcher\": \"^mmkb-android-.*-prod\\.apk$\", \"groupSize\": 3}] }";
console.info("Upload path: ", uploadPath);
console.info("Config: ", config);
app.use(fileUpload()); app.use(fileUpload());
@ -48,14 +51,16 @@ const processGroups = () => {
* clean up files based on the group size * clean up files based on the group size
*/ */
const cleanUp = () => { const cleanUp = () => {
console.info("Cleaning up files...");
const groups = processGroups(); const groups = processGroups();
console.info("Groups: ", groups);
groups.forEach(group => { groups.forEach(group => {
if (group.files.length > group.groupSize) { if (group.files.length > group.groupSize) {
const filesToDelete = group.files.slice(0, group.files.length - group.groupSize); const filesToDelete = group.files.slice(0, group.files.length - group.groupSize);
filesToDelete.forEach(file => { filesToDelete.forEach(file => {
console.log(new Date().toISOString() + " Deleting file: ", file); console.log(new Date().toISOString() + " Deleting file: ", file);
fs.unlinkSync(uploadPath + "/" + file); //fs.unlinkSync(uploadPath + "/" + file);
}); });
} }
}); });
@ -104,6 +109,13 @@ app.get('/state', (req, res) => {
res.send(groups); res.send(groups);
}); });
app.get('/clean', (req, res) => {
cleanUp();
res.setHeader('Content-Type', 'application/json');
res.send('Clean up done!');
});
app.get('/', (req, res) => { app.get('/', (req, res) => {
const files = []; const files = [];
fs.readdirSync(uploadPath).forEach(file => { fs.readdirSync(uploadPath).forEach(file => {

17
package-lock.json generated
View File

@ -1,14 +1,15 @@
{ {
"name": "simple-http-file-api", "name": "simple-http-file-api",
"version": "1.0.0", "version": "1.1.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "simple-http-file-api", "name": "simple-http-file-api",
"version": "1.0.0", "version": "1.1.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"dotenv": "^16.4.7",
"express": "^4.21.2", "express": "^4.21.2",
"express-fileupload": "^1.5.1", "express-fileupload": "^1.5.1",
"mime": "^4.0.6" "mime": "^4.0.6"
@ -170,6 +171,18 @@
"npm": "1.2.8000 || >= 1.4.16" "npm": "1.2.8000 || >= 1.4.16"
} }
}, },
"node_modules/dotenv": {
"version": "16.4.7",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.7.tgz",
"integrity": "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==",
"license": "BSD-2-Clause",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://dotenvx.com"
}
},
"node_modules/dunder-proto": { "node_modules/dunder-proto": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",

View File

@ -1,15 +1,18 @@
{ {
"name": "simple-http-file-api", "name": "simple-http-file-api",
"version": "1.0.0", "version": "1.1.0",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "node index.js", "start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1",
"docker.build.image": "bash ./build.docker.image.sh",
"docker.push.image": "bash ./push.docker.image.sh"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"description": "", "description": "",
"dependencies": { "dependencies": {
"dotenv": "^16.4.7",
"express": "^4.21.2", "express": "^4.21.2",
"express-fileupload": "^1.5.1", "express-fileupload": "^1.5.1",
"mime": "^4.0.6" "mime": "^4.0.6"

7
push.docker.image.sh Normal file
View File

@ -0,0 +1,7 @@
#!/bin/bash
# Get the current version from package.json
VERSION=$(jq -r '.version' package.json)
# Build the Docker image with the updated version from package.json
docker push 172.22.102.100:40016/team-mmkb/simple-http-upload:$VERSION