In Gitlab CI/CD, how to commit and publish the docker container that is running our stages

Viewed 824

I am learning to work with gitlab CI/CD using docker executor.

Below is my gitlab-ci.yml file.

image: registry.gitlab.com/vbrin-office/components/karma-tests

stages:
  - test

test:
  stage: test
  script:
    - node --version
    - git --version
    - pwd
    - ls
    - cd /karmatests
    - ls
    - npx karma start

Each time when the pipeline is finished executing, I would like to commit the container and push it to the GitLab registry so that I can access it later.

Since we are actually inside the docker container for which I want to create an image. How could I do that? Is it even possible?

1 Answers

Yes, it is, but the image must have a certain name relative to your gitlab registry path. You can do something like this :

docker-build:
  stage: package
  script:
    - docker build -t $CI_REGISTRY_IMAGE:latest .
    - docker tag $CI_REGISTRY_IMAGE:latest $CI_REGISTRY_IMAGE:latest
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker push $CI_REGISTRY_IMAGE:latest
  only:
    - master

This will only push the image when the pipeline runs on your master branch.

Related