Skip to content

Instantly share code, notes, and snippets.

@zedtux
Last active January 25, 2019 11:54

Revisions

  1. zedtux revised this gist Jan 25, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gitlab-ci-create_secret-function.sh
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    echo "Creating the gitlab-registry secret ..."
    # Save the JSON to create the gitlab-registry docker-registry secret
    GITLAB_REGISTRY_SECRET_JSON=$(
    kubectl create secret --namespace=$KUBERNETES_NAMESPACE \
    kubectl create secret --namespace=$KUBE_NAMESPACE \
    docker-registry gitlab-registry \
    --docker-server="$CI_REGISTRY" \
    --docker-username="${CI_DEPLOY_USER:-$CI_REGISTRY_USER}" \
  2. zedtux revised this gist Jan 25, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gitlab-ci-create_secret-function.sh
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    docker-registry gitlab-registry \
    --docker-server="$CI_REGISTRY" \
    --docker-username="${CI_DEPLOY_USER:-$CI_REGISTRY_USER}" \
    --docker-password="${CI_DEPLOY_PASSWORD:-$CI_BUILD_TOKEN}" \
    --docker-password="${CI_DEPLOY_PASSWORD:-$CI_REGISTRY_PASSWORD}" \
    --docker-email="$GITLAB_USER_EMAIL" \
    --output json \
    --dry-run
  3. zedtux created this gist Jan 25, 2019.
    40 changes: 40 additions & 0 deletions gitlab-ci-create_secret-function.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    #
    # In order to get Kubernetes to be authorised to fetch the Docker images from
    # the private gitlab registry, we are creating a docker-registry secret.
    #
    function create_gitlab_registry_secret() {
    echo "Creating the gitlab-registry secret ..."
    # Save the JSON to create the gitlab-registry docker-registry secret
    GITLAB_REGISTRY_SECRET_JSON=$(
    kubectl create secret --namespace=$KUBERNETES_NAMESPACE \
    docker-registry gitlab-registry \
    --docker-server="$CI_REGISTRY" \
    --docker-username="${CI_DEPLOY_USER:-$CI_REGISTRY_USER}" \
    --docker-password="${CI_DEPLOY_PASSWORD:-$CI_BUILD_TOKEN}" \
    --docker-email="$GITLAB_USER_EMAIL" \
    --output json \
    --dry-run
    )

    # Extracts the auths key from the decoded JSON
    AUTHS=$(
    echo $GITLAB_REGISTRY_SECRET_JSON | \
    jq '.data[".dockerconfigjson"]' -r | \
    base64 -d | \
    jq '.auths' -c | \
    base64 | \
    tr -d '\n'
    )

    # Updates the GITLAB_REGISTRY_SECRET_JSON with the removed auths key
    # and replace the Kubernetes secret
    FINAL_JSON=$(
    echo $GITLAB_REGISTRY_SECRET_JSON | \
    sed -e s"/\".dockerconfigjson\":\s\"\([a-zA-Z0-9=]\+\)\"/\".dockerconfigjson\":\"$AUTHS\"/"
    )

    echo $FINAL_JSON | \
    kubectl replace --namespace=$KUBERNETES_NAMESPACE \
    --force \
    -f -
    }