0

我一直在尝试让一个简单的无服务器 ci/cd 工作。

在解决了很多问题并查看了gitlabktl代码(https://gitlab.com/gitlab-org/gitlabktl)之后,我设法得到了这个配置:

image: golang:latest

variables:
  NAMESPACE: company.com/infrastructure/security/authentication

stages:
  - test
  - build
  - deploy

test:
  stage: test
  script:
    - mkdir -p $GOPATH/src/$(dirname $NAMESPACE)
    - ln -svf $CI_PROJECT_DIR $GOPATH/src/$NAMESPACE
    - cd $GOPATH/src/$NAMESPACE
    - go get -u github.com/golang/dep/cmd/dep
    - dep ensure --vendor-only
    - go fmt $(go list ./... | grep -v /vendor/)
    - go vet $(go list ./... | grep -v /vendor/)
    - go test -race $(go list ./... | grep -v /vendor/)
  only:
    refs:
      - branches
      - tags

build:
  stage: build
  image: docker:git
  services:
    - docker:dind
  script:
    - version
    - build
  artifacts:
    paths:
      - .version
  only:
    refs:
      - branches
      - tags

deploy:staging:
  stage: deploy
  image: registry.gitlab.com/gitlab-org/gitlabktl:latest
  script:
    - deploy
  environment:
    name: staging
  only:
    refs:
      - branches
      - tags

# ---------------------------------------------------------------------------

.auto_devops: &auto_devops |
  export K8S_NAMESPACE="security-$CI_ENVIRONMENT_SLUG"
  if [ "$CI_ENVIRONMENT_SLUG" == "prod" ]; then
    export K8S_NAMESPACE="security"
  fi

  function version() {
    export VERSION=$(git describe --always)
    export VERSION="${VERSION/-g/.}"
    export VERSION="${VERSION/v-/.}"
    export VERSION="${VERSION/v/.}"

    echo "****************************************"
    echo "** VER --> $VERSION"
    echo "****************************************"

    export CONTAINER_IMAGE="$CI_REGISTRY_IMAGE:$VERSION"
    echo $VERSION > .version
  }

  function build() {
    echo "Building Docker image..."
    docker build -t $CONTAINER_IMAGE .

    if [[ -n "$CI_REGISTRY_USER" ]]; then
      echo "Logging to GitLab container registry with CI credentials..."
      docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
      echo ""
    fi

    echo "Pushing to GitLab container registry..."
    docker push "$CONTAINER_IMAGE"
    echo ""
  }

  function deploy() {
    /usr/bin/gitlabktl app deploy \
      --name authentication \
      --kubeconfig $KUBECONFIG \
      --image $CI_REGISTRY_IMAGE \
      --registry-username $CI_REGISTRY_USER \
      --registry-password $CI_REGISTRY_PASSWORD \
      --tag $(cat .version)
  }

before_script:
  - *auto_devops

但是,由于我不断收到此错误,我无法取得任何进一步的进展:

Welcome to GitLab CLI tool
time="2019-08-11T23:35:11Z" level=info msg="using registry" registry=registry.gitlab.com
time="2019-08-11T23:35:11Z" level=info msg="deploying registry credentials"
time="2019-08-11T23:35:12Z" level=fatal msg="could not deploy registry credentials" error=Unauthorized
4

0 回答 0