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