skip to Main Content

I have a Node.js application that I want to containerize in Gitlab CI/CD.
This app includes a git submodule containing its Angular front-end.
Before proceeding with the "docker-build" job, the front-end must be generated.

My idea was to add a dedicated stage in my .gitlab-ci.yml file that will generate the front-end in a cached "fe" directory:

  stages:
    - front-end
    - build
    - test
    - package-build

  front-end:
    stage: front-end
    image: "registry.hub.docker.com/trion/ng-cli-karma:latest"
    script:
      - npm run fe-install
      - npm run fe-build
    cache:
      paths:
        - fe

But, how to get this cached "fe" directory in subsequent "docker-build" job so that Dockerfile can copy the front-end into the container?

2

Answers


  1. I consider you can use the artifacts keyword in your .gitlab-ci.yml. This way you can get the cached fe directory in subsequent docker-build job.

    Updated file:

    stages:
      - front-end
      - build
      - test
      - package-build
    
    front-end:
      stage: front-end
      image: "registry.hub.docker.com/trion/ng-cli-karma:latest"
      script:
        - npm run fe-install
        - npm run fe-build
      cache:
        paths:
          - fe
    
    docker-build:
      stage: build
      image: docker:latest
      script:
        - docker build -t your-image-name .
        - docker create --name dummy your-image-name
        - docker cp dummy:/your-path-to-fe /path-in-container
        - docker rm -v dummy
      dependencies:
        - front-end  
      artifacts:
        paths:
          - fe/
    
    Login or Signup to reply.
  2. GitLab CI/CD’s cache feature allows you to save dependencies and artifacts between jobs.

    Here’s an example of how you can modify your .gitlab-ci.yml file:

    stages:
      - front-end
      - build
      - test
      - package-build
    
    variables:
      FRONT_END_DIR: "fe"
    
    front-end:
      stage: front-end
      image: "registry.hub.docker.com/trion/ng-cli-karma:latest"
      script:
        - npm run fe-install
        - npm run fe-build
      cache:
        paths:
          - $FRONT_END_DIR
    
    docker-build:
      stage: build
      script:
        - # Your Docker build commands here
        - docker build -t my-app .
        - docker run my-app
      dependencies:
        - front-end
    

    In your Dockerfile, you can use the COPY command to copy the cached front-end directory into the container.

    # Other Dockerfile instructions...
    COPY $FRONT_END_DIR /path/to/destination/in/container
    

    With this configuration, the front-end will be generated in the front-end job and cached. Then, in the docker-build job, you can use the cached front-end directory in your Dockerfile. This ensures that your front-end is included in the Docker image.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search