Search code examples
loggingservicecontainersgitlab-ci-runnerartifacts

how to get service container logs and upload them as artifacts in gitlab


Background: I use gitlab-ee of v13.11.4 and also have gitlab runners(both helper and builder) configured and deployed in a k8s cluster.
Problem: When a pipeline is triggered for a project in gitlab the jobs pick up a runner and finish the job and while job is running I want to capture those logs and upload them as artifacts so developers can download them?
I do have idea of kubernetes commands to get logs of a container but unable to approach the above problem

kubectl logs -c svc-0  -n ${RUNNER_NAMESPACE} $(hostname) > svc0.log
kubectl logs -c svc-1  -n ${RUNNER_NAMESPACE} $(hostname) > svc1.log

How can I get the logs while job is running and also upload them as artifacts in post-build ?


Solution

  • To get the service container logs of a job then use it like below in .gitlab-ci.yml

    sample_job:
      script:
        - kubectl logs ${HOSTNAME} -n ${RUNNER_NAMESPACE} -c svc-0 > svc0.log
    

    ${HOSTNAME} is a gitlab environment variable which gives pod's name where job is running. ${RUNNER_NAMESPACE} is namespace where your gitlab runners are deployed in k8s

    Note: if you think there might be multiple containers then have for loop running for all the svc-* containers in the runner pod(${HOSTNAME}) of job.