ci(builds): added better logic to building

This commit is contained in:
2025-07-12 14:31:15 -05:00
parent eed01197c0
commit 320968f994
8 changed files with 265 additions and 45 deletions

3
.gitignore vendored
View File

@@ -3,7 +3,7 @@ docker-compose.yml
frontend/.tanstack/
frontend/.output/
frontend/.nitro/
scripts/releases/
releases/
# ---> Go
# If you prefer the allow list template instead of the deny list, see community template:
@@ -182,3 +182,4 @@ dist
test-results/
backend/go.sum
BUILD_NUMBER

12
CHANGELOG.md Normal file
View File

@@ -0,0 +1,12 @@
## 1.0.0 (2025-07-12)
* fix(releases): spawn fixes to overcome npx and powershell ([eed0119](https://git.tuffraid.net/cowch/logistics_support_tool/commits/eed0119))
* refactor(releases): refactoring to get the actual release and changelog to show ([a20d377](https://git.tuffraid.net/cowch/logistics_support_tool/commits/a20d377))
* ci(package.json): missing scripts for committing ([0b17faf](https://git.tuffraid.net/cowch/logistics_support_tool/commits/0b17faf))
* ci(releases): added in release it to add to the build process ([1b07f56](https://git.tuffraid.net/cowch/logistics_support_tool/commits/1b07f56))
* feat(starter): intial starter release ([b370cb1](https://git.tuffraid.net/cowch/logistics_support_tool/commits/b370cb1))
* Initial commit ([e4d3fc9](https://git.tuffraid.net/cowch/logistics_support_tool/commits/e4d3fc9))
* Update README.md ([e081c8f](https://git.tuffraid.net/cowch/logistics_support_tool/commits/e081c8f))

View File

@@ -1 +0,0 @@
v0.0.1-alpha-1

View File

@@ -1,16 +1,17 @@
{
"name": "logistics_support_tool",
"version": "1.0.0",
"version": "0.0.1.alpha.1",
"description": "This is the new logisitcs support tool",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"backend": "cd backend && go run .",
"build":"powershell -File ./scripts/build.ps1",
"docker:front": "docker build -t logistics_support_tool:frontend-latest -f frontend/Dockerfile ./frontend",
"docker:back": "docker build -t logistics_support_tool:backend-latest -f backend/Dockerfile ./backend",
"docker": "powershell -File ./scripts/dockerBuild.ps1",
"release:createZip": "powershell -File ./scripts/release.ps1",
"release:gitea": "VERSION=$(cat VERSION) node ./scripts/create-gitea-release.js",
"release:gitea": "node ./scripts/create-gitea-release.js",
"commit": "cz"
},
"repository": {

View File

@@ -20,5 +20,10 @@
"gitea": {
"host": "https://${GITEA_URL}",
"tokenRef": "GITEA_TOKEN"
},
"increment": {
"prerelease": {
"name": "alpha"
}
}
}

172
scripts/build.ps1 Normal file
View File

@@ -0,0 +1,172 @@
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
$rootDir = Join-Path $scriptDir ".."
$releaseScript = Join-Path $scriptDir "release.ps1"
$packageJsonPath = Join-Path $rootDir "package.json"
# Path to BUILD_NUMBER file, assuming root is one level up from scripts
$buildNumberFile = Join-Path $scriptDir "..\BUILD_NUMBER"
$envFile = Join-Path $rootDir ".env"
if (Test-Path $envFile) {
Write-Host "Loading environment variables from $envFile"
Get-Content $envFile | ForEach-Object {
if ($_ -match "^\s*([^#][^=]*)=(.*)$") {
$name = $matches[1].Trim()
$value = $matches[2].Trim()
[System.Environment]::SetEnvironmentVariable($name, $value)
}
}
} else {
Write-Host ".env file not found at $envFile"
}
if (-not $env:BUILD_NAME) {
Write-Warning "BUILD_NAME environment variable is not set. Cannot create BUILD_NUMBER file."
exit 1
}
function Get-PackageVersion {
param (
[string]$packageJsonPath
)
if (-not (Test-Path $packageJsonPath)) {
Write-Warning "package.json not found at $packageJsonPath"
return $null
}
$jsonContent = Get-Content $packageJsonPath -Raw
try {
$json = $jsonContent | ConvertFrom-Json
return $json.version
}
catch {
Write-Warning "Failed to parse package.json: $_"
return $null
}
}
$version = Get-PackageVersion -packageJsonPath $packageJsonPath
Write-Host "Project version from package.json is: $version"
$buildNumberFile = Join-Path $rootDir "BUILD_NUMBER"
if (-not (Test-Path $buildNumberFile)) {
$initialBuildValue = "1-$($env:BUILD_NAME)"
Write-Host "BUILD_NUMBER file not found. Creating with value: $initialBuildValue"
Set-Content -Path $buildNumberFile -Value $initialBuildValue
} else {
Write-Host "BUILD_NUMBER file exists at $buildNumberFile, and will not be created"
}
function Update-BuildNumber {
if (-not (Test-Path $buildNumberFile)) {
Write-Warning "BUILD_NUMBER file not found at $buildNumberFile"
return $null
}
$current = Get-Content $buildNumberFile -Raw
$current = $current.Trim()
if ($current -match '^(\d+)-(.*)$') {
$number = [int]$matches[1]
$name = $matches[2]
$newNumber = $number + 1
$newBuildNumber = "$newNumber-$name"
Set-Content -Path $buildNumberFile -Value $newBuildNumber
Write-Host "Build number updated from $current to $newBuildNumber"
return $newBuildNumber
} else {
Write-Warning "BUILD_NUMBER file content '$current' is not in the expected 'number-name' format."
return $null
}
}
while ($true) {
Write-Host "Select build option:"
Write-Host "1) Backend"
Write-Host "2) Frontend"
Write-Host "3) All"
Write-Host "4) Exit"
$choice = Read-Host "Enter your choice (1-4)"
switch ($choice) {
'1' {
Push-Location $rootDir/backend
Write-Host "Building the app"
go build -ldflags "-X main.version=$($version)-$($initialBuildValue)" -o lst_backend.exe ./main.go
if ($LASTEXITCODE -ne 0) {
Write-Warning "Backend build failed!"
Pop-Location
break
}
Write-Host "Backend build finished successfully."
Pop-Location
npm run release:createZip
Update-BuildNumber
break
}
'2' {
Push-Location $rootDir/frontend
Write-Host "Building the frontend"
npm run build
if ($LASTEXITCODE -ne 0) {
Write-Warning "Fronend build failed!"
Pop-Location
break
}
Write-Host "Fronend build finished successfully."
Pop-Location
npm run release:createZip
Update-BuildNumber
break
}
'3' {
Push-Location $rootDir/backend
Write-Host "Building the app"
go build -ldflags "-X main.version=$($version)-$($initialBuildValue)" -o lst_backend.exe ./main.go
if ($LASTEXITCODE -ne 0) {
Write-Warning "Backend build failed!"
Pop-Location
break
}
Write-Host "Backend build finished successfully."
Pop-Location
Push-Location $rootDir/frontend
Write-Host "Building the frontend"
npm run build
if ($LASTEXITCODE -ne 0) {
Write-Warning "Fronend build failed!"
Pop-Location
break
}
Write-Host "Fronend build finished successfully."
Pop-Location
npm run release:createZip
Update-BuildNumber
break
}
'4' {
Write-Host "Exiting script."
exit 0
}
default {
Write-Warning "Invalid choice, please enter 1, 2, 3, or 4."
}
}
}

View File

@@ -2,22 +2,42 @@ import fs from 'fs-extra';
import { spawnSync } from 'child_process';
import fetch from 'node-fetch';
import dotenv from 'dotenv';
dotenv.config({ path: './.env' }); // or specify your env file path here
// Load environment variables for convenience (or use dotenv if you like)
dotenv.config({ path: './.env' });
// Load package.json version
const pkg = await fs.readJson('package.json');
const version = pkg.version;
if (!version) {
console.error('Version not found in package.json');
process.exit(1);
}
// Load build number from BUILD_NUMBER file
let buildNumber = '0';
try {
buildNumber = (await fs.readFile('BUILD_NUMBER', 'utf8')).trim();
if (!buildNumber) buildNumber = '0';
} catch {
console.warn('BUILD_NUMBER file not found, defaulting to 0');
}
// Compose full version string with build number
const fullVersion = `${version}.${buildNumber}`;
const {
GITEA_URL,
GITEA_USERNAME,
GITEA_REPO,
GITEA_TOKEN,
VERSION,
GITEA_TOKEN
} = process.env;
if (!GITEA_URL || !GITEA_USERNAME || !GITEA_REPO || !GITEA_TOKEN || !VERSION) {
console.error('Missing required environment variables!');
if (!GITEA_URL || !GITEA_USERNAME || !GITEA_REPO || !GITEA_TOKEN) {
console.error('Missing required environment variables (GITEA_URL, GITEA_USERNAME, GITEA_REPO, GITEA_TOKEN)');
process.exit(1);
}
// 1) Generate or update CHANGELOG.md for the version
// 1) Generate or update CHANGELOG.md using conventional-changelog CLI
console.log('Generating CHANGELOG.md...');
const result = spawnSync('npx', [
'conventional-changelog',
@@ -31,27 +51,26 @@ const result = spawnSync('npx', [
], { stdio: 'inherit', shell: true });
if (result.status !== 0) {
console.log(result)
console.error('Failed to generate changelog');
process.exit(1);
}
// 2) Read changelog content for the current version
const changelog = fs.readFileSync('CHANGELOG.md', 'utf8');
const regex = new RegExp(`## \\[${VERSION}\\][\\s\\S]*?(?=## \\[|$)`, 'm');
const changelog = await fs.readFile('CHANGELOG.md', 'utf8');
const regex = new RegExp(`## \\[${version}\\][\\s\\S]*?(?=## \\[|$)`, 'm');
const releaseNotes = changelog.match(regex)?.[0] || changelog;
console.log(`Release notes for v${VERSION}:\n`, releaseNotes);
console.log(`Release notes for v${fullVersion}:\n`, releaseNotes);
// 3) Create Gitea release
const createRelease = async () => {
const apiUrl = `https://${GITEA_URL}/api/v1/repos/${GITEA_USERNAME}/${GITEA_REPO}/releases`;
const releaseData = {
tag_name: `v${VERSION}`,
name: `v${VERSION}`,
tag_name: `v${fullVersion}`,
name: `v${fullVersion}`,
body: releaseNotes,
draft: false,
prerelease: false,
prerelease: true,
};
const response = await fetch(apiUrl, {
@@ -76,17 +95,16 @@ const createRelease = async () => {
// 4) Upload release zip asset
const uploadAsset = async (release) => {
const apiUrl = `https://${GITEA_URL}/api/v1/repos/${GITEA_USERNAME}/${GITEA_REPO}/releases/assets?tag=${release.tag_name}`;
const filePath = `releases/release-${VERSION}.zip`;
const filePath = `releases/release-${fullVersion}.zip`;
if (!fs.existsSync(filePath)) {
if (!await fs.pathExists(filePath)) {
console.warn(`Zip file not found: ${filePath}. Skipping asset upload.`);
return;
}
// Use form-data and fetch for uploading binary
const FormData = (await import('form-data')).default;
const form = new FormData();
form.append('name', `release-${VERSION}.zip`);
form.append('name', `release-${fullVersion}.zip`);
form.append('attachment', fs.createReadStream(filePath));
const response = await fetch(apiUrl, {

View File

@@ -1,33 +1,45 @@
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$versionFile = Join-Path $scriptDir "..\VERSION"
$rootDir = Join-Path $scriptDir ".."
$packageJsonPath = Join-Path $rootDir "package.json"
$env:GOOS = "windows"
$env:GOARCH = "amd64"
$keepReleases = 10
if (Test-Path $versionFile) {
$version = Get-Content $versionFile -Raw | ForEach-Object { $_.Trim() }
Write-Host "Project version: $version"
} else {
Write-Host "VERSION file not found at $versionFile"
function Get-PackageVersion {
param (
[string]$packageJsonPath
)
if (-not (Test-Path $packageJsonPath)) {
Write-Warning "package.json not found at $packageJsonPath"
return $null
}
Write-Host "Building release version: $version"
$jsonContent = Get-Content $packageJsonPath -Raw
try {
$json = $jsonContent | ConvertFrom-Json
return $json.version
}
catch {
Write-Warning "Failed to parse package.json: $_"
return $null
}
}
Write-Host "`nBuilding Go backend..."
Push-Location ./backend
$version = Get-PackageVersion -packageJsonPath $packageJsonPath
Write-Host "Project version from package.json is: $version"
$buildNumberFile = Join-Path $rootDir "BUILD_NUMBER"
go build -ldflags "-X main.version=$version" -o lst_backend.exe ./main.go
Pop-Location
if (Test-Path $buildNumberFile) {
$buildNumber = Get-Content $buildNumberFile -Raw
$buildNumber = $buildNumber.Trim()
Write-Host "Current build number: $buildNumber"
} else {
Write-Warning "BUILD_NUMBER file not found at $buildNumberFile"
}
Write-Host "`nBuilding frontend..."
Push-Location ./frontend
npm install
npm run build
Pop-Location
$releaseFolder = Join-Path $scriptDir "releases"
$zipName = "release-$version.zip"
$releaseFolder = Join-Path $rootDir "releases"
$zipName = "release-v$version-$buildNumber.zip"
$zipPath = Join-Path $releaseFolder $zipName
@@ -60,7 +72,7 @@ $filesToInclude = @(
"frontend\.tanstack",
"frontend\.output",
"frontend\public",
"VERSION"
"package.json"
)
Compress-Archive -Path $filesToInclude -DestinationPath $zipPath