هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
الهدف
Run Jenkins in a container and write a declarative Jenkinsfile that follows the CI/CD best practices from the course: every stage in code, fast feedback first, tests with reports, a security scan that can stop the build, an artifact with a version, a manual approval before production, and clean-up in post. The same ideas map 1:1 to GitHub Actions and GitLab CI.
قبل أن تبدأ
- Docker works. Start Jenkins LTS with a named volume:
- Open
http://localhost:8080, paste the password, install the suggested plugins, make an admin user. - Use the small Python app from the TDD lab (
alerts.py+test_alerts.py) in a Git repository.
المهام
ضع علامة على المهمة عندما تنتهي منها. متصفحك فقط يتذكر ذلك.
- Write a
Jenkinsfilewith the stages Checkout, Lint, Test, Security scan, Package, Deploy to staging, Approve, Deploy to production. - Run the Python steps inside a
python:3.12-slimcontainer agent or install Python in ashstep with a virtual environment (keep the job self-contained). - Publish the test results (
pytest --junitxml) so Jenkins shows the test trend. - Fail the build when Bandit finds a high-severity issue.
- Package a versioned artifact (
app-${BUILD_NUMBER}.tar.gz) and archive it with a SHA-256 checksum. - Add an
inputstep before production that only the grouprelease-managers(or your admin) may approve, with a 30-minute timeout. - Add
options(timeout of the whole job, keep the last 10 builds, no concurrent builds) and apostsection (always clean the workspace; on failure, print a clear message). - Create a Pipeline job that reads the Jenkinsfile from your repository (Pipeline script from SCM) and run it twice: once green, once with a broken test.
تحقّق من عملك
- The stage view shows all 8 stages; the run waits at Approve until you click Proceed.
- Test Result shows the number of tests; the broken run is red at Test and never reaches Package.
- The artifacts of a green build are
app-N.tar.gzandapp-N.tar.gz.sha256. - Changing the pipeline means a Git commit — nobody edits the job in the UI.
تلميحات
اعرض التلميحات
agent { docker { image 'python:3.12-slim' } }needs the Docker Pipeline plugin and Docker inside Jenkins; the simpler way for the lab is a virtual environment inshsteps (installpython3-venvin the container once:docker exec -u root jenkins apt-get update && docker exec -u root jenkins apt-get install -y python3-venv).junit 'reports/*.xml'publishes test results;archiveArtifacts artifacts: 'dist/*', fingerprint: truekeeps files.input message: 'Deploy to production?', submitter: 'release-managers'insidetimeout(time: 30, unit: 'MINUTES') { ... }.- Never put passwords in the Jenkinsfile: use Credentials and
withCredentials.
الحل
اعرض الحل
Jenkinsfile
pipeline {
agent any
options {
timeout(time: 45, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '10'))
disableConcurrentBuilds()
}
environment {
VENV = "${WORKSPACE}/.venv"
}
stages {
stage('Checkout') { steps { checkout scm } }
stage('Lint') {
steps {
sh 'python3 -m venv $VENV && . $VENV/bin/activate && pip install -q pytest bandit ruff'
sh '. $VENV/bin/activate && ruff check .'
}
}
stage('Test') {
steps { sh '. $VENV/bin/activate && mkdir -p reports && pytest -q --junitxml=reports/tests.xml' }
post { always { junit 'reports/*.xml' } }
}
stage('Security scan') {
steps { sh '. $VENV/bin/activate && bandit -r . -x ./.venv,./test_alerts.py --severity-level high' }
}
stage('Package') {
steps {
sh 'mkdir -p dist && tar czf dist/app-${BUILD_NUMBER}.tar.gz alerts.py && cd dist && sha256sum app-${BUILD_NUMBER}.tar.gz > app-${BUILD_NUMBER}.tar.gz.sha256'
archiveArtifacts artifacts: 'dist/*', fingerprint: true
}
}
stage('Deploy to staging') {
steps { sh 'echo "deploy app-${BUILD_NUMBER} to staging" && mkdir -p /tmp/staging && cp dist/app-${BUILD_NUMBER}.tar.gz /tmp/staging/' }
}
stage('Approve') {
steps {
timeout(time: 30, unit: 'MINUTES') {
input message: "Deploy app-${env.BUILD_NUMBER} to production?", submitter: 'admin,release-managers'
}
}
}
stage('Deploy to production') {
steps { sh 'echo "deploy app-${BUILD_NUMBER} to production" && mkdir -p /tmp/prod && cp dist/app-${BUILD_NUMBER}.tar.gz /tmp/prod/' }
}
}
post {
failure { echo "Build ${env.BUILD_NUMBER} failed in a quality gate. Fix it before you merge." }
always { cleanWs() }
}
}- Commit the Jenkinsfile, then in Jenkins: New Item → Pipeline → Pipeline script from SCM → Git → your repository URL → Script Path
Jenkinsfile→ Save → Build Now. - Click Proceed at Approve and check the artifacts and Test Result.
- Break a test (
return a - b), commit, build again: red at Test, no artifact, no deploy. Revert the commit. - Clean up:
docker rm -f jenkins && docker volume rm jenkins_home.
ملاحظاتي
لا توجد ملاحظات بعد.