From 041adfcda4d37d7c1da7de1861d98cc98e6e64b2 Mon Sep 17 00:00:00 2001 From: "Codex (authorized Assignment 5 implementation)" Date: Fri, 9 Oct 2026 18:33:17 +0000 Subject: [PATCH] Add Assignment 6 Python application and Jenkins pipeline --- Jenkinsfile | 29 +++++++++++++++++++++++++++++ ops/jenkins/README.md | 24 ++++++++++++++++++++++++ ops/jenkins/compose.jenkins.yaml | 17 +++++++++++++++++ ops/jenkins/install-python.sh | 6 ++++++ pricing.py | 4 ++++ tests/test_pricing.py | 11 +++++++++++ 6 files changed, 91 insertions(+) create mode 100644 Jenkinsfile create mode 100644 ops/jenkins/README.md create mode 100644 ops/jenkins/compose.jenkins.yaml create mode 100644 ops/jenkins/install-python.sh create mode 100644 pricing.py create mode 100644 tests/test_pricing.py diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..55c1a94 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,29 @@ +pipeline { + agent any + options { + skipDefaultCheckout() + disableConcurrentBuilds() + timeout(time: 5, unit: 'MINUTES') + buildDiscarder(logRotator(numToKeepStr: '10')) + timestamps() + } + stages { + stage('Checkout') { + steps { + checkout scm + sh 'git rev-parse HEAD' + } + } + stage('Syntax check') { + steps { + sh 'python3 --version' + sh 'python3 -m py_compile pricing.py tests/test_pricing.py' + } + } + stage('Test') { + steps { + sh 'python3 -m unittest discover -s tests -v' + } + } + } +} diff --git a/ops/jenkins/README.md b/ops/jenkins/README.md new file mode 100644 index 0000000..33b385c --- /dev/null +++ b/ops/jenkins/README.md @@ -0,0 +1,24 @@ +# Assignment 6 Python CI + +The pipeline runs on the trusted Jenkins controller executor. Only trusted +team members may change pipeline code. Do not supply secrets or privileged +Docker access to untrusted pull requests. Fork PR discovery is disabled. + +The controller uses the official Jenkins LTS image recorded in compose.jenkins.yaml. +Python comes from that image's Debian package repositories; record the actual +Python version from each build. No pip dependencies or Docker socket are needed. +After replacing the container, run `sh ops/jenkins/install-python.sh` on the +Droplet before building. A restart retains Python; replacement requires reinstall. +Jenkins jobs and settings persist in the jenkins-home volume. + +From the repository root, reproduce checks with: + + python3 -m py_compile pricing.py tests/test_pricing.py + python3 -m unittest discover -s tests -v + +Expected: both tests pass. Syntax or test failures must fail the Jenkins run. +Multibranch origin PR discovery tests the current PR revision, not a merged revision. +The printed checkout commit identifies what was actually tested. + +Webhook delivery, status reporting, branch protection, and the controlled +failure/repair demonstration must be configured and verified separately. diff --git a/ops/jenkins/compose.jenkins.yaml b/ops/jenkins/compose.jenkins.yaml new file mode 100644 index 0000000..f0b937e --- /dev/null +++ b/ops/jenkins/compose.jenkins.yaml @@ -0,0 +1,17 @@ +services: + jenkins: + image: jenkins/jenkins@sha256:a660310e39ade10631f774bacd5219de767dfd08947ea5c32a739b5e5bb382c1 + container_name: myfileagent-jenkins + restart: unless-stopped + environment: + JAVA_OPTS: "-Xms64m -Xmx256m" + mem_limit: 448m + ports: + - "127.0.0.1:8080:8080" + volumes: + - jenkins-home:/var/jenkins_home + networks: + - gitea-private + +volumes: + jenkins-home: diff --git a/ops/jenkins/install-python.sh b/ops/jenkins/install-python.sh new file mode 100644 index 0000000..b257d56 --- /dev/null +++ b/ops/jenkins/install-python.sh @@ -0,0 +1,6 @@ +#!/bin/sh +# Run on the Droplet after recreating the official Jenkins container. +# Python installed in the container survives restart, not container replacement. +set -eu +docker exec -u root myfileagent-jenkins sh -c 'apt-get update -qq && apt-get install -y --no-install-recommends python3' +docker exec myfileagent-jenkins python3 --version diff --git a/pricing.py b/pricing.py new file mode 100644 index 0000000..446eda4 --- /dev/null +++ b/pricing.py @@ -0,0 +1,4 @@ +def total_price(unit_price, quantity): + if quantity < 1: + raise ValueError("Quantity must be positive") + return unit_price * quantity diff --git a/tests/test_pricing.py b/tests/test_pricing.py new file mode 100644 index 0000000..573f010 --- /dev/null +++ b/tests/test_pricing.py @@ -0,0 +1,11 @@ +import unittest +from pricing import total_price + + +class PricingTests(unittest.TestCase): + def test_total(self): + self.assertEqual(total_price(10, 3), 30) + + def test_invalid_quantity(self): + with self.assertRaises(ValueError): + total_price(10, 0)