Commit c6d9bde9 authored by Thomas Boni's avatar Thomas Boni
Browse files

Merge branch '404-new-job-kustomize_deploy' into latest

parents 313dd11a 77a10532
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -346,3 +346,6 @@ templating
Templating
auths
severities
Kustomize
namespace
kubeconfig
+47 −0
Original line number Diff line number Diff line
## Objective

This job will deploy manifests to your cluster using Kustomize and set a new image for the deployment, the cluster can be either linked to Gitlab or no.

## How to use it

1. Make sure that you have a Kubernetes cluster, you have two options:
      1. Use a linked cluster with your Gitlab project, get more info [here](https://docs.gitlab.com/ee/user/project/clusters/){:target="_blank"}
      1. Provide a kubeconfig file through the variable `KUBECONFIG`, check [THIS](https://docs.gitlab.com/ee/ci/variables/#cicd-variable-types){:target="_blank"} guide to see how
1. Add this job URL inside the `include` list of your `.gitlab-ci.yml` file (see the [quick setup](/use-the-hub/#quick-setup)). You can specify [a fixed version](#changelog) instead of `latest`.
    ```yaml
      - remote: 'https://jobs.r2devops.io/latest/kustomize_deploy.yml'
    ```
1. Set the mandatory variables for your job, check [**Variables**](#variables)
1. If you need to customize the job (stage, variables, ...) 👉 check the [jobs
   customization](/use-the-hub/#jobs-customization)
1. Well done, your job is ready to work ! 😀

## Job details

* Job name: `kustomize_deploy`
* Docker image:
[`line/kubectl-kustomize:1.21.1-4.1.3`](https://hub.docker.com/r/line/kubectl-kustomize)
* Default stage: `deploy`
* When: `manual`, only when running on default branch (`$CI_DEFAULT_BRANCH`).  
  To update this behavior, see [job customization](https://r2devops.io/use-the-hub/#global) to override [`rules`](https://docs.gitlab.com/ee/ci/yaml/#rulesif)

### Variables

!!! important
    There are some mandatory variables that **you have to provide**, the most important one being `$KUBECONFIG`, you can do that by passing them as [CI/CD variables](https://docs.gitlab.com/ee/ci/variables/#cicd-variable-types){:target="_blank"} so that your data can stay confidential.

| Name | Description | Mandatory | Default |
| ---- | ----------- | --------- | ------- |
| `PROJECT_ROOT` | path to the root of the project | no | `.`
| `KUBECONFIG` | the config file for kubectl | yes | ` `
| `KUSTOMIZATION_DIR` | the folder that contains the `kustomization.yaml` file | yes | ` `
| `CHANGE_IMAGE` | ability to change image of deployment | no | `true`
| `NAMESPACE` | The namespace to use for deployment | no | `$KUBE_NAMESPACE`
| `POD_NAME` | name of the pods to update | yes, only if `CHANGE_IMAGE` is `true` | ` `
| `IMAGE_NAME` | name of the new image to use | yes, only if `CHANGE_IMAGE` is `true` | `$CI_REGISTRY_IMAGE`
| `IMAGE_TAG` | the tag to use for the new image | yes, only if `CHANGE_IMAGE` is `true` | ` `
| `KUSTOMIZE_OPTIONS` | Additional options for `kubectl` command | no | ` `

### Artifacts

* If the job is successful, the output of the commands will be available as an artifact exposed as `Kustomize job output`
 No newline at end of file
+14 −0
Original line number Diff line number Diff line
name: kustomize_deploy
description: This job will deploy your cluster using Kustomize
default_stage: deploy
icon: ⚙️
maintainer: moha-s
license: Apache-2.0
images:
  line/kubectl-kustomize:1.21.1-4.1.3
tools:
labels:
    - GitLab
    - Deploy
    - Kubernetes
    - Utilities
 No newline at end of file
+52 −0
Original line number Diff line number Diff line
# Job from R2Devops hub --> r2devops.io

stages:
  - deploy

kustomize_deploy:
  image: 
    name: line/kubectl-kustomize:1.21.1-4.1.3
    entrypoint: [""]
  stage: deploy
  variables:
    PROJECT_ROOT: "."
    KUBECONFIG: ""
    CHANGE_IMAGE: "true"
    KUSTOMIZATION_DIR: ""
    NAMESPACE: "$KUBE_NAMESPACE"
    POD_NAME: ""
    IMAGE_NAME: "$CI_REGISTRY_IMAGE"
    IMAGE_TAG: ""
    KUSTOMIZE_OPTIONS: ""


  script:
    # Working directory
    - cd ${PROJECT_ROOT}/${KUSTOMIZATION_DIR}
    # Image tag to use
    - if [ -z ${IMAGE_TAG} ]; then
    -   if [ ! -z ${CI_COMMIT_TAG} ]; then
    -     IMAGE_TAG=${CI_COMMIT_TAG}
    -   else
    -     IMAGE_TAG=${CI_COMMIT_SHA}
    -   fi
    - fi
    - if [ "${CHANGE_IMAGE}" = "true" ]; then
    -   kustomize edit set image ${POD_NAME}=${IMAGE_NAME}:${IMAGE_TAG} | tee -a ../output.log
    - fi
    # Set the namespace
    - kubectl config set-context --current --namespace=$NAMESPACE
    # Deploy
    - kubectl apply -k . $KUSTOMIZE_OPTIONS | tee -a ../output.log


  environment:
      name: ${CI_COMMIT_REF_SLUG}

  artifacts:
    expose_as: "Kustomize job output"
    paths:
      - output.log

  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
 No newline at end of file
+0 −0

Empty file added.

Loading