12

I am searching for a way to use kubectl in gitlab.

So far I have the following script:

deploy_to_dev:
  stage: deploy
  image: docker:dind
  environment:
    name: dev
  script:
    - mkdir -p $HOME/.kube
    - echo $KUBE_CONFIG | base64 -d > $HOME/.kube/config
    - kubectl config view
  only:
    - develop

But it says that gitlab does not know kubectl. So can you point me in the right direction.

2 Answers 2

21

You are using docker:dindimage which does not have the kubectl binary, you should bring your own image with the binary or download it in the process

deploy_to_dev:
  stage: deploy
  image: alpine:3.7
  environment:
    name: dev
  script:
    - apk update  && apk add --no-cache curl
    - curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
    - chmod +x ./kubectl && mv ./kubectl /usr/local/bin/kubectl
    - mkdir -p $HOME/.kube
    - echo -n $KUBE_CONFIG | base64 -d > $HOME/.kube/config
    - kubectl config view
  only:
    - develop
6
9

Use image google/cloud-sdk which has a preinstalled installation of gcloud and kubectl.

build:
  stage: build
  image: google/cloud-sdk
  services:
  - docker:dind
  script:
  # Make gcloud available
  - source /root/.bashrc
1
  • 1
    There are also some images with just kubectl installed like bitnami/kubectl rancher/kubectl ibmcom/kubectl ...
    – HOERNSCHEN
    Nov 4, 2021 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.