diff --git a/Jenkinsfile b/Jenkinsfile index 170777e..40dfe1f 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,267 +2,50 @@ agent any environment { + // Map the branch name from the webhook or manual trigger + // If BRANCH_NAME is not set, we use the parameter + TARGET_BRANCH = "${env.BRANCH_NAME ?: params.branch}" GIT_REPO = 'ssh://git@git.jasonpoage.vpn:29418/jason/express-blog.git' - DEPLOY_BASE = '/srv/jasonpoage.com' - YARN_ENABLE_GLOBAL_CACHE = 'false' - YARN_CACHE_FOLDER = '/var/cache/jenkins/yarn' CREDENTIALS_ID = '08a57452-477d-4aa6-86c6-242553660b3f' } - - - options { - timestamps() - } - - parameters { - string(name: 'branch', defaultValue: 'refs/heads/main', description: 'Branch ref from webhook') - string(name: 'oldrev', defaultValue: '', description: 'old rev') - string(name: 'newrev', defaultValue: '', description: 'new rev') - - booleanParam(name: 'SKIP_TESTS', defaultValue: true, description: 'Skip all testing') + string(name: 'branch', defaultValue: 'refs/heads/release', description: 'Deployment branch') + booleanParam(name: 'SKIP_TESTS', defaultValue: false, description: 'Skip integration tests') } + stages { - stage('Init') { - steps { - script { - if (params.branch?.startsWith("refs/heads/")) { - env.DEPLOY_BRANCH = params.branch.replaceFirst(/^refs\/heads\//, '') - } else { - error "Invalid branch ref: '${params.branch}'" - } + stage('Setup Runner') { + steps { + checkout scm + sh """ + python3 -m venv .venv + ./.venv/bin/pip install -r requirements.txt + """ + } + } - echo "==== DEBUG: Branch Param ====" - echo "params.branch: '${params.branch}'" - echo "env.DEPLOY_BRANCH: '${env.DEPLOY_BRANCH}'" - - env.TIMESTAMP = sh(script: "date +%Y%m%d-%H%M%S", returnStdout: true).trim() - env.LOG_DIR = "${env.DEPLOY_BASE}/deployments/logs" - env.SERVER_LOG_FILE = "${env.LOG_DIR}/server/server-${env.TIMESTAMP}.log" - env.TEST_LOGS_FILE = "${env.LOG_DIR}/test-results/test-" - env.BUILD_DIR = "${env.WORKSPACE}/build" - env.PIDFILE = "${BUILD_DIR}/test.pid" - env.ENV_FILE = "${env.DEPLOY_BASE}/env/${env.DEPLOY_BRANCH}.env" - env.SERVICE_NAME = "express-blog@${env.DEPLOY_BRANCH}.service" - env.DEPLOY_PATH = "${env.DEPLOY_BASE}/deployments/blog-${env.DEPLOY_BRANCH}" - - - if (params.oldrev?.trim() && params.newrev?.trim()) { - env.OLD_REV = params.oldrev - env.NEW_REV = params.newrev - } else { - env.OLD_REV = sh(script: 'git rev-parse HEAD~1', returnStdout: true).trim() - env.NEW_REV = sh(script: 'git rev-parse HEAD', returnStdout: true).trim() - } - - echo "==== DEBUG: Revisions ====" - echo "params.oldrev: '${params.oldrev}'" - echo "params.newrev: '${params.newrev}'" - echo "Old revision: ${env.OLD_REV}" - echo "New revision: ${env.NEW_REV}" - sh "mkdir -p '${env.LOG_DIR}/server' '${env.LOG_DIR}/test-results'" - } - } - } - - stage('Checkout') { - steps { - checkout([$class: 'GitSCM', - branches: [[name: "*/${env.DEPLOY_BRANCH}"]], - userRemoteConfigs: [[ - url: env.GIT_REPO, - credentialsId: env.CREDENTIALS_ID - ]] - ]) - } - } - - - stage('Validate Branch') { - steps { - script { - def allowed = ['testing', 'staging', 'main', 'production'] - if (!allowed.contains(env.DEPLOY_BRANCH)) { - error "Branch '${env.DEPLOY_BRANCH}' is not allowed for deployment." - } - } - } - } - - stage('Clone to Build Dir') { - steps { - script { - sh "git clone --branch '${env.DEPLOY_BRANCH}' '${env.GIT_REPO}' '${env.BUILD_DIR}'" - } - } - } - - stage('Copy and Source .env') { - steps { - script { - sh "ln -s '${ENV_FILE}' '${env.BUILD_DIR}/.env'" - - def envVars = sh( - script: "set -a && . '${env.BUILD_DIR}/.env' && env | grep -E '^(SERVER_SCHEMA|SERVER_DOMAIN)='", - returnStdout: true - ).trim().split("\n") - - def parsedEnv = [:] - envVars.each { - def (key, value) = it.tokenize('=') - parsedEnv[key] = value - } - env.SERVER_SCHEMA = parsedEnv['SERVER_SCHEMA'] - env.SERVER_DOMAIN = parsedEnv['SERVER_DOMAIN'] - } - } - } - - stage('Build') { - steps { - dir("${BUILD_DIR}") { - sh """ - git submodule update --init --recursive - yarn - yarn combine:css - """ - } - } - } - - stage('Start Application for Test') { - steps { - script { - if ( !params.SKIP_TESTS ) { - dir(BUILD_DIR) { - sh """ - sudo systemctl stop ${env.SERVICE_NAME} || true - corepack enable - nohup yarn run prod >> '${env.SERVER_LOG_FILE}' 2>&1 & - echo \$! > '${env.PIDFILE}' - """ - } - } - } - } - } - - stage('Wait for Service Readiness') { - steps { - script { - if ( !params.SKIP_TESTS ) { - def timeout = 30 - def elapsed = 0 - def success = false - while (elapsed < timeout) { - def result = sh(script: "curl --max-time 2 --silent --fail '\${SERVER_SCHEMA}://\${SERVER_DOMAIN}/health -I' > /dev/null || true", returnStatus: true) - if (result == 0) { - success = true - break - } - sleep 1 - elapsed += 1 - } - if (!success) { - sh "cat '${env.SERVER_LOG_FILE}'" - error "Service did not become available within ${timeout}s." - } - } - } - } - } - - stage('Run Tests') { - steps { - script { - if ( !params.SKIP_TESTS ) { - def testStatus = sh(script: "cd '${env.BUILD_DIR}' && npm run test:postreceive", returnStatus: true) - archiveArtifacts artifacts: "${env.TEST_LOGS_FILE}*", onlyIfSuccessful: false - if (testStatus != 0) { - sh """ - kill \$(cat '${env.PIDFILE}') || true - cat '${env.SERVER_LOG_FILE}' - """ - error "Tests failed for branch ${env.DEPLOY_BRANCH}" - } - } - } - } - } - - stage('Stop Test App') { - steps { - script { - if ( !params.SKIP_TESTS ) { - sh "kill \$(cat '${env.PIDFILE}') || true" - } - } - } - } - - stage('Deploy') { - steps { + stage('Execute Deployment') { + steps { script { - // 1. Create the new release directory - def releaseDir = "${env.DEPLOY_BASE}/releases/blog-${env.DEPLOY_BRANCH}-${env.TIMESTAMP}" - sh "mkdir -p ${releaseDir}" - - // 2. Sync the finished build to the release directory - echo "Deploying build to ${releaseDir}" - sh "rsync -a --delete '${env.BUILD_DIR}/' '${releaseDir}/'" - - // 3. Atomically flip the symlink - // We use 'ln -sfn' to overwrite the existing link to the new path - sh "ln -sfn '${releaseDir}' '${env.DEPLOY_PATH}'" - - // 4. Cleanup old releases (Keep only last 5) - dir("${env.DEPLOY_BASE}/releases") { - sh "ls -1t | grep 'blog-${env.DEPLOY_BRANCH}' | tail -n +6 | xargs rm -rf || true" - } - } - } - } - - stage('Restart Service') { - steps { - script { - sh "sudo systemctl restart ${env.SERVICE_NAME}" - } - } - } - stage('Verify Service') { - steps { - script { - def timeout = 30 - def elapsed = 0 - def success = false - while (elapsed < timeout) { - def result = sh(script: "curl --max-time 2 --silent --fail '\${env.SERVER_SCHEMA}://\${env.SERVER_DOMAIN}/health -I' > /dev/null || true", returnStatus: true) - if (result == 0) { - success = true - break - } - sleep 1 - elapsed += 1 - } - if (!success) { - sh "cat '${env.SERVER_LOG_FILE}'" - error "Service did not become available within ${timeout}s." - } - } - } - } + def skipFlag = params.SKIP_TESTS ? "--skip-tests" : "" + // Call the python binary inside the venv directly + sh "./.venv/bin/python3 ./deployment --config /srv/jasonpoage.com/deployment.lua --branch ${env.TARGET_BRANCH} ${skipFlag}" + } + } + } } + post { + always { + // Clean up the build directory in the workspace to prevent the "already exists" error + sh "rm -rf build/" + } success { - echo "Deployment of ${env.DEPLOY_BRANCH} completed successfully." + echo "Deployment of ${env.TARGET_BRANCH} successful." } failure { - echo "Deployment of ${env.DEPLOY_BRANCH} failed." - } - cleanup { - sh "rm -rf '${env.BUILD_DIR}' || true" + echo "Deployment of ${env.TARGET_BRANCH} failed. Check Python logs above." } } } diff --git a/deployment.lua b/deployment.lua new file mode 100644 index 0000000..c7330e9 --- /dev/null +++ b/deployment.lua @@ -0,0 +1,32 @@ +local app_name = "Express Blog" +local repo = "ssh://git@git.jasonpoage.vpn:29418/jason/express-blog.git" +local config_dir = "/srv/jasonpoage.com/env/" +-- 1. Static Lookups +local base = "/srv/jasonpoage.com" +local deployments = base .. "/deployments" + +function get_config(env_key) + -- Specific folder name for this environment + local instance_name = "blog-" .. env_key + local deploy_link = deployments .. "/" .. instance_name + + return { + deploy_link = deploy_link, + config_file = config_dir .. env_key .. ".toml", + service_name = "expressjs-blog@" .. env_key .. ".service", + -- Tracking which deployments were successful + get_release_dir = function(timestamp) + return deploy_link .. "-" .. timestamp + end, + count = (env_key == "release") and 5 or 1, + } +end + +return { + app_name = app_name, + timestamp_format = "%Y%m%d-%H%M%S", + repo = repo, + base = base, + release = get_config("release"), + testing = get_config("testing"), +} diff --git a/deployment/README b/deployment/README new file mode 100644 index 0000000..32a7bf0 --- /dev/null +++ b/deployment/README @@ -0,0 +1,2 @@ +#Entry point: +python . --config ../deployment.lua --dry-run --branch refs/heads/dev diff --git a/deployment/core/task_runner.py b/deployment/core/task_runner.py index 09a77ce..5ddf4ce 100755 --- a/deployment/core/task_runner.py +++ b/deployment/core/task_runner.py @@ -48,7 +48,7 @@ CheckNix, VerifySystemDependencies, GetDeploymentConfig, - VerifyConfigExists, + LoadServerConfig, EnsureBuildPaths, YarnBuild, TestRunner, diff --git a/deployment/core/tasks.py b/deployment/core/tasks.py index 2b5c963..ab83a00 100644 --- a/deployment/core/tasks.py +++ b/deployment/core/tasks.py @@ -21,38 +21,40 @@ def _run(self): # 1. Load Lua lua = LuaRuntime(unpack_returned_tuples=True) - with open(self.get_arg("config"), "r") as f: + config_path = self.get_arg("config") + + with open(config_path, "r") as f: module = lua.execute(f.read()) - # 2. Call the factory - target_env = self.get_arg("branch").split("/")[-1] # e.g., 'main' or 'testing' - print(target_env) + # 2. Determine environment key from branch + # Mapping 'main' to 'release' as per lua schema + branch = self.get_arg("branch").split("/")[-1] + target_env = "release" if branch == "main" else branch - # 3. Hydrate self.env - config = module.get_config(target_env) + if target_env not in module: + self.fail(f"Environment '{target_env}' not defined in {config_path}") - self.env.build_dir = Path(config.paths.build) - self.env.release_dir = Path(config.paths.release_dir) - self.env.deploy_path = Path(config.paths.deploy_link) - self.env.service = config.systemd.service_name - self.env.config_file_source = config.paths.config_file - self.env.meta = config.meta + # 3. Extract environment specific sub-table + cfg = module[target_env] - self.print(f"✅ Context hydrated for {config.meta.app_name}:{target_env}") + # 4. Hydrate self.env + self.env.lua_cfg = cfg # Store the lua object for functional calls later + self.env.app_name = module.app_name + self.env.repo = module.repo + self.env.timestamp_format = module.timestamp_format + + self.env.deploy_path = Path(cfg.deploy_link) + self.env.service_name = cfg.service_name + self.env.config_file_source = Path(cfg.config_file) + self.env.retention_count = cfg.count + self.env.deploy_branch = branch + + self.print(f"✅ Context hydrated for {self.env.app_name}:{target_env}") + # self.env.build_dir = Path(config.paths.build) return True class LoadServerConfig(SuiteTask): - """Fails the pipeline if the required TOML config is missing from the host""" - - _stage = Stage.BOOTSTRAP - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.name = "Verify the server's toml configuration exists" - - -class VerifyConfigExists(SuiteTask): """Verifies TOML existence and hydrates the environment with health check URI components""" _stage = Stage.BOOTSTRAP @@ -114,13 +116,23 @@ self.name = "Running Yarn build process" def _run(self): - build_dir = self.env.build_dir - self.sh( - f"git clone --branch {self.env.deploy_branch} {self.get_arg('repo')} {build_dir}" + # Use a temporary build directory with -BUILDING suffix + # This is finalized in AtomicDeploy + timestamp = time.strftime(self.env.timestamp_format) + self.env.release_dir = Path(self.env.lua_cfg.get_release_dir(timestamp)) + self.env.build_dir = self.env.release_dir.with_name( + self.env.release_dir.name + "-BUILDING" ) - self.sh("git submodule update --init --recursive", cwd=build_dir) - self.sh("yarn install", cwd=build_dir) - self.sh("yarn combine:css", cwd=build_dir) + + self.print(f" [BUILD] Target: {self.env.build_dir}") + + self.sh( + f"git clone --branch {self.env.deploy_branch} {self.env.repo} {self.env.build_dir}" + ) + self.sh("git submodule update --init --recursive", cwd=self.env.build_dir) + self.sh("yarn install", cwd=self.env.build_dir) + self.sh("yarn combine:css", cwd=self.env.build_dir) + return True class AtomicDeploy(SuiteTask): @@ -135,11 +147,19 @@ self.name = "Executing atomic symlink swap" def _run(self): - env = self.env - self.sh(f"mkdir -p {env.release_dir}") - self.sh(f"rsync -a --delete {env.build_dir}/ {env.release_dir}/") - self.sh(f"ln -sfn {env.release_dir} {env.deploy_path}") - self.sh(f"sudo systemctl restart {env.service_name}") + # 1. Finalize the directory name (remove -BUILDING) + self.sh(f"mv {self.env.build_dir} {self.env.release_dir}") + + # 2. Atomic Symlink Swap + temp_link = self.env.deploy_path.with_name(self.env.deploy_path.name + "_tmp") + self.sh(f"ln -sfn {self.env.release_dir} {temp_link}") + self.sh(f"mv -Tf {temp_link} {self.env.deploy_path}") + + # 3. Restart Service + self.sh(f"sudo systemctl restart {self.env.service_name}") + + self.print(f"🚀 Deployed to {self.env.deploy_path} -> {self.env.release_dir}") + return True class HealthCheck(SuiteTask): diff --git a/deployment/lib/types.py b/deployment/lib/types.py index 1fe95b9..ddc4c89 100644 --- a/deployment/lib/types.py +++ b/deployment/lib/types.py @@ -1,5 +1,4 @@ import os -import time from enum import Enum from pathlib import Path @@ -22,7 +21,6 @@ service_name: str release_dir: Path test_endpoint_uri: str - meta: dict = dict() pidfile: Path = Path() def __init__(self, timestamp_format: str | None = None): @@ -33,12 +31,12 @@ self.build_dir: Path = Path() self.service_name: str = "" self.release_dir: Path = Path() - self.server_schema: str = "http" - self.server_domain: str = "localhost" + self.server_schema = "http" + self.server_address = "localhost" + self.pidfile = Path("/tmp/hexascript_test.pid") self.root_dir = os.getcwd() if timestamp_format is not None: self.timestamp_format = timestamp_format - self.timestamp = time.strftime(self.timestamp_format) self.workspace = Path(os.getenv("WORKSPACE", self.root_dir)) self.build_dir = self.workspace / "build"