digipolisgent/domainator9k-citype-jenkins-bundle
Installation
Add the bundle to your composer.json:
composer require district09/domainator9k-citype-jenkins-bundle
Enable the bundle in config/bundles.php:
return [
// ...
District09\Domainator9kCitypeJenkinsBundle\Domainator9kCitypeJenkinsBundle::class => ['all' => true],
];
Configuration
Publish the default config (if needed) and update config/packages/domainator9k_citype_jenkins.yaml:
php bin/console config:dump-reference District09\Domainator9kCitypeJenkinsBundle
Example minimal config:
domainator9k_citype_jenkins:
jenkins_url: 'http://your-jenkins-server'
api_token: '%env(JENKINS_API_TOKEN)%'
First Use Case: Trigger a Jenkins Job Inject the service and trigger a build:
use District09\Domainator9kCitypeJenkinsBundle\Service\JenkinsService;
class MyController extends AbstractController
{
public function __construct(private JenkinsService $jenkinsService) {}
public function triggerBuild(): Response
{
$jobName = 'my-project';
$this->jenkinsService->triggerJob($jobName);
return new Response('Job triggered!');
}
}
Job Management
triggerJob($jobName, $params = []) to start builds with parameters.getJobStatus($jobName) to check build progress.getBuildLog($jobName, $buildNumber) for debugging.$this->jenkinsService->triggerJob('deployment-pipeline', ['BRANCH' => 'main']);
$status = $this->jenkinsService->getJobStatus('deployment-pipeline');
Parameterized Jobs Pass parameters as an associative array:
$params = [
'ENVIRONMENT' => 'staging',
'DEPLOY_USER' => 'deploy-bot'
];
$this->jenkinsService->triggerJob('deploy', $params);
Event Listeners Subscribe to Jenkins events (e.g., build completion) using Symfony’s event system:
# config/services.yaml
services:
App\EventListener\JenkinsBuildListener:
tags:
- { name: kernel.event_listener, event: domainator9k.citype.jenkins.build_completed, method: onBuildCompleted }
Dependency Injection
Prefer constructor injection for the JenkinsService to ensure testability:
class MyService {
public function __construct(private JenkinsService $jenkins) {}
}
Environment Variables
Store sensitive data (e.g., JENKINS_API_TOKEN) in .env:
JENKINS_API_TOKEN=your_token_here
Caching Responses Cache frequent API calls (e.g., job status) using Symfony’s cache system:
$status = $this->jenkinsService->getJobStatus($jobName, 300); // Cache for 5 minutes
Error Handling Wrap Jenkins API calls in try-catch blocks to handle HTTP errors gracefully:
try {
$this->jenkinsService->triggerJob('failing-job');
} catch (\RuntimeException $e) {
$this->logger->error('Jenkins trigger failed: ' . $e->getMessage());
}
Laravel-Specific Adaptations
If using Laravel, bind the bundle’s services to the container in AppServiceProvider:
public function register()
{
$this->app->bind(
District09\Domainator9kCitypeJenkinsBundle\Service\JenkinsService::class,
function ($app) {
return new \District09\Domainator9kCitypeJenkinsBundle\Service\JenkinsService(
$app['config']['domainator9k_citype_jenkins.jenkins_url'],
$app['config']['domainator9k_citype_jenkins.api_token']
);
}
);
}
Deprecated Symfony Version
symfony/http-client, guzzlehttp/guzzle) to v6+.No Official Laravel Support
API Token Security
ParameterBag.# config/packages/domainator9k_citype_jenkins.yaml
domainator9k_citype_jenkins:
api_token: '%env(JENKINS_TOKEN)%'
Rate Limiting
use Symfony\Component\HttpClient\RetryableHttpClient;
$client = new RetryableHttpClient(
$baseClient,
[
'max_retries' => 3,
'delay' => 1000, // 1 second
'multiplier' => 2,
'max_delay' => 60000, // 1 minute
]
);
Job Name Sensitivity
Enable Debug Mode
Set debug: true in the config to log raw API responses:
domainator9k_citype_jenkins:
debug: '%kernel.debug%'
Check HTTP Headers
Use bin/console debug:config domainator9k_citype_jenkins to verify the Jenkins-User header is set correctly.
Mock Jenkins API for Tests Use a mock HTTP client in tests:
use Symfony\Component\HttpClient\MockHttpClient;
$mockClient = new MockHttpClient([
new Response(200, [], json_encode(['queueId' => 123])),
]);
$this->jenkinsService->setHttpClient($mockClient);
Custom Job Parameters
Extend the JenkinsService to add domain-specific parameters:
class CustomJenkinsService extends JenkinsService {
public function triggerDeployment($branch, $environment) {
$params = [
'BRANCH' => $branch,
'ENV' => strtoupper($environment),
];
return $this->triggerJob('deploy', $params);
}
}
Webhook Integration Create a Symfony controller to handle Jenkins webhook callbacks:
#[Route('/jenkins/webhook', name: 'jenkins_webhook', methods: ['POST'])]
public function handleWebhook(Request $request): Response
{
$data = json_decode($request->getContent(), true);
// Process webhook data (e.g., build status changes)
return new Response('Webhook received');
}
Event Dispatching
Dispatch custom events for Jenkins actions (e.g., jenkins.job.triggered):
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
$dispatcher->dispatch(new JenkinsJobTriggeredEvent($jobName, $params));
How can I help you explore Laravel packages today?