From 73b335bd0966903491a2ef55695af11127f22e06 Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Wed, 10 Jan 2024 16:41:49 -0600 Subject: [PATCH] Add `--job-timeout` to `generate-runner` This will allow the user to set the exact timeout to use for the generated monitor jobs, which is useful if they're known to take a long time. Signed-off-by: Ryan Gonzalez --- README.md | 7 +++++++ src/handler.rs | 15 +++++++++++++-- src/pipeline.rs | 4 ++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 682431c..6ff07e5 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ generate-monitor RUNNER_TAG [--build-info BUILD_INFO_FILE=build-info.yml] [--pipeline-out PIPELINE_FILE=obs.yml] [--job-prefix MONITOR_JOB_PREFIX=obs] + [--job-timeout MONITOR_JOB_TIMEOUT] [--artifact-expiration ARTIFACT_EXPIRATION='3 days'] [--build-log-out BUILD_LOG_FILE=build.log] ``` @@ -214,6 +215,12 @@ Changes the filename of the child pipeline YAML. Changes the prefix that will be prepended to each generated job (`MONITOR_JOB_PREFIX-REPOSITORY-ARCH`). +##### `--job-timeout MONITOR_JOB_TIMEOUT` + +Changes the timeout for each generated job, using the [job `timeout` +setting](https://docs.gitlab.com/ee/ci/yaml/#timeout). If not passed, the +timeout will not be set. + ##### `--artifact-expiration ARTIFACT_EXPIRATION='3 days'` Changes the expiration of the build results & logs. diff --git a/src/handler.rs b/src/handler.rs index 1ba2126..346c7c8 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -84,6 +84,8 @@ struct GenerateMonitorAction { pipeline_out: String, #[clap(long, default_value_t = DEFAULT_PIPELINE_JOB_PREFIX.to_owned())] job_prefix: String, + #[clap(long)] + job_timeout: Option, #[clap(long, default_value_t = DEFAULT_ARTIFACT_EXPIRATION.to_owned())] artifact_expiration: String, #[clap(long, default_value_t = DEFAULT_BUILD_LOG.into())] @@ -410,6 +412,7 @@ impl ObsJobHandler { tags: vec![args.tag], artifact_expiration: args.artifact_expiration, prefix: args.job_prefix, + timeout: args.job_timeout, rules: args.rules, download_binaries: if let Some(build_results_dir) = args.build_results_dir { PipelineDownloadBinaries::OnSuccess { @@ -1266,6 +1269,7 @@ mod tests { download_binaries: bool, ) { const TEST_JOB_RUNNER_TAG: &str = "test-tag"; + const TEST_MONITOR_TIMEOUT: &str = "1 day"; const TEST_BUILD_RESULTS_DIR: &str = "results"; const TEST_BUILD_RESULT: &str = "test-build-result"; const TEST_BUILD_RESULT_CONTENTS: &[u8] = b"abcdef"; @@ -1307,8 +1311,8 @@ mod tests { ); let mut generate_command = format!( - "generate-monitor {} --rules '[{{a: 1}}, {{b: 2}}]'", - TEST_JOB_RUNNER_TAG + "generate-monitor {} --job-timeout '{}' --rules '[{{a: 1}}, {{b: 2}}]'", + TEST_JOB_RUNNER_TAG, TEST_MONITOR_TIMEOUT ); if download_binaries { generate_command += &format!(" --download-build-results-to {}", TEST_BUILD_RESULTS_DIR); @@ -1432,6 +1436,13 @@ mod tests { assert_eq!(tags.len(), 1); assert_eq!(tags[0].as_str().unwrap(), TEST_JOB_RUNNER_TAG); + let timeout = monitor_map + .get(&"timeout".into()) + .unwrap() + .as_str() + .unwrap(); + assert_eq!(timeout, TEST_MONITOR_TIMEOUT); + let rules: Vec<_> = monitor_map .get(&"rules".into()) .unwrap() diff --git a/src/pipeline.rs b/src/pipeline.rs index 89aae04..247cede 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -17,6 +17,7 @@ pub struct GeneratePipelineOptions { pub tags: Vec, pub artifact_expiration: String, pub prefix: String, + pub timeout: Option, pub rules: Option, pub build_log_out: String, pub download_binaries: PipelineDownloadBinaries, @@ -35,6 +36,8 @@ struct JobSpec { before_script: Vec, script: Vec, after_script: Vec, + #[serde(skip_serializing_if = "Option::is_none")] + timeout: Option, artifacts: ArtifactsSpec, #[serde(skip_serializing_if = "Option::is_none")] rules: Option, @@ -114,6 +117,7 @@ pub fn generate_monitor_pipeline( // ensure that they're set to be empty. before_script: vec![], after_script: vec![], + timeout: options.timeout.clone(), artifacts: ArtifactsSpec { paths: artifact_paths, when: "always".to_owned(),