nixos-config/nixos/library/jenkins.nix

151 lines
4.7 KiB
Nix
Raw Normal View History

2019-10-24 02:20:38 +02:00
{ lib, ... }:
with builtins;
2020-03-09 17:49:37 +01:00
# https://jenkinsci.github.io/job-dsl-plugin/
2019-10-24 02:20:38 +02:00
{
# source container url and credentialsId
2019-12-20 05:54:26 +01:00
job = name:
{ url, credentialsId, branch ? "master",
2019-10-24 02:20:38 +02:00
# https://docs.openstack.org/infra/jenkins-job-builder/triggers.html
2019-12-20 05:54:26 +01:00
triggers ? [{
pollscm = {
cron = "H/30 * * * *";
ignore-post-commit-hooks = true;
};
}], ... }:
config: {
job = {
inherit name triggers;
sandbox = true;
project-type = "pipeline";
dsl = let
stage = elem:
let
stageName = head (attrNames elem);
stateScripts = map (stage:
lib.getAttr (typeOf stage) {
string = ''
2019-10-24 02:20:38 +02:00
withEnv(['PATH=/run/current-system/sw/bin/','NIX_PATH=/var/src/']) {
2019-12-20 05:54:26 +01:00
sh '${toString stage}'
}'';
set = let
script = ''
withEnv(['PATH=/run/current-system/sw/bin/','NIX_PATH=/var/src/']) {
sh '${toString stage.script}'
}
'';
in if (stage.credentialsId != null) then ''
2019-10-24 02:20:38 +02:00
sshagent(['${stage.credentialsId}']) { ${script} }
2019-12-20 05:54:26 +01:00
'' else
script;
}) (getAttr stageName elem);
in ''
stage('${stageName}') {
steps {
${concatStringsSep "\n" stateScripts}
}
2019-10-24 02:20:38 +02:00
}
2019-12-20 05:54:26 +01:00
'';
stages = map stage config;
2019-10-24 02:20:38 +02:00
in ''
2019-12-20 05:54:26 +01:00
pipeline {
agent any
2020-03-09 17:49:37 +01:00
post {
failure {
mattermostSend channel: 'notification', color: '#FF0000', message: "Failed to build : [''${env.JOB_NAME}-''${env.BUILD_NUMBER}](''${env.BUILD_URL})"
}
success {
mattermostSend channel: 'jenkins', color: '#00FF00', message: "Successfully build : [''${env.JOB_NAME}-''${env.BUILD_NUMBER}](''${env.JOB_URL})"
}
}
2019-12-20 05:54:26 +01:00
stages{
stage('Pull') {
steps {
checkout(
[$class: 'GitSCM'
, branches: [[name: '*/${branch}']]
, doGenerateSubmoduleConfigurations: false
, extensions: [[$class: 'LocalBranch', localBranch: 'master']]
, submoduleCfg: []
, userRemoteConfigs:
[[ credentialsId: '${credentialsId}'
, url: '${url}']]
]
)
}
}
${concatStringsSep "\n" stages}
2019-10-24 02:20:38 +02:00
}
}
'';
2019-12-20 05:54:26 +01:00
};
};
2019-10-24 02:20:38 +02:00
# creates a sync job
# source and target container url and credentialsId
syncJob = name: source: target: {
job = {
name = name;
sandbox = true;
project-type = "pipeline";
2019-12-20 05:54:26 +01:00
triggers = [{
2019-10-24 02:20:38 +02:00
pollscm = {
cron = "H/30 * * * *";
ignore-post-commit-hooks = true;
};
2019-12-20 05:54:26 +01:00
}];
2019-10-24 02:20:38 +02:00
dsl = ''
pipeline {
agent any
2020-03-09 17:49:37 +01:00
post {
failure {
mattermostSend channel: 'notification', color: '#FF0000', message: "Failed to build : [''${env.JOB_NAME}-''${env.BUILD_NUMBER}](''${env.BUILD_URL})"
}
success {
mattermostSend channel: 'jenkins', color: '#00FF00', message: "Successfully build : [''${env.JOB_NAME}-''${env.BUILD_NUMBER}](''${env.JOB_URL})"
}
}
2019-10-24 02:20:38 +02:00
stages{
stage('Pull') {
steps {
checkout(
[$class: 'GitSCM'
, branches: [[name: '*/master']]
, doGenerateSubmoduleConfigurations: false
, extensions: [[$class: 'LocalBranch', localBranch: 'master']]
, submoduleCfg: []
, userRemoteConfigs:
[[ credentialsId: '${source.credentialsId}'
, url: '${source.url}']]
]
)
}
}
stage('Push') {
steps {
sshagent(['${target.credentialsId}']) {
withEnv(['PATH=/run/current-system/sw/bin/','NIX_PATH=/var/src/']) {
sh "git push -f ${target.url}"
}
}
}
}
stage('Push Tags') {
steps {
sshagent(['${target.credentialsId}']) {
withEnv(['PATH=/run/current-system/sw/bin/']) {
sh "git push -f ${target.url} --tags"
}
}
}
}
}
}
'';
};
};
}