Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS has been phased out. To see alternatives please check here

Skip to content
Snippets Groups Projects

repair CI form bug.

Merged Diego MENDEZ requested to merge feature/ci_form into main
4 files
+ 55
167
Compare changes
  • Side-by-side
  • Inline

Files

############################################################################
# This job needs a CI file with the name "NETBOX_LIST". JSON Format. e.g.:
# ```
# [
# {
# "netbox": "http://netbox.domain.com:8080",
# "token": "somenetboxtoken123456789",
# "description": "optinal_description_value"
# },
# {
# "netbox": "http://netbox.domain2.com:8082",
# "token": "somenetboxtoken123456789",
# "description": "testing_lab2"
# }
# ]
# ```
############################################################################
variables:
COMMIT_TAG:
value: $CI_COMMIT_TAG
options:
- $CI_COMMIT_TAG
- "test-deploy"
description: "Select tag 'test-deploy' to upload image to a Netbox. Make sure tag do not exists already on target."
NETBOX_URL:
value: "http://netbox.domain2.com:8080"
description: "Your target Netbox."
NETBOX_TOKEN:
value: "yourtestingtoken123456789"
description: "Token of your target Netbox."
.default_deploy_rules:
rules:
# if manually triggered from CI forms
- if: $COMMIT_TAG == "test-deploy"
when: on_success
# if not main branch
- if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
when: never
# if tagged
- if: $CI_COMMIT_TAG
when: on_success
deploy-job:
stage: deploy
variables:
REGISTRY: https://code.europa.eu:4567/v2/
REGISTRY_IMAGE_NETBOX: code.europa.eu:4567/digit-c4/netbox-plugins
REGISTRY_IMAGE: $CI_REGISTRY_IMAGE
before_script:
# print variables
- echo $REGISTRY_IMAGE
- echo $COMMIT_TAG
# requirements
- mkdir -p "$CI_PROJECT_DIR/opt_tmp/"
- sudo apt install jq -y
# decide list of netbox to target
- |
if [[ $COMMIT_TAG == "test-deploy" ]] ; then
# if running manually, then just prepare values from CI form.
echo '[{ "netbox": "'${NETBOX_URL%%[[:space:]]*}'", "token": "'${NETBOX_TOKEN%%[[:space:]]*}'" }]' > "$CI_PROJECT_DIR/opt_tmp/list.json"
else
# else, prepare full list of Netbox.
cat $NETBOX_LIST > "$CI_PROJECT_DIR/opt_tmp/list.json"
fi
script:
- |
# iterate over netbox's urls
for str1 in $(cat "$CI_PROJECT_DIR/opt_tmp/list.json" | jq -c '.[]')
do
# declare variables
NETBOX_URL=$(echo ${str1} | jq -r '.netbox')
NETBOX_TOKEN=$(echo ${str1} | jq -r '.token')
echo $NETBOX_URL
echo $NETBOX_TOKEN
# curl GET info from netbox
curl -X 'GET' "${NETBOX_URL}/api/plugins/docker/hosts/" \
-H 'accept: application/json; indent=4' \
-H "Authorization: Token ${NETBOX_TOKEN}" \
> "$CI_PROJECT_DIR/opt_tmp/res.json"
# iterate over VMs with a Netbox inside.
for str2 in $(cat "$CI_PROJECT_DIR/opt_tmp/res.json" | \
jq ".results[] | select(.images[].name | contains( \"${REGISTRY_IMAGE_NETBOX}\" )) | .id" | \
uniq)
do
# VMs with a netbox (host_id).
NETBOX_PLUGIN_HOST_ID="${str2}"
echo $NETBOX_PLUGIN_HOST_ID
# curl POST image to netbox
curl -X 'POST' "${NETBOX_URL}/api/plugins/docker/images/" \
-H 'accept: application/json; indent=4' \
-H 'Content-Type: application/json' \
-H "Authorization: Token ${NETBOX_TOKEN}" \
-d "{ \
\"name\": \"${REGISTRY_IMAGE}\", \
\"version\": \"${COMMIT_TAG}\", \
\"host\": \"${NETBOX_PLUGIN_HOST_ID}\", \
\"registry\": {\"serveraddress\": \"${REGISTRY}\"} \
}"
done
done
- echo "Ended."
after_script:
- rm -rf "$CI_PROJECT_DIR/opt_tmp/"
extends:
- .default_deploy_rules
Loading