diff --git a/.tekton/pull-request.yaml b/.tekton/pull-request.yaml index 5355879d27..73b4fca78d 100644 --- a/.tekton/pull-request.yaml +++ b/.tekton/pull-request.yaml @@ -227,7 +227,8 @@ spec: name: e2e-test # Added a timeout due to https://issues.redhat.com/browse/STONEBLD-2265 timeout: "2h" - - name: check-task-pipeline-repo-existence + # TODO only for testing, remove after. Should fail anyway because of a missing quay token + - name: create-repositories-if-missing when: - input: "tasks_pipelines" operator: "in" @@ -236,12 +237,12 @@ spec: - build-bundles taskSpec: steps: - - name: fail-when-repo-is-missed + - name: run-create-bundle-repos image: quay.io/konflux-ci/pull-request-builds:appstudio-utils-{{revision}} workingDir: $(workspaces.source.path)/source script: | #!/usr/bin/env bash - .tekton/scripts/check-task-pipeline-bundle-repos.sh + .tekton/scripts/create-task-pipeline-bundle-repos.sh workspaces: - name: source workspaces: diff --git a/.tekton/push.yaml b/.tekton/push.yaml index 3ad2aa839c..e8cca52499 100644 --- a/.tekton/push.yaml +++ b/.tekton/push.yaml @@ -176,6 +176,29 @@ spec: workspaces: - name: source + - name: create-repositories-if-missing + runAfter: + - build-bundles + taskSpec: + steps: + - name: run-create-bundle-repos + image: quay.io/konflux-ci/pull-request-builds:appstudio-utils-{{revision}} + workingDir: $(workspaces.source.path)/source + script: | + #!/usr/bin/env bash + .tekton/scripts/create-task-pipeline-bundle-repos.sh + env: + - name: QUAY_TOKEN + valueFrom: + secretKeyRef: + name: konflux-ci-repo-creator + key: quaytoken + workspaces: + - name: source + workspaces: + - name: source + workspace: workspace + - name: update-infra-repo runAfter: - build-bundles diff --git a/.tekton/scripts/check-task-pipeline-bundle-repos.sh b/.tekton/scripts/check-task-pipeline-bundle-repos.sh deleted file mode 100755 index 74e3f3eaad..0000000000 --- a/.tekton/scripts/check-task-pipeline-bundle-repos.sh +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/bash - -set -o errexit -set -o pipefail -set -o nounset - -SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -cd "$SCRIPTDIR/../.." - -CATALOG_NAMESPACES=( - konflux-ci/tekton-catalog -) - -locate_bundle_repo() { - local -r quay_namespace="$1" - local -r type="$2" - local -r object="$3" - - curl -I -s -L -w "%{http_code}\n" -o /dev/null "https://quay.io/v2/${quay_namespace}/${type}-${object}/tags/list" -} - -locate_in_all_namespaces() { - local -r type="$1" - local -r object="$2" - - local rc=0 - - for quay_namespace in "${CATALOG_NAMESPACES[@]}"; do - found=$(locate_bundle_repo "$quay_namespace" "$type" "$object") - if [ "$found" != "200" ]; then - echo "Missing $type bundle repo: ${quay_namespace}/${type}-${object}" - rc=1 - fi - done - - return "$rc" -} - -has_missing_repo= - -echo "Checking existence of task and pipeline bundle repositories ..." - -# tasks -for task_dir in $(find task/*/*/ -maxdepth 0 -type d); do - if [ ! -f $task_dir/kustomization.yaml ]; then - # expected structure: task/${name}/${version}/${name}.yaml - task_name=$(basename "$(dirname "$task_dir")") - task_name=$(yq < "$task_dir/$task_name.yaml" .metadata.name) - else - task_name=$(oc kustomize "$task_dir" | yq .metadata.name) - fi - - if ! locate_in_all_namespaces task "$task_name"; then - has_missing_repo=yes - fi -done - -# pipelines -pl_names=($(oc kustomize pipelines/ | yq -o json '.metadata.name' | jq -r)) -# Currently, only one pipeline for core services CI -pl_names+=($(oc kustomize pipelines/core-services/ | yq -o json '"core-services-" + .metadata.name' | jq -r)) -for pl_name in ${pl_names[@]}; do - if ! locate_in_all_namespaces pipeline "$pl_name"; then - has_missing_repo=yes - fi -done - -if [ -n "$has_missing_repo" ]; then - echo "Please contact Build team - #forum-konflux-build that the missing repos should be created in:" - echo "- https://quay.io/organization/konflux-ci" - exit 1 -else - echo "Done" -fi diff --git a/.tekton/scripts/create-task-pipeline-bundle-repos.sh b/.tekton/scripts/create-task-pipeline-bundle-repos.sh new file mode 100755 index 0000000000..2f6b9b7800 --- /dev/null +++ b/.tekton/scripts/create-task-pipeline-bundle-repos.sh @@ -0,0 +1,97 @@ +#!/usr/bin/bash + +set -o errexit +set -o pipefail +set -o nounset + +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +cd "$SCRIPTDIR/../.." || (echo "Failed to cd to '$SCRIPTDIR/../..'" && exit) + +CATALOG_NAMESPACES=( + konflux-ci/tekton-catalog +) + +locate_bundle_repo() { + local -r quay_namespace="$1" + local -r type="$2" + local -r object="$3" + + curl -I -s -L -w "%{http_code}\n" -o /dev/null "https://quay.io/v2/${quay_namespace}/${type}-${object}/tags/list" +} + +locate_in_all_namespaces() { + local -r type="$1" + local -r object="$2" + + for quay_namespace in "${CATALOG_NAMESPACES[@]}"; do + found=$(locate_bundle_repo "$quay_namespace" "$type" "$object") + + # konflux-ci/tekton-catalog + if [[ $quay_namespace = */* ]]; then + # tekton-catalog + quay_repo_prefix=${quay_namespace##*/} + # konflux-ci + quay_namespace=${quay_namespace%%/*} + else + quay_repo_prefix="" + fi + + echo "Checking ${quay_namespace}/${quay_repo_prefix}/${object}, http code: ${found}" + if [ "$found" != "200" ]; then + echo "Missing $type bundle repo: ${quay_repo_prefix}/${object} in ${quay_namespace}, creating..." + payload=$( + jq -n \ + --arg namespace "$quay_namespace" \ + --arg repository "$quay_repo_prefix/$object" \ + --arg visibility "public" \ + --arg description "" \ + '$ARGS.named' + ) + if ! err_msg=$(curl --oauth2-bearer "${QUAY_TOKEN}" "https://quay.io/api/v1/repository" --json "$payload" | jq '.error_message // empty'); + then + echo "curl returned an error when creating the repository. See the error above." # will this ever trigger??? + exit 1 + fi + + if [ -n "$err_msg" ]; then + echo "Quay returned an error when creating the repository: ${err_msg}" + exit 1 + fi + fi + done +} + +echo "Checking existence of task bundle repositories..." +echo + +# tasks +while IFS= read -r -d '' task_dir +do + if [ ! -f "$task_dir"/kustomization.yaml ]; then + # expected structure: task/${name}/${version}/${name}.yaml + task_name=$(basename "$(dirname "$task_dir")") + task_name=$(yq < "$task_dir/$task_name.yaml" .metadata.name) + else + task_name=$(oc kustomize "$task_dir" | yq .metadata.name) + fi + + locate_in_all_namespaces task "$task_name" +done < <(find task/*/*/ -maxdepth 0 -type d -print0) + +echo +echo "Checking existence of pipeline bundle repositories..." +echo + +# pipelines +pl_names=() +# Split by newlines into an array +while IFS=$'\n' read -r line; + do pl_names+=("$line"); +done <<<"$(oc kustomize pipelines/ | yq -o json '.metadata.name' | jq -r)" + +# Currently, only one pipeline for core services CI +pl_names+=("$(oc kustomize pipelines/core-services/ | yq -o json '"core-services-" + .metadata.name' | jq -r)") +for pl_name in "${pl_names[@]}"; do + echo "Checking pipeline: ${pl_name}" + locate_in_all_namespaces pipeline "$pl_name" +done