|
| 1 | +# SPDX-FileCopyrightText: 2023 German Aerospace Center (DLR), Forschungszentrum Jülich, Helmholtz-Zentrum Dresden-Rossendorf |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: CC0-1.0 |
| 4 | + |
| 5 | +# "Call-back" to create archive |
| 6 | +.hermes_create_deposition: &hermes_create_deposition |
| 7 | + - git fetch origin "$MR_TARGET_BRANCH" |
| 8 | + - git archive --format zip "origin/$MR_TARGET_BRANCH" {%deposit_zip_files%} > $HERMES_ARCHIVE |
| 9 | + |
| 10 | +# Basic building blocks of hermes jobs |
| 11 | +# - Set up git with a valid author and configure URL to allow pushes. |
| 12 | +.hermes_prepare_git: &hermes_prepare_git |
| 13 | + - git config user.name "$GITLAB_USER_NAME" |
| 14 | + - git config user.email "$GITLAB_USER_EMAIL" |
| 15 | + - git remote set-url origin "${CI_SERVER_PROTOCOL}://__token__:${HERMES_PUSH_TOKEN}@${CI_SERVER_HOST}:${CI_SERVER_PORT}/${CI_PROJECT_PATH}.git" |
| 16 | + |
| 17 | +# - Switch to new MR_SOURCE_BRANCH. |
| 18 | +.hermes_switch_branch: &hermes_switch_branch |
| 19 | + - git branch "$MR_SOURCE_BRANCH" "origin/$MR_TARGET_BRANCH" |
| 20 | + - git switch "$MR_SOURCE_BRANCH" |
| 21 | + |
| 22 | +# - Commit changes and create a matching merge request. |
| 23 | +.hermes_create_merge_request: &hermes_create_merge_request |
| 24 | + - | |
| 25 | + if git diff --cached --quiet; then |
| 26 | + echo "No staged changes. Skipping MR creation." |
| 27 | + else |
| 28 | + git commit -m "$MR_COMMIT_MESSAGE" |
| 29 | + git push origin "$MR_SOURCE_BRANCH" \ |
| 30 | + -o merge_request.create \ |
| 31 | + -o merge_request.target="$MR_TARGET_BRANCH" \ |
| 32 | + -o merge_request.title="$MR_TITLE" \ |
| 33 | + -o merge_request.description="$MR_DESCRIPTION" \ |
| 34 | + -o merge_request.remove_source_branch |
| 35 | + fi |
| 36 | + |
| 37 | +# - Delete all branches created by hermes for curation. |
| 38 | +.hermes_cleanup_branches: &hermes_cleanup_branches |
| 39 | + - | |
| 40 | + for BRANCH in $(git ls-remote origin 'refs/heads/hermes/curate-*' | cut -f2 | cut -d'/' -f'3-'); do |
| 41 | + git push origin --delete "$BRANCH" |
| 42 | + done |
| 43 | + |
| 44 | +# Base job template for hermes |
| 45 | +.hermes_job: |
| 46 | + stage: deploy |
| 47 | + image: python:3.10 |
| 48 | + cache: |
| 49 | + key: hermes |
| 50 | + paths: |
| 51 | + - .hermes-env |
| 52 | + before_script: |
| 53 | + - *hermes_prepare_git |
| 54 | + - | |
| 55 | + if test ! -d .hermes-env |
| 56 | + then |
| 57 | + python -m venv .hermes-env |
| 58 | + fi |
| 59 | + - . .hermes-env/bin/activate |
| 60 | + - {%pip_install_hermes%} |
| 61 | +{%pip_install_plugins_gitlab%} |
| 62 | + tags: |
| 63 | + - docker |
| 64 | + artifacts: |
| 65 | + paths: [".hermes/", "hermes.log"] |
| 66 | + when: always |
| 67 | + |
| 68 | +# hermes metadata preparation - this job creates a MR that will trigger deposition when merged. |
| 69 | +.hermes_curate: |
| 70 | + extends: |
| 71 | + - .hermes_job |
| 72 | + variables: |
| 73 | + MR_BASE_REF: "origin/$CI_COMMIT_BRANCH" |
| 74 | + MR_SOURCE_BRANCH: hermes/curate-result-$CI_COMMIT_SHORT_SHA |
| 75 | + MR_TARGET_BRANCH: hermes/curate-$CI_COMMIT_SHORT_SHA |
| 76 | + MR_COMMIT_MESSAGE: "[hermes] Add metadata for curation" |
| 77 | + MR_TITLE: Metadata Curation for Commit $CI_COMMIT_SHORT_SHA |
| 78 | + MR_DESCRIPTION: >- |
| 79 | + Please carefully review the attached metadata. |
| 80 | + If you are satisfied with the result, you may merge this MR, which will trigger publication. |
| 81 | + (Any temporary branches will be cleaned up.) |
| 82 | + rules: |
| 83 | + - exists: |
| 84 | + - .hermes/curate/target_branch |
| 85 | + when: never |
| 86 | + - if: $CI_COMMIT_TITLE =~ /^Merge branch 'hermes\/post-[0-9a-f]{8}' into/ |
| 87 | + when: never |
| 88 | + - if: $CI_PIPELINE_SOURCE == "push" && {%gl_push_condition%} |
| 89 | + - when: never |
| 90 | + script: |
| 91 | + # Run hermes pipeline |
| 92 | + - hermes harvest |
| 93 | + - hermes process |
| 94 | + - hermes curate |
| 95 | + |
| 96 | + # Store branch & commit ref for post-processing |
| 97 | + - | |
| 98 | + if [ -z "$MR_POST_BRANCH" ] |
| 99 | + then |
| 100 | + if [ ! -z "$CI_COMMIT_BRANCH" ] |
| 101 | + then |
| 102 | + MR_POST_BRANCH=$CI_COMMIT_BRANCH |
| 103 | + else |
| 104 | + MR_POST_BRANCH="" |
| 105 | + fi |
| 106 | + fi |
| 107 | + echo $MR_POST_BRANCH > .hermes/curate/target_branch |
| 108 | + echo $CI_COMMIT_REF_NAME > .hermes/curate/commit_ref |
| 109 | + |
| 110 | + # Create target branch for curation merge |
| 111 | + - {%gl_create_curate_branch|default('git branch "$MR_TARGET_BRANCH" "$MR_BASE_REF"', true)%} |
| 112 | + - git push origin "$MR_TARGET_BRANCH" |
| 113 | + |
| 114 | + # Create curation branch and merge request |
| 115 | + - *hermes_switch_branch |
| 116 | + - git add -f .hermes/curate |
| 117 | + - *hermes_create_merge_request |
| 118 | + |
| 119 | +# hermes deposition and post-processing - this job creates a MR to re-integrate the results of post-processing. |
| 120 | +.hermes_deposit: |
| 121 | + stage: deploy |
| 122 | + extends: |
| 123 | + - .hermes_job |
| 124 | + variables: |
| 125 | + HERMES_ARCHIVE: {%deposit_zip_name%} |
| 126 | + MR_SOURCE_BRANCH: hermes/post-$CI_COMMIT_SHORT_SHA |
| 127 | + MR_COMMIT_MESSAGE: "[hermes] Add post-processing results" |
| 128 | + MR_TITLE: Review hermes post-processing results |
| 129 | + MR_DESCRIPTION: >- |
| 130 | + This is an automated pull request created by HERMES post-processing. |
| 131 | + Please carefully review the changes and finally merge them into your |
| 132 | + rules: |
| 133 | + - if: $CI_PIPELINE_SOURCE != "push" || $CI_COMMIT_BRANCH !~ /^hermes\/curate-[0-9a-f]{8}/ |
| 134 | + when: never |
| 135 | + - exists: |
| 136 | + - .hermes/curate/target_branch |
| 137 | + script: |
| 138 | + # Restore target branch |
| 139 | + - MR_TARGET_BRANCH="$(cat .hermes/curate/target_branch)" |
| 140 | + |
| 141 | + # Get target branch from tag ref if empty |
| 142 | + - | |
| 143 | + # Test if empty and .hermes/curate/commit_ref exists |
| 144 | + if [ -z "$MR_TARGET_BRANCH" ] && [ -s .hermes/curate/commit_ref ]; then |
| 145 | + # Get default branch |
| 146 | + DEFAULT_BRANCH="$CI_DEFAULT_BRANCH" |
| 147 | + |
| 148 | + # Take tag ref from .hermes/curate/commit_ref |
| 149 | + REF="$(tr -d '\r\n' < .hermes/curate/commit_ref)" |
| 150 | + TAG="${REF#refs/tags/}" |
| 151 | + |
| 152 | + # Get commit |
| 153 | + git fetch --force --tags origin '+refs/heads/*:refs/remotes/origin/*' >/dev/null 2>&1 || true |
| 154 | + TAG_COMMIT="$(git rev-parse "${TAG}^{commit}" 2>/dev/null)" || exit 0 |
| 155 | + |
| 156 | + # Check if the default or another branch contains the tag commit |
| 157 | + if git merge-base --is-ancestor "$TAG_COMMIT" "origin/$DEFAULT_BRANCH"; then |
| 158 | + MR_TARGET_BRANCH="$DEFAULT_BRANCH" |
| 159 | + else |
| 160 | + BRANCH_REF="$( |
| 161 | + git for-each-ref --contains "$TAG_COMMIT" --format='%(refname:short)' refs/remotes/origin \ |
| 162 | + | grep -vE '^origin/(hermes/|HEAD$)' \ |
| 163 | + | head -n1 |
| 164 | + )" |
| 165 | + if [ -n "$BRANCH_REF" ]; then |
| 166 | + MR_TARGET_BRANCH="${BRANCH_REF#refs/heads/}" |
| 167 | + fi |
| 168 | + fi |
| 169 | + fi |
| 170 | + |
| 171 | + {%% if deposit_parameter_zip_file %%} |
| 172 | + # Invoke callback to create {%deposit_zip_name%} and publish |
| 173 | + - *hermes_create_deposition |
| 174 | + {%% endif %%} |
| 175 | + # Call deposit to publish |
| 176 | + - hermes deposit {%deposit_initial%} {%deposit_parameter_token%} "${%deposit_token_name%}" {%deposit_parameter_zip_file%} {%deposit_extra_files%} |
| 177 | + |
| 178 | + # Skip on missing target branch |
| 179 | + - | |
| 180 | + if [ -z "$MR_TARGET_BRANCH" ]; then |
| 181 | + echo "MR_TARGET_BRANCH is empty. Skipping postprocess." |
| 182 | + exit 0 |
| 183 | + fi |
| 184 | + |
| 185 | + # Prepare clean branch for post-processing and run it |
| 186 | + - *hermes_switch_branch |
| 187 | + - hermes postprocess |
| 188 | + |
| 189 | + # Put all changes into a merge request (all but .hermes-env) |
| 190 | + - git add . |
| 191 | + - git reset .hermes-env |
| 192 | + - *hermes_create_merge_request |
| 193 | + after_script: |
| 194 | + - *hermes_cleanup_branches |
| 195 | + |
| 196 | +.hermes_cleanup: |
| 197 | + script: |
| 198 | + - *hermes_cleanup_branches |
0 commit comments