diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 285b964..a431299 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,7 +1,7 @@ # borrowed from https://gitlab.com/nix17/nixos-config/-/blob/main/.gitlab-ci.yml stages: - - flake + - misc - test - deploy - deploy_gitlab @@ -11,7 +11,7 @@ stages: # $PACKAGE_NAME = name of the flake that needs to be updated # $UPDATE_FLAKE = flag to update the flake update: - stage: flake + stage: misc tags: - nix # from https://forum.gitlab.com/t/git-push-from-inside-a-gitlab-runner/30554/5 @@ -40,6 +40,17 @@ update: variables: - $UPDATE_FLAKE == "yes" +sync_repos: + stage: misc + image: registry.gitlab.com/gitlab-ci-utils/curl-jq:2.0.0 + script: + - cd sync + - chmod +x ./sync.sh + - ./sync.sh + rules: + - changes: + - sync/repos.csv + .scripts_base: &scripts_base # load nix environment - git pull origin $CI_COMMIT_REF_NAME diff --git a/sync/.gitignore b/sync/.gitignore new file mode 100644 index 0000000..6b8afe3 --- /dev/null +++ b/sync/.gitignore @@ -0,0 +1,2 @@ +/.idea +.env \ No newline at end of file diff --git a/sync/README.md b/sync/README.md new file mode 100644 index 0000000..a9cae7d --- /dev/null +++ b/sync/README.md @@ -0,0 +1,2 @@ +# Repo Sync +This subdir is intended for syncing repos on with \ No newline at end of file diff --git a/sync/repos.csv b/sync/repos.csv new file mode 100644 index 0000000..25a4da2 --- /dev/null +++ b/sync/repos.csv @@ -0,0 +1,2 @@ +id_local, remote_url +4,https://gitlab.com/compsoc1/skynet/nixos.git diff --git a/sync/sync.sh b/sync/sync.sh new file mode 100644 index 0000000..7fbc83e --- /dev/null +++ b/sync/sync.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +Mirror-Clear(){ + # existing remotes + local response=$(curl -s -X "GET" "https://gitlab.skynet.ie/api/v4/projects/$1/remote_mirrors" --header "PRIVATE-TOKEN: $TOKEN") + + # https://stackoverflow.com/a/67638584 + readarray -t local my_array < <(jq -c '.[]' <<< $response) + # iterate through the Bash array + for item in "${my_array[@]}"; do + local id=$(jq --raw-output '.id' <<< "$item") + curl -s -X "DELETE" "https://gitlab.skynet.ie/api/v4/projects/$1/remote_mirrors/$id" --header "PRIVATE-TOKEN: $TOKEN" + done +} + +Mirror-Create(){ + # make sure the values are clean of extra characters + local ID=${1} + local REPO_TMP=$(tr -d '\n\t\r ' <<<"${2}" ) + local token=$(tr -d '\n\t\r ' <<<"$TOKEN" ) + local REPO=${REPO_TMP#"https://"} + + local body="url=https://oauth2:$token@$REPO&enabled=true&only_protected_branches=false&keep_divergent_refs=false" + local uri="https://gitlab.skynet.ie/api/v4/projects/$id/remote_mirrors" + echo $uri + local tmp="" + curl -sS -X "POST" "$uri" --header "PRIVATE-TOKEN: $token" --data $body > tmp +} + +Main() { + # for local dev + source .env + + while IFS="," read -r id remote + do + Mirror-Clear $id + Mirror-Create $id $remote + done < <(tail -n +2 ./repos.csv) +} + +Main \ No newline at end of file