Skip to content

Instantly share code, notes, and snippets.

@pangyuteng
Last active January 23, 2025 16:17
Show Gist options
  • Save pangyuteng/a49a7968143489459d3aa6bb8dc5092f to your computer and use it in GitHub Desktop.
Save pangyuteng/a49a7968143489459d3aa6bb8dc5092f to your computer and use it in GitHub Desktop.
gitlab cicd via kube

gitlab cicd via kube

--

notes

setup kubernetes "cluster"

curl -sfL https://get.k3s.io | sh -

export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

connect kube cluster with gitlab via gitlab agent

  • follow section in below link

https://docs.gitlab.com/ee/user/clusters/agent/install/index.html#create-an-agent-configuration-file

  • created empty repo https://gitlab.com/pangyuteng/hola-gitlab-cicd

  • "Operate"->"Kubernetes clusters"->"Connect a cluster"

  • "Option 2: Create and register an agent with the UI".

    • agent name: "hola"

    • "Create and register"

    • run the provided helm commands

image

image

image

  • create another new repo via "Create from template"--> pick "GitLab Cluster Management"-->"Use template"

name hola-cluster-management

cloned from https://gitlab.com/gitlab-org/project-templates/cluster-management

https://gitlab.com/gitlab-org/project-templates/cluster-management/-/tree/fe85a1cdcd80a1d78541d3a0a7293d776af440db

commit fe85a1cd

  • make some changes to files.

  • get runner token and add as cicd variable GITLAB_RUNNER_TOKEN (masked!)

    tags hola,kubernetes

    check run untagged jobs

    description hola-gitlab-runner

https://gitlab.com/pangyuteng/hola-cluster-management/-/runners/new

  • in repo create .gitlab/agents/hola/config.yamlto authorize agent access to project.
ci_access:
  projects:
    - id: pangyuteng/hola-cluster-management

https://docs.gitlab.com/ee/user/clusters/agent/ci_cd_workflow.html#authorize-the-agent-to-access-your-projects

https://gitlab.com/pangyuteng/hola-gitlab-cicd/-/blob/main/.gitlab/agents/hola/config.yaml?ref_type=heads

  • the initial Build Pipeline, you will need to use instance runner or create a runner.

https://gitlab.com/pangyuteng/hola-cluster-management/-/pipelines

  • monitor build pipelin and click play to enable the second step in the pipeline.

  • create new repo to testout running a cicd job with the runner in the cluster.

https://gitlab.com/pangyuteng/hola-app-in-cluster#

  • enable "hola-gitlab-runner" as runnner in settings->cicd

https://gitlab.com/pangyuteng/hola-app-in-cluster/-/settings/ci_cd#js-runners-settings

--

public:

sample "gitlab agent" repo to access kube cluster:

https://gitlab.com/pangyuteng/hola-gitlab-cicd

sample "cluster management" repo to deploying the runner:

https://gitlab.com/pangyuteng/hola-cluster-management

sample "hello-world" repo with cicd:

https://gitlab.com/pangyuteng/hola-app-in-cluster

--

old notes.

gitlab cicd via kube

  • create gitlab agent with kube cluster.

image

  • "install gitlab agent" (create repo, then follow belo, via Operate->Kubernetes cluster, run helm commands to create gitlab agent which lets gitlab access your cluster")

https://docs.gitlab.com/ee/user/clusters/agent/install/

https://docs.gitlab.com/ee/user/clusters/agent/ci_cd_workflow.html#use-gitlab-cicd-with-your-cluster

image

image

  • clone repo "cluster management project template" for spinning up the gitlab runner.

image

public:

https://gitlab.com/pangyuteng/hola-gitlab-cicd

https://gitlab.com/pangyuteng/hola-cluster-management

https://gitlab.com/pangyuteng/hola-app-in-cluster

private:

sample "gitlab agent" repo to access kube cluster: https://gitlab.com/pangyuteng/hello-k3s-gitlab-agent/-/tree/main?ref_type=heads

sample "cluster management" repo to deploying the runner: https://gitlab.com/pangyuteng/hello-gitlab-cluster-management

sample "hello-world" repo with cicd: https://gitlab.com/pangyuteng/hello-k3s-flask-app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment