\chapter{Continuous Integration with Jenkins} \label{chap:jenkins-ci} \section{Overview} This chapter documents the Jenkins continuous integration system deployed for the SSW590 startup product. Jenkins integrates with Gitea to automatically validate source code changes. \section{Jenkins Deployment and Persistence} Jenkins was deployed as a Docker container on the team's DigitalOcean Droplet running Ubuntu 24.04. The deployment uses the custom image \texttt{jenkins-node:2.580.1}, which provides the Jenkins automation server and the Node.js tooling required for the startup product's continuous integration pipeline. The Docker Compose project is located at \texttt{/opt/jenkins}. The Jenkins container uses the \texttt{jenkins\_jenkins\_internal} bridge network. \subsection{Persistent Storage} Jenkins configuration, installed plugins, job definitions, credentials, and build history are stored in the persistent host directory \texttt{/opt/jenkins/jenkins\_home}, mounted inside the container at \texttt{/var/jenkins\_home}. This bind mount separates Jenkins application data from the container's writable layer so that the data remains available when the container is replaced, provided the host directory is preserved. \subsection{Resource Limits} The Droplet has limited system resources. Docker Compose therefore restricts Jenkins to 384 MiB of container memory and 768 MiB of combined memory and swap. The Java virtual machine is configured with an initial heap of 64 MiB, a maximum heap of 192 MiB, a maximum metaspace of 128 MiB, and the serial garbage collector. These limits reduce resource consumption but do not guarantee that every build will succeed under memory pressure. \subsection{Network Exposure and Restart Policy} The Jenkins HTTP service listens on container port 8080, published only to \texttt{127.0.0.1:8080} on the Droplet. This prevents direct access to Jenkins through the Droplet's public IP address on port 8080. The container uses the restart policy \texttt{no}. Consequently, an administrator must explicitly start Jenkins after a container stop or host restart. \subsection{Deployment Verification} The deployment was verified using the following commands on the DigitalOcean Droplet: \begin{verbatim} cd /opt/jenkins docker compose config docker ps --filter name=jenkins \end{verbatim} The Docker inspection confirmed that the container was running with image \texttt{jenkins-node:2.580.1}, persistent storage, the private bridge network, and localhost-only HTTP port publishing. \section{HTTPS and Reverse Proxy Configuration} The Jenkins continuous integration service is published through the hostname \url{https://ci.nageshwaran.dev}. The deployment uses Caddy as its reverse proxy, alongside the existing Gitea service at \url{https://git.nageshwaran.dev}. \subsection{Caddy Reverse Proxy Rules} The reverse proxy configuration is stored in \texttt{/opt/gitea/Caddyfile} on the DigitalOcean Droplet. The verified configuration contains the following routes: \begin{verbatim} git.nageshwaran.dev { reverse_proxy gitea:3000 } ci.nageshwaran.dev { reverse_proxy jenkins:8080 } \end{verbatim} Caddy routes requests according to their requested hostname. Traffic for the Gitea hostname is forwarded to container port 3000, while requests for the Jenkins hostname are forwarded to container port 8080. \subsection{Network Isolation} The Jenkins Docker Compose configuration publishes port 8080 only on the Droplet loopback interface, using \texttt{127.0.0.1:8080:8080}. Caddy forwards traffic to the Jenkins service by its Docker service name, \texttt{jenkins}, on port 8080. This separates the public HTTPS entry point from the Jenkins application listener. The internal Jenkins port does not need to be published directly on the Droplet's public network interface. \subsection{HTTPS Operations} The Caddy configuration uses hostname-based site blocks, allowing Caddy to manage HTTPS for the configured domains when DNS, certificate issuance, and network connectivity are correctly configured. The verified configuration establishes the reverse proxy routes. Certificate renewal and external firewall behavior should be verified separately rather than inferred from the Caddyfile alone. \section{Gitea Integration and Webhooks} \subsection{Repository and Jenkins Server} The startup product source code is hosted on the team's self-managed Gitea instance at \url{https://git.nageshwaran.dev}. The repository belongs to the organization \texttt{SSW590-Startup} and is named \texttt{startup-product}. Jenkins is accessible through \url{https://ci.nageshwaran.dev}. The Jenkins Gitea integration was configured with a Gitea server entry named \texttt{SSW590-Gitea}. An API credential identified as \texttt{ssw590-gitea-token} is stored in the Jenkins credential store rather than in the version-controlled application source. \subsection{Multibranch Pipeline} The Jenkins multibranch pipeline is identified by \texttt{startup-product-gitea-ci}. The pipeline discovers repository branches and pull requests and executes the version-controlled \texttt{Jenkinsfile} associated with each build. The pipeline provides continuous integration checks for changes proposed to the protected main branch. \subsection{Webhook-Triggered Builds} Gitea webhooks notify Jenkins when relevant repository events occur, including updates to pull requests. This integration was tested during pull request \#8, titled ``Add Jenkins CI pipeline and automated Node.js tests.'' Jenkins executed builds after changes were pushed to the pull request, and their results were reported back to Gitea as commit status checks. A deliberately failing test produced a failed status. After the test was corrected, Jenkins reported a successful status, allowing the reviewed pull request to proceed through the merge workflow. \subsection{Integration Verification} The integration was verified through Jenkins build results, Gitea pull request status checks, and the successful Jenkins build of the main branch following the merge. The final main-branch build completed successfully with two passing automated tests and zero failures. \section{Version-Controlled Jenkins Pipeline} The continuous integration pipeline is defined in a \texttt{Jenkinsfile} stored at the root of the \texttt{startup-product} repository. Using a version-controlled Jenkinsfile ensures that pipeline changes are reviewed and tracked alongside the application source code. \subsection{Pipeline Execution Controls} The pipeline uses the Jenkins declarative pipeline syntax with \texttt{agent any}. The following execution controls are configured: \begin{itemize} \item Concurrent builds are disabled using \texttt{disableConcurrentBuilds()}. \item A five-minute timeout prevents indefinitely running builds. \item Build history is limited to the five most recent builds using \texttt{buildDiscarder}. \end{itemize} \subsection{Pipeline Stages} The Jenkinsfile defines four sequential stages: \begin{enumerate} \item \textbf{Checkout:} Retrieves the repository revision associated with the Jenkins build using \texttt{checkout scm}. \item \textbf{Verify Node.js:} Executes \texttt{node --version} and \texttt{npm --version} to verify that the required tools are available. \item \textbf{Syntax Check:} Changes into the \texttt{app} directory and executes \texttt{npm run check}. \item \textbf{Automated Tests:} Executes \texttt{npm test} from the \texttt{app} directory. \end{enumerate} \subsection{Node.js Project Scripts} The application defines its validation commands in \texttt{app/package.json}: \begin{verbatim} "scripts": { "test": "node --test", "check": "node --check src/health.js" } \end{verbatim} The syntax-check command uses Node.js to validate the JavaScript syntax of \texttt{src/health.js}. The test command uses the built-in Node.js test runner to execute the project's automated tests. \subsection{Build Result Reporting} The Jenkinsfile includes post-build handlers for successful and failed executions. On success, Jenkins prints: \begin{verbatim} CI PASSED: All checks completed successfully. \end{verbatim} On failure, Jenkins prints: \begin{verbatim} CI FAILED: Review the Jenkins console output. \end{verbatim} These messages supplement the Jenkins build status and console output. \section{Automated Tests and CI Verification} \subsection{Application Health-Check Function} The startup product includes a health-check function in \texttt{app/src/health.js}. The function \texttt{getHealthStatus} accepts a Boolean parameter representing database connectivity and returns an object containing the application status and database connection state. When database connectivity is true, the function returns \texttt{healthy} and \texttt{connected}. When connectivity is false, it returns \texttt{unhealthy} and \texttt{disconnected}. \subsection{Automated Unit Tests} The test file \texttt{app/test/health.test.js} uses the built-in Node.js test runner and strict assertions. Two automated test cases are implemented: \begin{enumerate} \item \textbf{Connected database:} Calling \texttt{getHealthStatus(true)} must return an object with status \texttt{healthy} and database state \texttt{connected}. \item \textbf{Disconnected database:} Calling \texttt{getHealthStatus(false)} must return an object with status \texttt{unhealthy} and database state \texttt{disconnected}. \end{enumerate} These tests validate the application's response to simulated database connectivity states. They do not establish connectivity to a live database server. \subsection{Jenkins Test Execution} The automated test stage runs the following command inside the application directory: \begin{verbatim} npm test \end{verbatim} The command invokes the Node.js test runner through the project's \texttt{package.json} configuration. During the verified successful Jenkins main-branch build, both automated tests passed with zero failures. The successful build also completed the Node.js verification and JavaScript syntax-check stages. \subsection{Continuous Integration Result} The Jenkins multibranch pipeline reported a successful main-branch build after pull request \#8 was reviewed and merged. The verified Jenkins main-branch build was build number \#1, which completed in approximately 33 seconds with two passing tests and zero failures. The build console is available at: \begin{quote} \url{https://ci.nageshwaran.dev/job/startup-product-gitea-ci/job/main/1/console} \end{quote} \section{Failed Build and Successful Recovery} \subsection{Controlled Failure Experiment} To demonstrate that the continuous integration pipeline detects incorrect application behavior, the team deliberately introduced an incorrect expected result into an automated health-check test. The change was pushed to the feature branch \texttt{feature/jenkins-ci}, associated with Gitea pull request \#8. The failing commit was identified by the abbreviated Git SHA \texttt{e1d9007}. The Gitea webhook triggered Jenkins to execute the pipeline against the updated pull request. Jenkins pull-request build \#2 failed because the automated test assertion did not match the application's actual result. The failed Jenkins status was reported to Gitea, demonstrating that the CI integration detected the deliberately introduced defect. \subsection{Corrective Commit} The team corrected the failing test expectation and pushed the repaired implementation to the same feature branch. The corrective commit was identified by the abbreviated Git SHA \texttt{e00142a}. The subsequent Jenkins pull-request build \#3 completed successfully. The pipeline executed the syntax check and automated tests, and Jenkins reported a successful result back to Gitea. \subsection{Failure and Recovery Results} The observed results are summarized below: \begin{center} \begin{tabular}{llll} \toprule Revision & Jenkins Build & Result & Purpose \\ \midrule \texttt{e1d9007} & PR-8 \#2 & FAILURE & Deliberate defect \\ \texttt{e00142a} & PR-8 \#3 & SUCCESS & Corrected test \\ \bottomrule \end{tabular} \end{center} This controlled failure-and-recovery experiment demonstrates that the Jenkins pipeline does not simply execute automatically; it can identify an incorrect test expectation and confirm that the correction restores the passing status. \section{Branch Protection and Required Status Checks} \subsection{Protected Main Branch} The Gitea repository was configured with branch protection for \texttt{main}. The purpose of this protection is to ensure that changes are validated by the Jenkins continuous integration pipeline before being merged into the main development branch. \subsection{Required Jenkins Status Check} The required CI status check was configured as: \begin{verbatim} startup-product-gitea-ci/pipeline/pr-main \end{verbatim} This status check connects Jenkins build results to the Gitea pull request merge workflow. A failing Jenkins build prevents the required status check from being satisfied. \subsection{Verification Using Pull Request 8} During pull request \#8, the deliberately failing test caused Jenkins PR build \#2 to report \texttt{FAILURE}. Gitea displayed the unsuccessful status, and the pull request could not satisfy the required CI check. After the corrective commit was pushed, Jenkins PR build \#3 reported \texttt{SUCCESS}. The required status check became successful, allowing the pull request to proceed through the review and merge workflow. This verifies that the required CI status check was enforced during the demonstrated workflow. \section{Pull Request Review and Merge} \subsection{Pull Request Creation} The Jenkins CI implementation was developed on the feature branch \texttt{feature/jenkins-ci}. The changes were submitted to Gitea through pull request \#8, titled: \begin{quote} Add Jenkins CI pipeline and automated Node.js tests \end{quote} The pull request proposed merging the feature branch into the protected \texttt{main} branch. The changes included the version-controlled Jenkinsfile, Node.js project configuration, application health-check implementation, and automated tests. \subsection{Continuous Integration Validation} Jenkins automatically validated changes associated with pull request \#8. An intentionally incorrect test expectation caused Jenkins pull-request build \#2 to fail. After the corrective commit was pushed, Jenkins pull-request build \#3 completed successfully. Gitea displayed the successful required status check: \begin{verbatim} startup-product-gitea-ci/pipeline/pr-main \end{verbatim} \subsection{Reviewer Approval} The pull request was reviewed by the team member using the Gitea account \texttt{shiv}. The reviewer inspected the five changed files and submitted an approval through Gitea's pull request review interface. The approval was recorded before the pull request was merged. \subsection{Merge into Main} After Jenkins reported a successful required status check and the reviewer approved the changes, pull request \#8 was merged into the \texttt{main} branch. The resulting merge commit was: \begin{verbatim} b2cdc5cad4d522a36160f738c0befbb6d86cbda0 \end{verbatim} \subsection{Post-Merge Verification} After the merge, the Jenkins multibranch pipeline detected the updated \texttt{main} branch and executed a new build. Jenkins main-branch build \#1 completed successfully in approximately 33 seconds. The console output confirmed: \begin{itemize} \item Node.js and npm verification completed. \item JavaScript syntax checking succeeded. \item Both automated health tests passed. \item Zero automated test failures were reported. \item The final pipeline result was \texttt{SUCCESS}. \end{itemize} This confirms that the merged source code passed the automated Jenkins validation on the main branch. \section{Security, Credentials, and Trust Boundaries} \subsection{Credential Management} Jenkins integrates with the self-hosted Gitea server using an API credential stored in the Jenkins credential store. The credential identifier is: \begin{verbatim} ssw590-gitea-token \end{verbatim} The API token value is not included in the Jenkinsfile, application source code, or this manual. Credentials should be rotated if exposed, and replacement values must be updated in Jenkins. \subsection{Public and Private Network Boundaries} The public Jenkins endpoint is: \begin{verbatim} https://ci.nageshwaran.dev \end{verbatim} Caddy routes HTTPS requests to the Jenkins container using the internal Docker service address \texttt{jenkins:8080}. Jenkins publishes its application port only on the Droplet's loopback interface: \begin{verbatim} 127.0.0.1:8080:8080 \end{verbatim} This avoids publishing the Jenkins application port directly on the Droplet's public interface. \subsection{Repository and Pipeline Trust} The Jenkinsfile is stored in the Gitea repository and executed by Jenkins during continuous integration builds. Changes to the Jenkinsfile can affect the commands executed by the Jenkins build agent. Therefore, repository write access, pull request review, and branch protection are important security controls. The demonstrated workflow used a protected main branch, a required Jenkins status check, and a separate reviewer approval before merging pull request \#8. \subsection{Resource and Operational Boundaries} The Jenkins container uses a 384 MiB memory limit and a maximum Java heap of 192 MiB. These resource limits help control consumption on the small DigitalOcean Droplet. The container restart policy is \texttt{no}, so Jenkins requires an explicit start after a container stop or host restart. The persistent Jenkins home directory is stored at: \begin{verbatim} /opt/jenkins/jenkins_home \end{verbatim} Administrators must protect this directory because it contains Jenkins configuration and sensitive application state. \subsection{Security Limitations} The verified configuration demonstrates localhost-only port publishing, Docker network separation, credential-store usage, and branch protection. These controls do not by themselves prove complete host hardening or eliminate risks from untrusted pipeline code. API token rotation, firewall verification, software updates, and access-control reviews remain important operational responsibilities. \section{Screenshot Evidence} The following figures record the observed Jenkins and Gitea CI workflow. \begin{figure}[H] \centering \includegraphics[width=0.92\linewidth]{img/jenkins_dashboard.png} \caption{Jenkins dashboard showing the configured CI jobs.} \label{fig:jenkins-dashboard} \end{figure} \begin{figure}[H] \centering \includegraphics[width=0.92\linewidth]{img/jenkins_failed_build.png} \caption{Jenkins PR 8 build 2: one automated test failed and Jenkins reported FAILURE to Gitea.} \label{fig:jenkins-failed} \end{figure} \begin{figure}[H] \centering \includegraphics[width=0.92\linewidth]{img/jenkins_successful_build.png} \caption{Jenkins PR 8 build 3: both automated tests passed and Jenkins reported SUCCESS.} \label{fig:jenkins-recovery} \end{figure} \begin{figure}[H] \centering \includegraphics[width=0.92\linewidth]{img/gitea_pr8_merged.png} \caption{Gitea pull request 8 merged from the Jenkins feature branch into main.} \label{fig:gitea-pr8-merged} \end{figure} \begin{figure}[H] \centering \includegraphics[width=0.92\linewidth]{img/gitea_pr8_approval.png} \caption{Gitea reviewer approval and merge activity for pull request 8.} \label{fig:gitea-pr8-approval} \end{figure} \begin{figure}[H] \centering \includegraphics[width=0.92\linewidth]{img/jenkins_main_success.png} \caption{Post-merge Jenkins main branch build 1: two tests passed with zero failures.} \label{fig:jenkins-main-success} \end{figure} \begin{figure}[H] \centering \includegraphics[width=0.92\linewidth]{img/gitea_branch_protection.png} \caption{Gitea repository branch protection rule for main.} \label{fig:gitea-branch-protection} \end{figure} \begin{figure}[H] \centering \includegraphics[width=0.92\linewidth]{img/gitea_required_ci_check.png} \caption{Gitea branch protection requiring the Jenkins pr-main status check.} \label{fig:gitea-required-check} \end{figure} \section{Operations and Verification Evidence} \subsection{Routine Jenkins Operations} Jenkins is managed through Docker Compose on the DigitalOcean Droplet. The Compose project is located at \texttt{/opt/jenkins}. The following commands provide routine operational control and inspection: \begin{verbatim} cd /opt/jenkins # Inspect container status docker compose ps # Start Jenkins docker compose up -d # Stop Jenkins docker compose stop # Inspect recent logs docker compose logs --tail=100 jenkins # Verify effective configuration docker compose config \end{verbatim} The commands above are documented operational procedures; they are not all claimed as separately executed tests. \subsection{Persistence and Recovery} The Jenkins home directory is stored on the host at \texttt{/opt/jenkins/jenkins\_home}. This bind mount preserves Jenkins application data independently of the container writable layer. Before container replacement or configuration changes, administrators should back up the Jenkins home directory and the Docker Compose configuration. Because the restart policy is \texttt{no}, Jenkins must be explicitly started after a host restart. \subsection{Troubleshooting Procedure} If Jenkins becomes unavailable, the administrator should first inspect the container state and logs: \begin{verbatim} cd /opt/jenkins docker compose ps docker compose logs --tail=100 jenkins \end{verbatim} If the container is running but the public Jenkins hostname is unavailable, the administrator should inspect Caddy, DNS, HTTPS certificate status, and Docker network connectivity. If a pipeline fails, the Jenkins console output should be inspected to identify the failing stage. The controlled failure experiment in pull request \#8 demonstrates this diagnostic workflow. \subsection{Verified Continuous Integration Evidence} The following results were observed during the Jenkins and Gitea integration exercise: \begin{enumerate} \item Jenkins ran using the custom Docker image \texttt{jenkins-node:2.580.1}. \item Docker Compose confirmed persistent storage, resource limits, and localhost-only port publishing. \item Caddy routed the Jenkins hostname to \texttt{jenkins:8080}. \item Gitea webhook events triggered Jenkins pull-request builds. \item Pull request \#8 produced a deliberately failed Jenkins build \#2. \item A corrective commit produced successful Jenkins pull-request build \#3. \item Gitea displayed the required Jenkins status check as successful. \item Reviewer \texttt{shiv} approved the pull request before it was merged. \item The merged main branch passed Jenkins build \#1 with two passing automated tests and zero failures. \end{enumerate} \subsection{Evidence and Submission Controls} Screenshots of the actual Docker deployment, Jenkins pipeline stages, failed and successful builds, Gitea required status check, reviewer approval, and merged pull request should be included in the final manual. Each screenshot must correspond to an actual observed result and include an explanatory caption. Sensitive credentials, API tokens, private keys, and passwords must not appear in the submitted PDF. The final compiled manual should be checked for correct chapter ordering, figure references, document history, and successful LaTeX compilation.