Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
set -e
function replace_value(){
local key=$1
local value=$2
local file=$3
jq .$key="\"$value\"" $file > tmpfile && mv tmpfile $file
}
function update_repo(){
local repo=$1
local folder=$2
local target_branch=$3
local kube=$4
git clone --branch $target_branch --depth 3 https://$GITLAB_USER:$GITLAB_PASSWORD@$repo $folder
mkdir -p $folder/resources
cd $folder/resources
sops --encrypt ../../azure.json > azure.enc.json
if $kube
then
# Copy Kubeconfig
sops --encrypt $HOME/.kube/tp > kubeconfig.enc
fi
git checkout -b "ci-update-az-$(date +%Y-%m-%d)"
git add .
git commit -m "update azure k8s & Registry $(date +%Y-%m-%d)"
git push --set-upstream origin "infrastructure-az-$(date +%Y-%m-%d)" \
-o merge_request.create \
-o merge_request.target=$target_branch \
-o merge_request.title="Automatic update of infrastructure $(date +%Y-%m-%d)" \
-o merge_request.merge_when_pipeline_succeeds \
-o merge_request.remove_source_branch \
-o merge_request.label=infrastructure
cd ../..
}
SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})
ENVIRONMENTS_DIR=$SCRIPT_DIR/../environments/
ENVIRONMENT=${ENVIRONMENT:-staging}
cd "$ENVIRONMENTS_DIR$ENVIRONMENT" || exit
# Create template file
cat <<EOF > azure.json
{
"$ENVIRONMENT": {
"registry":{
"registry":"",
"user":"",
"password":""
},
"helm": {
"registry":"",
"user":"",
"password":""
}
}
}
EOF
git config --global user.email "runner@gitlab.com"
git config --global user.name "Your friendly Gitlab Runner"
AB_BRANCH=${AB_BRANCH:-azure}
INFRA_BRANCH=${INFRA_BRANCH:-master}
# Get Terraform output
REGISTRY_USER=$(terraform output registry_docker_user)
REGISTRY_PASSWORD=$(terraform output registry_docker_password)
REGISTRY_SERVER=$(terraform output registry_docker_server)
echo "got terraform output"
# Replace Values in template file
replace_value "$ENVIRONMENT.registry.registry" "$REGISTRY_SERVER" azure.json
replace_value "$ENVIRONMENT.registry.user" "$REGISTRY_USER" azure.json
replace_value "$ENVIRONMENT.registry.password" "$REGISTRY_PASSWORD" azure.json
replace_value "$ENVIRONMENT.helm.registry" "$REGISTRY_SERVER" azure.json
replace_value "$ENVIRONMENT.helm.user" "$REGISTRY_USER" azure.json
replace_value "$ENVIRONMENT.helm.password" "$REGISTRY_PASSWORD" azure.json
echo "replaced all values"
# Get Repo
sops --decrypt $CI_PROJECT_DIR/ci/gitlab.enc.json > gitlab.json
GITLAB_USER=$(cat gitlab.json | jq -r '.user')
GITLAB_PASSWORD=$(cat gitlab.json | jq -r '.password')
GITLAB_REPO=$(cat gitlab.json | jq -r '.repo')
GITLAB_INFRA=$(cat gitlab.json | jq -r '.infrarepo')
rm gitlab.json
update_repo $GITLAB_REPO production-code $AB_BRANCH true
update_repo $GITLAB_INFRA infra-helm-charts $INFRA_BRANCH false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment