← notlar

2026-03-16

Jenkins API ile pipeline oluşturma

Her yeni proje için Jenkins'te folder + 3 pipeline job oluşturuyorum: backend, ecms, client. Elle yapmak yerine API ile otomatize ettim.

Folder oluşturma

curl -X POST "https://jenkins.example.com/createItem?name=proje-adi" \
  -u "kullanici:api-token" \
  -H "Content-Type: application/xml" \
  -d '<com.cloudbees.hudson.plugins.folder.Folder/>'

Basit XML body yeterli. Folder içi boş bir container.

Pipeline job oluşturma

Folder'ın içine job için URL'e folder adını ekliyorsun:

curl -X POST "https://jenkins.example.com/job/proje-adi/createItem?name=backend" \
  -u "kullanici:api-token" \
  -H "Content-Type: application/xml" \
  -d @pipeline-config.xml

Config XML:

<flow-definition>
  <definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition">
    <scm class="hudson.plugins.git.GitSCM">
      <userRemoteConfigs>
        <hudson.plugins.git.UserRemoteConfig>
          <url>git@github.com:org/repo.git</url>
          <credentialsId>git-credential-id</credentialsId>
        </hudson.plugins.git.UserRemoteConfig>
      </userRemoteConfigs>
      <branches>
        <hudson.plugins.git.BranchSpec>
          <name>*/master</name>
        </hudson.plugins.git.BranchSpec>
      </branches>
    </scm>
    <scriptPath>Jenkinsfile</scriptPath>
  </definition>
  <assignedNode>AGENT-LABEL</assignedNode>
  <canRoam>false</canRoam>
</flow-definition>

Önemli alanlar: url (repo), credentialsId (Jenkins credential), scriptPath (Jenkinsfile yolu), assignedNode (agent).

Build tetikleme

curl -X POST "https://jenkins.example.com/job/proje-adi/job/backend/build" \
  -u "kullanici:api-token"

Async başlıyor. Sonucu sorgulamak için:

curl "https://jenkins.example.com/job/proje-adi/job/backend/lastBuild/api/json" \
  -u "kullanici:api-token"

result alanı: SUCCESS, FAILURE veya null (devam ediyor).

Dikkat ettiklerim

  • API token: Jenkins UI > kullanıcı > Configure > API Token
  • Folder/job isimleri URL-safe olmalı, Türkçe karakter ve boşluk kullanma
  • assignedNode yanlışsa build sırada bekler, hata vermez
  • Git credential Jenkins'te tanımlı olmalı, yoksa build başlar ama repo çekemez

3 pipeline kalıbı

Her projede aynı 3 job:

JobNe yapıyor
backendSpring Boot build, Docker image, container restart
ecmsNuxt.js admin panel build, deploy
clientNuxt.js frontend build, deploy

Template haline getirdim. Yeni projede sadece repo URL ve agent label değişiyor.