How to use Bitbucket pipeline to deploy to Oracle Container Engine For Kubernetes(OKE) with OCI CLI Container Image [Aug 2022]

·

2 min read

In this post, we will see how to connect Oracle Container Engine for Kubernetes(OKE) from Bitbucket with help of OCI CLI Container Image.

Recently, Oracle released OCI CLI image that we can use for this purpose. documentation

I have previously written post on how we can create our own image and use it. Check here

For kubectl, we are using commands provided in k8s documentation. Lastly, creating empty config file ~/.oci/config and providing entrypoint.

image:
  name: ghcr.io/oracle/oci-cli:latest
pipelines:
  default:
    - step:
        script:
           - cd /oracle/ && mkdir .oci
           - (umask  077 ; echo $OCI_CLI_KEY | base64 --decode > ~/.oci/oci_api_key.pem)
           - echo -e " [DEFAULT]\ntenancy=$OCI_CLI_TENANCY\nregion=$OCI_CLI_REGION\nuser=$OCI_CLI_USER\nfingerprint=$OCI_CLI_FINGERPRINT \nkey_file=~/.oci/oci_api_key.pem" | tee  ~/.oci/config
           - oci setup repair-file-permissions --file ~/.oci/config
           - mkdir -p $HOME/.kube
           - oci ce cluster create-kubeconfig --cluster-id ocid1.cluster.oc1.iad.aaaaaaaaae4wkobxgfrdgobzgq2damtegrtdmyrzgvswemzvmc2wkmzzgezg --file $HOME/.kube/config --region us-ashburn-1 --token-version 2.0.0
           - export KUBECONFIG=$HOME/.kube/config
           - curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
           - chmod +x kubectl
           - mkdir -p ~/.local/bin
           - mv ./kubectl ~/.local/bin/kubectl
           - export PATH="~/.local/bin:$PATH"
           - kubectl get nodes

In this above YAML file inside script we are setting up OCI CLI by exporting required variable. One of those variable is key_file which we are setting up by using base64 encode of private key and then decoding in our script.

Main purpose of installing OCI CLI is to use it for downloading kubeconfig file for our k8s cluster.

After setting up config file for OCI, we are installing kubectl. Since docker image is using Oracle user we don't have sudo privilege and we need to install kubectl as non-root user, check here for more info.

Finally, we can run our command to check if we can access OKE cluster.

To set variables go to repository settings in Bitbucket and then click on Repository variables.

SCR-20220811-d12.png