From 6f6c649ec606d6ec5bd5874002fda6efb568e9ec Mon Sep 17 00:00:00 2001 From: Pamplemousse Date: Wed, 5 May 2021 13:52:52 -0700 Subject: [PATCH 1/3] jenkins: Create the `jenkins-cli` command Signed-off-by: Pamplemousse --- .../continuous-integration/jenkins/default.nix | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pkgs/development/tools/continuous-integration/jenkins/default.nix b/pkgs/development/tools/continuous-integration/jenkins/default.nix index 9807866f1bdf..49e083e26969 100644 --- a/pkgs/development/tools/continuous-integration/jenkins/default.nix +++ b/pkgs/development/tools/continuous-integration/jenkins/default.nix @@ -1,5 +1,5 @@ -{ lib, stdenv, fetchurl, common-updater-scripts, coreutils, git, gnused, nix -, nixfmt, writeScript, nixosTests, jq, cacert, curl }: +{ lib, stdenv, fetchurl, common-updater-scripts, coreutils, git, gnused, makeWrapper, nix +, nixfmt, openjdk, writeScript, nixosTests, jq, cacert, curl }: stdenv.mkDerivation rec { pname = "jenkins"; @@ -10,9 +10,19 @@ stdenv.mkDerivation rec { sha256 = "0lficvngxzl7q088n3ssnnhjicd0xxr0k3n0inz7pvjj27dl35rr"; }; + nativeBuildInputs = [ makeWrapper ]; + buildCommand = '' - mkdir -p "$out/webapps" + mkdir -p "$out/bin" "$out/share" "$out/webapps" + cp "$src" "$out/webapps/jenkins.war" + + # Create the `jenkins-cli` command. + ${openjdk}/bin/jar -xf "$src" WEB-INF/lib/cli-${version}.jar \ + && mv WEB-INF/lib/cli-${version}.jar "$out/share/jenkins-cli.jar" + + makeWrapper "${openjdk}/bin/java" "$out/bin/jenkins-cli" \ + --add-flags "-jar $out/share/jenkins-cli.jar" ''; passthru = { From 4265efef54bd5e2fe6d7dff3241d69ef3e86b161 Mon Sep 17 00:00:00 2001 From: Pamplemousse Date: Wed, 5 May 2021 14:21:24 -0700 Subject: [PATCH 2/3] nixos/modules/jenkins: Add option to add CLI Signed-off-by: Pamplemousse --- .../jenkins/default.nix | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/nixos/modules/services/continuous-integration/jenkins/default.nix b/nixos/modules/services/continuous-integration/jenkins/default.nix index cdc3b4b5c58f..889688a26853 100644 --- a/nixos/modules/services/continuous-integration/jenkins/default.nix +++ b/nixos/modules/services/continuous-integration/jenkins/default.nix @@ -2,6 +2,7 @@ with lib; let cfg = config.services.jenkins; + jenkinsUrl = "http://${cfg.listenAddress}:${toString cfg.port}${cfg.prefix}"; in { options = { services.jenkins = { @@ -141,14 +142,34 @@ in { Additional command line arguments to pass to the Java run time (as opposed to Jenkins). ''; }; + + withCLI = mkOption { + type = types.bool; + default = false; + description = '' + Whether to make the CLI available. + + More info about the CLI available at + + https://www.jenkins.io/doc/book/managing/cli . + ''; + }; }; }; config = mkIf cfg.enable { - # server references the dejavu fonts - environment.systemPackages = [ - pkgs.dejavu_fonts - ]; + environment = { + # server references the dejavu fonts + systemPackages = [ + pkgs.dejavu_fonts + ] ++ optional cfg.withCLI cfg.package; + + variables = {} + // optionalAttrs cfg.withCLI { + # Make it more convenient to use the `jenkins-cli`. + JENKINS_URL = jenkinsUrl; + }; + }; users.groups = optionalAttrs (cfg.group == "jenkins") { jenkins.gid = config.ids.gids.jenkins; @@ -215,7 +236,7 @@ in { ''; postStart = '' - until [[ $(${pkgs.curl.bin}/bin/curl -L -s --head -w '\n%{http_code}' http://${cfg.listenAddress}:${toString cfg.port}${cfg.prefix} | tail -n1) =~ ^(200|403)$ ]]; do + until [[ $(${pkgs.curl.bin}/bin/curl -L -s --head -w '\n%{http_code}' ${jenkinsUrl} | tail -n1) =~ ^(200|403)$ ]]; do sleep 1 done ''; From 4f093b8fdb3e6b7102ad99089712beca2ae49abb Mon Sep 17 00:00:00 2001 From: Pamplemousse Date: Sun, 4 Jul 2021 14:49:39 -0700 Subject: [PATCH 3/3] nixos/modules/jenkins: Test the CLI Signed-off-by: Pamplemousse --- nixos/tests/jenkins-cli.nix | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 nixos/tests/jenkins-cli.nix diff --git a/nixos/tests/jenkins-cli.nix b/nixos/tests/jenkins-cli.nix new file mode 100644 index 000000000000..f25e1604da33 --- /dev/null +++ b/nixos/tests/jenkins-cli.nix @@ -0,0 +1,30 @@ +import ./make-test-python.nix ({ pkgs, ...} : rec { + name = "jenkins-cli"; + meta = with pkgs.lib.maintainers; { + maintainers = [ pamplemousse ]; + }; + + nodes = { + machine = + { ... }: + { + services.jenkins = { + enable = true; + withCLI = true; + }; + }; + }; + + testScript = '' + start_all() + + machine.wait_for_unit("jenkins") + + assert "JENKINS_URL" in machine.succeed("env") + assert "http://0.0.0.0:8080" in machine.succeed("echo $JENKINS_URL") + + machine.succeed( + "jenkins-cli -auth admin:$(cat /var/lib/jenkins/secrets/initialAdminPassword)" + ) + ''; +})