هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
pipeline {
agent { label 'linux-docker' }
options { timeout(time: 30, unit: 'MINUTES'); disableConcurrentBuilds() }
environment { IMAGE = "registry.example.com/shop/api" }
stages {
stage('Test') {
steps { sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt && .venv/bin/pytest -q' }
}
stage('Build') {
steps { sh 'docker build -t $IMAGE:$GIT_COMMIT .' }
}
stage('Push') {
when { branch 'main' }
steps {
withCredentials([usernamePassword(credentialsId: 'registry-ci', usernameVariable: 'REG_USER', passwordVariable: 'REG_PASS')]) {
sh 'echo "$REG_PASS" | docker login registry.example.com -u "$REG_USER" --password-stdin'
sh 'docker push $IMAGE:$GIT_COMMIT'
}
}
}
}
post {
always { junit allowEmptyResults: true, testResults: 'reports/*.xml' }
failure { echo "Build failed: ${env.BUILD_URL}" }
}
}Step by step
- Agents, not the controller — set the controller's number of executors to 0 and run builds on agents (VMs, containers or Kubernetes pods) with the label you use in
agent { label … }. - Multibranch Pipeline job — New Item → Multibranch Pipeline → Git/GitHub source. Jenkins finds every branch and PR with a
Jenkinsfileand builds it. - Stages (quick answer) — test, build, push.
when { branch 'main' }pushes only from main.GIT_COMMITtags the image with the exact commit. - Credentials — store passwords and tokens in Jenkins Credentials, never in the Jenkinsfile.
withCredentialsputs them in environment variables and masks them in the log. Use single quotes insh '…'so Groovy does not put the secret into the command text. - Validate before commit — the linter on the controller:
curl -s -X POST -u alice:"$JENKINS_TOKEN" -F "jenkinsfile=<Jenkinsfile" https://jenkins.example.com/pipeline-model-converter/validateIt answers Jenkinsfile successfully validated. or the error with line numbers.
- Test results and artifacts —
junitshows test trends;archiveArtifactskeeps build output. - Shared libraries — when many repositories have the same steps, move them into a Jenkins shared library and call one function.
- Keep Jenkins safe — update Jenkins and plugins often (most security issues are in plugins), use few plugins, no anonymous access, and Configuration as Code (JCasC) for the controller setup.
Check that it worked
The Multibranch job shows the branches; a push to a feature branch runs Test and Build (Push skipped); a merge to main runs all three and the image …/api:<commit> is in the registry; the console log shows **** instead of the password.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Build waits forever ("Waiting for next available executor") | No agent with that label online. | Start/label an agent; check the label. |
| Secret visible in the log | Double quotes in sh "…" with Groovy interpolation. | Single quotes; let the shell read the env var. |
docker: permission denied on the agent | Jenkins user not allowed to use Docker. | Add it to the docker group, or build with rootless Podman or Buildah. |
| Branch not built | No Jenkinsfile in that branch, or scan not triggered. | Add the file; webhook or "Scan repository now". |
| Pipeline syntax error after push | Not validated. | Use the linter or the replay function first. |
| Jenkins slow / unstable | Builds on the controller, too many plugins. | 0 executors on the controller; remove unused plugins. |
Go further
- Command pages: jenkins, docker, git.
- Next how-tos: Create a CI pipeline in GitHub Actions, Run GitLab CI jobs on your own runner, Build and push an image in CI, Run a package proxy with Nexus.
- Learn it: CI/CD services — Jenkins, Delivery tools at work — pipelines. Full guide: Jenkins.
1Why keep the pipeline in a Jenkinsfile?
The pipeline is then code in the repository: reviewed, versioned and the same for every branch.
2Why should the Jenkins controller have zero executors?
Builds then run on agents; a bad build cannot take over or slow down the controller that holds all secrets.