Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Run two kubectl commands in a pipe, having output of the first one as an input for the second

Kostya Kostyushko May 16, 2021

For apply / replace configMap I need to run two kubectl commands in the following way:

 

kubectl create configmap sb-products-files --from-file=configmap_files/ \

   -o yaml --dry-run=client | kubectl replace --namespace=myNamespace -f -

Basically, I need to run one command, and it's output send as input to another command.

Is there any way to accomplish it in bitbucket pipeline?

2 answers

0 votes
Ziga Gregoric November 15, 2021

I've got similar issue. I need to execute `kubectl` to generate a manifest file on the fly. 

kubectl create configmap foo --from-file=foo.txt -o yaml --dry-run > foo.yaml

Using atlassian/kubectl-run would require me to configure and provide KUBE_CONFIG (because it's mandatory for that pipe) which I'd like to avoid. 

- pipe: atlassian/kubectl-run:3.1.2
  variables:
    KUBE_CONFIG: '<string>'
    KUBECTL_COMMAND: '<string>'

So I ended up adding an extra step with custom image to generate output file which I pass to the next step through as an artifact.

- step: 
name: 'Create ConfigMap'
image: bitnami/kubectl
artifacts:
- foo.yaml
script:
- kubectl create configmap foo --from-file=foo.txt -o yaml --dry-run > foo.yaml

For such cases, lifting requirement for mandatory KUBE_CONFIG in the official pipe would be practical (or to add `kubectl` to atlassian/default-image).

0 votes
Halyna Berezovska
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 21, 2021

@Kostya Kostyushko you could execute create configmap firstly and save it to file, and then run a pipe with RESOURCE_PATH equal to that file you saved output to

Regards, Galyna

Kostya Kostyushko May 21, 2021

Can you give examples please? I tried to make something, but it didn't work.

Halyna Berezovska
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 25, 2021

@Kostya Kostyushko I meant somethong like

script:
- kubectl ... > output.yml
- pipe: atlassian/kubectl-run:2.0.0
variables:
...
RESOURCE_PATH: 'output.yaml'

The location of your resource file can vary depending on your requirements and project structure.

Regards, Galyna

Kostya Kostyushko June 20, 2021
bash: kubectl: command not found

 

I'm using

Images used:    build : docker.io/atlassian/default-image
Kostya Kostyushko June 20, 2021

Ok. I found.
For those who need to run multiple kubectl commands
this will be a good solution:

 

      - step:
          # trigger: manual
          name: Deploy to Kubernetes
          image: atlassian/pipelines-kubectl
          script:
            # NOTE: $KUBECONFIG is secret stored as a base64 encoded string
            # Base64 decode our kubeconfig file into a temporary kubeconfig.yml file (this will be destroyed automatically after this step runs)
            - echo $KUBECONFIG | base64 -d > kubeconfig.yml
            # Tell our Kubernetes deployment to use the new Docker image tag
            - kubectl --kubeconfig=kubeconfig.yml --namespace=<namespace> set image deployment/<deployment-name> <deployment-name>=<docker-username>/<docker-image>:$BITBUCKET_COMMIT

 

by using  image: atlassian/pipelines-kubectl

Like guik likes this
Halyna Berezovska
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 25, 2021

@Kostya Kostyushko yeap, you can use also kubectl official version, whatever is suitable for you

In the future we may think about suppoorting output sharing from pipe execution, so that you can execute pipes sequentially passing output from previous execution

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events