Hog Hog2026.2-1
download_child_artifacts.tcl
Go to the documentation of this file.
1 # @file
2 # Copyright 2018-2026 The University of Birmingham
3 # Copyright 2018-2026 Max-Planck-Institute for Physics
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 # Downloads artifacts from child pipelines
19 #parsing command options
20 set OldPath [pwd]
21 set TclPath [file dirname [info script]]/..
22 set repo_path [file normalize "$TclPath/../.."]
23 source $TclPath/hog.tcl
24 
25 set usage "- CI script that downloads artifacts from child pipelines.\n USAGE: $::argv0 <project id> <commit SHA> <create_job id>."
26 
27 if {[llength $argv] < 3} {
28  Msg Info $usage
29  cd $OldPath
30  return
31 }
32 
33 set proj_id [lindex $argv 0]
34 set commit_sha [lindex $argv 1]
35 set create_job_id [lindex $argv 2]
36 set page 1
37 
38 lassign [ExecuteRet glab api "/projects/$proj_id/jobs/?page=1"] ret msg
39 if {$ret != 0} {
40  Msg Error "Some problem when getting parent pipeline: $msg"
41  return -1
42 } else {
43  set result [catch {package require json} JsonFound]
44  if {"$result" != "0"} {
45  Msg Error "Cannot find JSON package equal or higher than 1.0.\n $JsonFound\n Exiting"
46  return -1
47  }
48 
49  set ChildList [json::json2dict $msg]
50  foreach Child $ChildList {
51  set result [catch {dict get $Child "id"} child_job_id]
52  if {"$result" != "0" || $child_job_id < $create_job_id} {
53  continue
54  }
55  set result [catch {dict get [dict get $Child "commit"] "id"} child_sha]
56  if {"$result" != "0"} {
57  Msg Error "Error when retrieving SHA of child process $child_job_id. Error message:\n $child_sha\n Exiting"
58  return -1
59  }
60  if {"$child_sha" != "$commit_sha"} {
61  #Msg CriticalWarning "Child process $child_job_id SHA $child_sha does not correspond to current SHA $commit_sha. Ignoring child process"
62  continue
63  }
64 
65  set result [catch {dict get $Child "name"} job_name]
66  if {"$result" != "0" || "$job_name" != "collect_artifacts"} {
67  continue
68  }
69 
70  #ignoring jobs without artifacts
71  set result [catch {dict get $Child "artifacts"} artifact_list]
72  if {"$result" != "0"} {
73  continue
74  }
75  set withArchive 0
76  foreach artifact $artifact_list {
77  set result [catch {dict get $artifact "file_type"} file_type]
78  if {"$result" != "0"} {
79  Msg CriticalWarning "Problem when reading artifact for child process $job_name"
80  continue
81  }
82  if {"$file_type" == "archive"} {
83  set withArchive 1
84  }
85  }
86  if {$withArchive == "0"} {
87  Msg Info "No archive artifacts found for child job $job_name, ignoring it\n"
88  continue
89  }
90 
91  Msg Info "Downloading artifacts for child job at: projects/${proj_id}/jobs/${child_job_id}/artifacts/"
92  lassign [ExecuteRet glab api "/projects/${proj_id}/jobs/${child_job_id}/artifacts/" > output_${child_job_id}.zip] ret msg
93  if {$ret != 0} {
94  Msg Error "Some problem when downloading artifacts for child job id:$child_job_id. Error message: $msg"
95  return -1
96  }
97  Execute unzip -o output_${child_job_id}.zip
98  file delete output_${child_job_id}.zip
99  }
100 }