Deploy to Kubernetes

4 minute read

Create namespaces, apply resource files, and deploy Kustomize configurations to Kubernetes clusters using CloudBees Kubernetes actions.

Create a Kubernetes namespace

Use the cloudbees-io/create-k8s-namespace action to create a namespace that partitions a Kubernetes cluster into separate scopes.

All CloudBees action repositories are listed at CloudBees, Inc. on GitHub.

Inputs

Table 1. Input details
Input name Data type Required? Description

name

String

Yes

The namespace name.

sanitize-name

Boolean

No

Whether to sanitize the namespace name. Default is true. When value is true, the name value is truncated, suffixed with a hash, and validated.

labels

String

Yes

The YAML object of labels used to create the namespace.

Usage examples

The following examples show how to create a namespace standalone and how to use a namespace to scope a Helm chart installation.

Basic example

- id: createns name: Create Kubernetes namespace uses: https://github.com/cloudbees-io/create-k8s-namespace@v1 with: name: 'ns_name' sanitize-name: '' labels: | example.org/mylabel: xyz

Create a namespace and install a Helm chart into it

Create a Kubernetes namespace, then specify that namespace when installing a Helm chart to scope the installation to the cluster partition.

- id: nshelm name: Create Kubernetes namespace uses: https://github.com/cloudbees-io/create-k8s-namespace@v1 with: name: 'k8s_ns_name' sanitize-name: '' labels: | example.org/mylabel: xyz - name: Install Helm chart uses: https://github.com/cloudbees-io/helm-install@v1 with: namespace: ${{ steps.nshelm.outputs.name }}

The namespace input accepts the name of the namespace resource. Refer to Deploy with Helm for full Helm action details.

Create or update a Kubernetes resource file

Use the cloudbees-io/kubernetes-create-resource action to create or update a Kubernetes resource file, which groups multiple resources for your application into a single YAML file.

All CloudBees action repositories are listed at CloudBees, Inc. on GitHub.

Inputs

Table 2. Input details
Input name Data type Required? Description

file-path

String

Yes

The path of the Kubernetes resource file to be created or updated.

environment-variables

JSON

No

Kubernetes environment variables, formatted as JSON data in key/value pairs.

environment-variables-path

String

Required only if environment-variables is not specified.

The path of the file that contains the environment variables.

namespace

String

No

The namespace where the created or updated Kubernetes resource file is located.

Usage example

In your YAML file, add:

- name: Create or update a Kubernetes resource uses: https://github.com/cloudbees-io/kubernetes-create-resource@v1 with: file-path: ${{ cloudbees.workspace }}/input.yml environment-variables: '{"IMAGE_NAME":"nginx:latest","CUSTOM_VALUE":"alpine:latest","port":"80"}' environment-variables-path: ${{ cloudbees.workspace }}/sample.json namespace: my-namespace

Deploy with Kustomize

Use the cloudbees-io/kustomize-deploy action to customize Kubernetes configurations using overlays, which modify base YAML manifests without directly editing them.

This action requires AWS credentials to be configured before use. Add the following step before invoking Kustomize in your YAML file:
- name: Configure AWS credentials uses: https://github.com/cloudbees-io/configure-aws-credentials@v1 with: aws-access-key-id: ${{ secrets.AWS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }} aws-region: ap-south-1
All CloudBees action repositories are listed at CloudBees, Inc. on GitHub.

Inputs

Kustomize ConfigMap literals and files are not encrypted, so store only non-confidential data with kustomize-configmap-literal and kustomize-configmap-file. To store data that must be encrypted, use kustomize-secret-file.
Table 3. Input details
Input name Data type Required? Description

kustomization-base-dir

String

Yes

Set the path to the Kustomize base directory.

kustomization-overlays-dir

String

Yes

Set the path to the Kustomize overlay directory.

kustomize-annotation

JSON

No

Add, update, or remove Kustomize annotations. Valid objects are add, set, and remove.

kustomize-label

JSON

No

Add, update, or remove Kustomize labels. Valid objects are add, set, and remove.

kustomize-configmap-literal

JSON

No

Refer to non-sensitive data as literals on a ConfigMap resource. Valid value is add.

kustomize-configmap-file

JSON

No

Refer to non-sensitive data as files on a ConfigMap resource. Valid value is add.

kustomize-secret-file

JSON

No

Refer to sensitive data as secrets files on a Secrets resource. Valid value is add.

kustomize-buildmetadata

JSON

No

Specify build information options in Kustomize labels and annotations. Valid options are originAnnotations, managedByLabel, and transformerAnnotations.

kustomize-image

JSON

No

Set container image names, tags, and/or digests without creating patches. Valid options are name, newName, and newTag.

kustomize-nameprefix

String

No

Add a prefix to names in defined YAML files.

kustomize-namesuffix

String

No

Add a suffix to names in defined YAML files. The suffix is appended before the content hash if the resource type is ConfigMap or Secret.

kustomize-namespace

String

No

Add a namespace, or override an existing namespace, on a resource.

kustomize-replicas

JSON

No

Set the number of replicas for a resource.

Usage examples

The following example adds annotations, updates existing labels, updates the build metadata options, overrides the namespace, and adds a custom prefix.

- id: k8s-kustomize-deploy name: Add Kustomize labels and annotations uses: https://github.com/cloudbees-io/kustomize-deploy@v2 with: kustomization-base-dir: ${{ cloudbees.workspace }}/testdata/base kustomization-overlays-dir: ${{ cloudbees.workspace }}/testdata/overlays/dev kustomize-annotation: '{"add":{"nam1":"val1","nam2":"val2"}}' kustomize-label: '{"set":{"nam1":"val1","nam2":"val2"}}' kustomize-buildmetadata: '{"add":["originAnnotations"],"set":["managedByLabel"],"remove":["transformerAnnotations"]}' kustomize-namespace: 'dev' kustomize-nameprefix: 'dev-'

The following example stores non-sensitive data as literals and files in a ConfigMap, and sets container image information without creating patches.

- id: k8s-kustomize-deploy name: Store non-sensitive data uses: https://github.com/cloudbees-io/kustomize-deploy@v2 with: kustomization-base-dir: ${{ cloudbees.workspace }}/testdata/base kustomization-overlays-dir: ${{ cloudbees.workspace }}/testdata/overlays/dev kustomize-configmap-literal: '{"name":"example-config","add":{"nam1":"val1","nam2":"val2"}}' kustomize-configmap-file: '{"name":"my-config","add":"config.properties"}' kustomize-image: '{"name":"nginx:1.7.9","newName":"nginx","newTag":"1.7.9-alpine"}'

The following example stores sensitive data encrypted as Secrets and sets the number of replicas for a resource.

- id: k8s-kustomize-deploy name: Store secrets and set replicas uses: https://github.com/cloudbees-io/kustomize-deploy@v2 with: kustomization-base-dir: ${{ cloudbees.workspace }}/testdata/base kustomization-overlays-dir: ${{ cloudbees.workspace }}/testdata/overlays/dev kustomize-secret-file: '{"name":"my-secret","namespace":"dev","add":"secrets.properties"}' kustomize-replicas: '["hello:5"]'