Kubectl: command not found on travis ci

3/10/2019

I am trying to deploy a kubernetes cluster using Travis CI and I get the following error

EDIT:

invalid argument "myAcc/imgName:" for t: invalid reference format

See docker build --help

./deploy.sh: line 1: kubectl: command not found

This is my travis config file

travis.yml

sudo: required
services: 
  - docker
env:
  global:
    - SHA-$(git rev-parse HEAD)
    - CLOUDSDK_CORE_DISABLE_PROMPTS=1
before-install:
  - openssl aes-256-cbc -K $encrypted_0c35eebf403c_key -iv $encrypted_0c35eebf403c_iv -in service-account.json.enc -out service-account.json -d
  - curl https://sdk.cloud.google.com | bash > /dev/null
  - source $HOME/google-cloud-sdk/path.bash.inc
  - gcloud components update kubectl
  - gcloud auth activate-service-account --key-file service-account.json
  - gcloud config set project robust-chess-234104
  - gcloud config set compute/zone asia-south1-a
  - gcloud container clusters get-credentials standard-cluster-1
  - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin

deploy:
  provider: script
  script: bash ./deploy.sh
  on: 
    branch: master

This is my deploy script

deploy.sh

doccker build -t myAcc/imgName:$SHA
docker push myAcc/imgName:$SHA
kubectl apply -k8s

I guess the gcloud components update kubectl command is not working. Any ideas?

Thanks !

-- Shankar Thyagarajan
docker
gcloud
kubernetes
travis-ci

1 Answer

3/10/2019

The first issue invalid argument "myAcc/imgName:" for t: invalid reference format because the variable $SHA is not defined as expected. There is a syntax issue with defining the variable you should use = instead of - after SHA, so it should be like this:

- SHA=$(git rev-parse HEAD)

The second issue which is related to kubectl you need to install it using the following command according to the docs:

gcloud components install kubectl

Update:

After testing this file on Travis-CI I was able to figure out the issue. You should use before_install instead of before-install so in your case the before installation steps never get executed.

# travis.yml
---
env:
  global:
    - CLOUDSDK_CORE_DISABLE_PROMPTS=1
before_install:
 - curl https://sdk.cloud.google.com | bash > /dev/null
 - source $HOME/google-cloud-sdk/path.bash.inc
 - gcloud components install kubectl

script: kubectl version

And the final part of the build result:

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.7", GitCommit:"65ecaf0671341311ce6aea0edab46ee69f65d59e", GitTreeState:"clean", BuildDate:"2019-01-24T19:32:00Z", GoVersion:"go1.10.7", Compiler:"gc", Platform:"linux/amd64"}
-- Mostafa Hussein
Source: StackOverflow