متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 240CI/CD · Jenkins

How to build a pipeline with Jenkins

Clicking build steps together in Jenkins jobs works until someone changes them and nobody knows what was there before. A Jenkinsfile keeps the pipeline as code in the repository: reviewed, versioned, the same for every branch. This how-to writes a declarative pipeline that tests, builds and pushes an image, uses credentials safely, runs on agents (not on the controller), and sets up a Multibranch job.

  • 5 الدروس
  • 4 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

240.1

The quick answer

Jenkinsfile
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}" }
  }
}

240.2

Step by step

  1. 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 … }.
  2. Multibranch Pipeline job — New Item → Multibranch Pipeline → Git/GitHub source. Jenkins finds every branch and PR with a Jenkinsfile and builds it.
  3. Stages (quick answer) — test, build, push. when { branch 'main' } pushes only from main. GIT_COMMIT tags the image with the exact commit.
  4. Credentials — store passwords and tokens in Jenkins Credentials, never in the Jenkinsfile. withCredentials puts them in environment variables and masks them in the log. Use single quotes in sh '…' so Groovy does not put the secret into the command text.
  5. 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/validate

It answers Jenkinsfile successfully validated. or the error with line numbers.

  1. Test results and artifacts — junit shows test trends; archiveArtifacts keeps build output.
  2. Shared libraries — when many repositories have the same steps, move them into a Jenkins shared library and call one function.
  3. 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.

الأوامر في هذا الدرس

240.3

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.

240.4

Common problems

ProblemCauseFix
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 logDouble quotes in sh "…" with Groovy interpolation.Single quotes; let the shell read the env var.
docker: permission denied on the agentJenkins user not allowed to use Docker.Add it to the docker group, or build with rootless Podman or Buildah.
Branch not builtNo Jenkinsfile in that branch, or scan not triggered.Add the file; webhook or "Scan repository now".
Pipeline syntax error after pushNot validated.Use the linter or the replay function first.
Jenkins slow / unstableBuilds on the controller, too many plugins.0 executors on the controller; remove unused plugins.

الأوامر في هذا الدرس

240.5

Go further

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.

كل الأوامر في هذا الفصل