nixos-config/library/jenkins.nix
2019-12-20 17:54:26 +13:00

132 lines
3.8 KiB
Nix

{ lib, ... }:
with builtins;
{
# source container url and credentialsId
job = name:
{ url, credentialsId, branch ? "master",
# https://docs.openstack.org/infra/jenkins-job-builder/triggers.html
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 = ''
withEnv(['PATH=/run/current-system/sw/bin/','NIX_PATH=/var/src/']) {
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 ''
sshagent(['${stage.credentialsId}']) { ${script} }
'' else
script;
}) (getAttr stageName elem);
in ''
stage('${stageName}') {
steps {
${concatStringsSep "\n" stateScripts}
}
}
'';
stages = map stage config;
in ''
pipeline {
agent any
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}
}
}
'';
};
};
# creates a sync job
# source and target container url and credentialsId
syncJob = name: source: target: {
job = {
name = name;
sandbox = true;
project-type = "pipeline";
triggers = [{
pollscm = {
cron = "H/30 * * * *";
ignore-post-commit-hooks = true;
};
}];
dsl = ''
pipeline {
agent any
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"
}
}
}
}
}
}
'';
};
};
}