From 120e00ea2e0e4a65829d0fe5541d76d0b337c0a4 Mon Sep 17 00:00:00 2001 From: Sarah Vaupel Date: Mon, 16 Sep 2024 01:51:13 +0200 Subject: [PATCH] ci(gitlab-ci): pull frontend artifacts from backend downstream pipeline --- .gitlab-ci.yml | 2 +- .gitlab-ci/backend.yml | 4 +++ .gitlab-ci/pull-frontend-artifacts.pl | 52 +++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100755 .gitlab-ci/pull-frontend-artifacts.pl diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9d6c68047..c4bd76f1e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -67,7 +67,7 @@ setup:dynamic: - BACKEND_IMAGE_VERSION=`${GIT_LOG_COMMAND} ${BACKEND_IMAGE_DEPENDENCIES} | tee backend-image-version` - 'echo "FRONTEND_IMAGE_VERSION: ${FRONTEND_IMAGE_VERSION}, BACKEND_IMAGE_VERSION: ${BACKEND_IMAGE_VERSION}"' - cat .gitlab-ci/frontend.yml | .gitlab-ci/dynamci.pl FRONTEND_IMAGE_VERSION=${FRONTEND_IMAGE_VERSION} > frontend.yml - - cat .gitlab-ci/backend.yml | .gitlab-ci/dynamci.pl BACKEND_IMAGE_VERSION=${BACKEND_IMAGE_VERSION} > backend.yml + - cat .gitlab-ci/backend.yml | .gitlab-ci/dynamci.pl BACKEND_IMAGE_VERSION=${BACKEND_IMAGE_VERSION} PARENT_PIPELINE_ID=${CI_PIPELINE_ID} > backend.yml artifacts: paths: - frontend-image-version diff --git a/.gitlab-ci/backend.yml b/.gitlab-ci/backend.yml index e951ba3b6..beb349b68 100644 --- a/.gitlab-ci/backend.yml +++ b/.gitlab-ci/backend.yml @@ -13,6 +13,7 @@ variables: BACKEND_IMAGE_VERSION: #dyn# + PARENT_PIPELINE_ID: #dyn# stages: - compile @@ -26,6 +27,9 @@ default: entrypoint: [""] docker: platform: x86_64 + before_script: + - ./.gitlab-ci/pull-frontend-artifacts.pl "${REGISTRY_AUTH_TOKEN}" "${PARENT_PIPELINE_ID}" + - unzip ./.artifacts.tmp/artifacts.zip artifacts: name: "${CI_JOB_NAME}-${CI_COMMIT_SHORT_SHA}" expire_in: "1 day" diff --git a/.gitlab-ci/pull-frontend-artifacts.pl b/.gitlab-ci/pull-frontend-artifacts.pl new file mode 100755 index 000000000..8c6c9e457 --- /dev/null +++ b/.gitlab-ci/pull-frontend-artifacts.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +my $dir = ".artifacts.tmp"; +my ($token, $id) = @ARGV; +die "usage: $0 [token] [id]" unless defined $token and defined $id; +die "id in bad format" unless $id=~m#^[0-9]+$#; + +if(!-d $dir) { + mkdir($dir) or die "Cannot create directory '$dir', because: $!\n"; +} + +system(qq(curl --globoff --header "PRIVATE-TOKEN: $token" "https://gitlab.uniworx.de/api/v4/projects/5/pipelines/$id/bridges" > $dir/bridges)); +my $pips = pparse("$dir/bridges", {id=>qq#."downstream_pipeline"."id"#}, {name=>""}); + +my $fe = $pips->{frontend}{id}; + +die "No frontend pipeline found!" unless $fe; + +system(qq(curl --globoff --header "PRIVATE-TOKEN: $token" "https://gitlab.uniworx.de/api/v4/projects/5/pipelines/$fe/jobs" > $dir/fe-jobs)); +my $arte = pparse("$dir/fe-jobs", {id=>""}, {name=>"", web_url=>"", artifacts=>""}); + +system(qq#curl --output $dir/artifacts.zip --location --header "PRIVATE-TOKEN: $token" "https://gitlab.uniworx.de/api/v4/projects/5/jobs/$arte->{compile}{id}/artifacts"#); + + +sub pparse { + my ($file, $numerical, $alpha) = @_; + my %all = (); + for my $k(keys %$numerical) { + $all{$k} = $numerical->{$k} || qq#."$k"#; + } + for my $k(keys %$alpha) { + $all{$k} = $alpha->{$k} || qq#."$k"#; + } + my $select = join ', ', map {qq#"$_": $all{$_}#} sort keys %all; + my $cont = qx(cat $file | jq -c '.[] | {$select}'); + my @cont = split m/\R/, $cont; + my %ret = (); + for my $c(@cont) { + my %block = (); + for(keys %$numerical) { + $block{$_} = $1 if $c=~m#"$_":([0-9]+)#; + } + for(keys %$alpha) { + $block{$_} = $1 if $c=~m#"$_":"([^"]*)"#; + } + $ret{$block{name}} =\%block; + } + return \%ret +} \ No newline at end of file