feat: setup script to sync repos

Related to #50
This commit is contained in:
silver 2023-12-27 21:16:30 +00:00
parent 54529e0d21
commit 07601f708c
5 changed files with 60 additions and 2 deletions

View file

@ -1,7 +1,7 @@
# borrowed from https://gitlab.com/nix17/nixos-config/-/blob/main/.gitlab-ci.yml # borrowed from https://gitlab.com/nix17/nixos-config/-/blob/main/.gitlab-ci.yml
stages: stages:
- flake - misc
- test - test
- deploy - deploy
- deploy_gitlab - deploy_gitlab
@ -11,7 +11,7 @@ stages:
# $PACKAGE_NAME = name of the flake that needs to be updated # $PACKAGE_NAME = name of the flake that needs to be updated
# $UPDATE_FLAKE = flag to update the flake # $UPDATE_FLAKE = flag to update the flake
update: update:
stage: flake stage: misc
tags: tags:
- nix - nix
# from https://forum.gitlab.com/t/git-push-from-inside-a-gitlab-runner/30554/5 # from https://forum.gitlab.com/t/git-push-from-inside-a-gitlab-runner/30554/5
@ -40,6 +40,17 @@ update:
variables: variables:
- $UPDATE_FLAKE == "yes" - $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 .scripts_base: &scripts_base
# load nix environment # load nix environment
- git pull origin $CI_COMMIT_REF_NAME - git pull origin $CI_COMMIT_REF_NAME

2
sync/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/.idea
.env

2
sync/README.md Normal file
View file

@ -0,0 +1,2 @@
# Repo Sync
This subdir is intended for syncing repos on <gitlab.skynet.ie> with <gitlab.com>

2
sync/repos.csv Normal file
View file

@ -0,0 +1,2 @@
id_local, remote_url
4,https://gitlab.com/compsoc1/skynet/nixos.git
1 id_local remote_url
2 4 https://gitlab.com/compsoc1/skynet/nixos.git

41
sync/sync.sh Normal file
View file

@ -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