patternkubernetesMinor
One time/set up jobs on gitlab ci?
Viewed 0 times
gitlabtimeonejobsset
Problem
I'm setting up a pipeline that compiles and builds new docker images, and then deploys those images to testing kubernetes cluster.
I'm using a shell runner.
I need to setup my
As I understand it - I should only need to run this once. Is there a way to get this to run once, as defined in the
I'm using a shell runner.
I need to setup my
kubectl client to point to the testing kubernetes cluster. (ie. setting the cluster and context).As I understand it - I should only need to run this once. Is there a way to get this to run once, as defined in the
.gitlab-ci.yaml? Or should I just repeat the set up steps everytime in before_script?Solution
I dealt with the same issue. Some people recommended using the Gitlab group "secrets" and using
Now just set an environment variable for your gitlab runner to point to your new image.
Don't forget to test your new container!
Edit: Missed this part.. I'm using a shell runner.
So post of what I said is probably not relevant. How ever I do think there is some advantages to building and deploying with a gitlab runner directly on a cluster.
before_script. Since I knew I was also going to deploy and would need other tools on my runner like "helm", I made my own docker container. I still keep my config for my clusters in a base64 encoded group secret but I set it like this in the dockerfile.ENV kube_config=$kube_config
RUN echo -n ${kube_config} | base64 -d > ~/.kube/configNow just set an environment variable for your gitlab runner to point to your new image.
KUBERNETES_IMAGE: registry.gitlab.com/MY_USERNAME/MY_REPO_NAME/MY_IMAGE_NAME:latestDon't forget to test your new container!
stages:
- build
- test
- release
variables:
CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE/gitlab_runner:$CI_COMMIT_REF_NAME
CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE/gitlab_runner:latest
before_script:
- apt-get update && apt-get install docker.io -y
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
Build Image:
stage: build
script:
- docker build --build-arg kube_config=${kube_config} -t ${CONTAINER_TEST_IMAGE} .
- docker push ${CONTAINER_TEST_IMAGE}
except:
- master
Test Kubectl:
stage: test
script:
- docker pull ${CONTAINER_TEST_IMAGE}
- docker run --rm ${CONTAINER_TEST_IMAGE} kubectl get deployments -n kube-system
except:
- master
Test Helm:
stage: test
script:
- docker pull ${CONTAINER_TEST_IMAGE}
- docker run --rm ${CONTAINER_TEST_IMAGE} helm ls
except:
- master
Test Docker:
stage: test
script:
- docker pull ${CONTAINER_TEST_IMAGE}
- docker run --rm -v /var/run/docker.sock:/var/run/docker.sock ${CONTAINER_TEST_IMAGE} docker images
except:
- master
release-image:
stage: release
script:
- docker pull $CONTAINER_TEST_IMAGE
- docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE
- docker push $CONTAINER_RELEASE_IMAGE
only:
- masterEdit: Missed this part.. I'm using a shell runner.
So post of what I said is probably not relevant. How ever I do think there is some advantages to building and deploying with a gitlab runner directly on a cluster.
Code Snippets
ENV kube_config=$kube_config
RUN echo -n ${kube_config} | base64 -d > ~/.kube/configKUBERNETES_IMAGE: registry.gitlab.com/MY_USERNAME/MY_REPO_NAME/MY_IMAGE_NAME:lateststages:
- build
- test
- release
variables:
CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE/gitlab_runner:$CI_COMMIT_REF_NAME
CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE/gitlab_runner:latest
before_script:
- apt-get update && apt-get install docker.io -y
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
Build Image:
stage: build
script:
- docker build --build-arg kube_config=${kube_config} -t ${CONTAINER_TEST_IMAGE} .
- docker push ${CONTAINER_TEST_IMAGE}
except:
- master
Test Kubectl:
stage: test
script:
- docker pull ${CONTAINER_TEST_IMAGE}
- docker run --rm ${CONTAINER_TEST_IMAGE} kubectl get deployments -n kube-system
except:
- master
Test Helm:
stage: test
script:
- docker pull ${CONTAINER_TEST_IMAGE}
- docker run --rm ${CONTAINER_TEST_IMAGE} helm ls
except:
- master
Test Docker:
stage: test
script:
- docker pull ${CONTAINER_TEST_IMAGE}
- docker run --rm -v /var/run/docker.sock:/var/run/docker.sock ${CONTAINER_TEST_IMAGE} docker images
except:
- master
release-image:
stage: release
script:
- docker pull $CONTAINER_TEST_IMAGE
- docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE
- docker push $CONTAINER_RELEASE_IMAGE
only:
- masterContext
StackExchange DevOps Q#3857, answer score: 3
Revisions (0)
No revisions yet.