Skip to content

kameshsampath/kn-service-action

Repository files navigation

Knative Client Service Action

CI

GitHub Action to deploy Knative Service using Knative Client.

Pre-requisites

Kubernetes Cluster with Knative, if you dont have an OpenShift cluster get one spinned instantly via try.openshift.com.

Parameters

The action has the following parameters:

Table 1. Inputs
Name Description Default

service_name*

The Knative Service Name

service_namespace

The Kubernetes Namespace to deploy to

default

service_operation

The kn service operation create, update, delete etc.,

create

container_image*

The container image to use for service

service_params

The extra service parameters to pass to the service

private_registry

Whether the image is pushed to private registry. Possible values yes or no

yes

registry_user

The registry user to use to create image pull secret.Required if private_registry is yes.

registry_password

The registry user credentials. Required if private_registry is yes.

Marked * are required attributes

Table 2. Outputs
Name Description

service_url

Knative Service URL of the service created

Note

When private_registry is yes, then the action will create a Kubernetes Secret of type docker-registry with the docker-username to be registry_user and docker-password to be registry_password. The docker-server value will be the first part of the container_image value.

The secret will be created in service_namespace with name pattern like docker-server.pull-secret.

How to add to your action

  • Create the following GitHub Secrets in your GitHub repository:

  • Container registry access e.g GitHub Container Registry where you push built images.

As the examples show using GitHub Container registry create the following secret and add to your repository:

GHCR_PAT - The GithHub PAT to allow pushing built image into GitHub Container registry .

If you are using other Container registries you might need to add extra secrets to your repository typically for your registry username and password.

As OpenShift is used as the Kubernetes platform in the example, you can use the oc-login action,to login into the OpenShift cluster to perform kn actions.

# Login into the OpenShift cluster with your OpenShift username
# and password
- name: Login into OpenShift Cluster
  id: oc_login
  uses: redhat-actions/oc-login@v1
  with:
     openshift_server_url: ${{ secrets.OPENSHIFT_SERVER }}
     openshift_username: ${{ secrets.OPENSHIFT_USERNAME }}
     openshift_password: ${{ secrets.OPENSHIFT_PASSWORD }}
# With successful login in previous step, do the kn action
- name: Knative Service Deploy
  id: kn_service_deploy
  uses: kameshsampath/kn-service-action
  with:
    service_name: fruits-app
    service_namespace: demos
    container_image: "ghcr.io/kameshsampath/fruits-app@${{ steps.docker_build_push.outputs.digest }}"
    registry_user: "${{ github.repository_owner }}"
    registry_user: "${{ secrets.GHCR_PAT }}"

The above action configuration will be interpreted in to kn command like:

 kn service create fruits-app \
  --namespace=demos \
  --image=ghcr.io/kameshsampath/fruits-app@sha256:b38df3a060b6eb5c9cb4d4fba4deecba4abc5f97f6db0f5d6a04109bf1e8fe79 \
 --pull-secret=ghcr.io.pull-secret

Passing extra service arguments

The service action provides basic options such namespace, service name, image and operation to be configured. There might be cases where you might want to pass extra arguments to the kn service <command>, in those cases you can use service_params as shown:

Consider an example that you want to add --max-scale=5 and --min-scale=1, then your action snippet will be:

- name: Knative Service Deploy
  id: kn_service_deploy
  uses: kameshsampath/kn-service-action
  with:
    service_name: fruits-app
    service_namespace: demos
    private_registry: no
    container_image: "docker.io/kameshsampath/fruits-app@${{ steps.docker_build_push.outputs.digest }}"
    service_params: >
      --max-scale=5
      --min-scale=1

The above action configuration will be interpreted in to kn command like:

 kn service create fruits-app --namespace=demos --image=docker.io/kameshsampath/fruits-app@sha256:b38df3a060b6eb5c9cb4d4fba4deecba4abc5f97f6db0f5d6a04109bf1e8fe79 --max-scale=5 --min-scale=1

For a complete action example check kn-service-action-example.