skip to Main Content

I have a project that requires npm and gradle for build, and docker for building and pushing the image.

At first I thought that I should create my own ubuntu image with gradle and npm setup, but I found out that is not what docker images are for.

So I hoped to run official Gradle and node images as a service so that my script can call those commands, but that is not happening for some reason.

My .gitlab-ci.yml:

  variables:
  IMAGE_NAME: my.registry.production/project
  IMAGE_TAG: $CI_COMMIT_BRANCH
  GIT_SUBMODULE_STRATEGY: recursive

stages:
  - build
  - deploy

build_project:
  stage: build
  image: ubuntu:jammy
  services:
    - name: node:12.20
      alias: npm
    - name: gradle:6.3.0-jre8
      alias: gradle
  before_script:
    - git submodule init && git submodule update --remote --recursive
  script:
    - cd project-server && npm install && gradle clean build -Pprod -Pwar -x test -x integrationTest

deploy_image:
  stage: deploy
  image: docker:20.10.17
  services:
    - name: docker:20.10.17-dind
      alias: docker
  variables:
    DOCKER_HOST: tcp://docker:2375
    DOCKER_TLS_CERTDIR: ""
    DOCKER_DRIVER: overlay2
  script:
    - docker login -u $REGISTRY_USER -p $REGISTRY_PASSWORD my.registry.production
    - docker build -t $IMAGE_NAME:$IMAGE_TAG .
    - docker push $IMAGE_NAME:$IMAGE_TAG

If anyone has any info on how to solve this I would greatly appreciate it, since I’m a novice DevOps.

Edit 1:
My Dockerfile for custom image with Gradle and Node installed.

FROM ubuntu:jammy

LABEL key=DevOps

SHELL ["/bin/bash", "--login", "-i", "-c"]

RUN apt update && apt upgrade -y && apt install curl -y 

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

RUN source /root/.bashrc && nvm install 12.14.1

RUN nvm install 12.20.0

RUN apt install zip unzip

RUN curl -s "https://get.sdkman.io" | bash

RUN source "$HOME/.sdkman/bin/sdkman-init.sh"

RUN sdk install java 8.0.302-open

RUN sdk install gradle 3.4.1

SHELL ["/bin/bash", "--login", "-c"]

CMD [ "bin/bash" ]

After I run it, it says that npm is not found in $PATH, I tried Java, Gradle as well but they weren’t found in the path as well.

I don’t know why since I installed them as you can tell from the Dockerfile.

2

Answers


  1. As I know, a docker image is equal to one build. So if you have multiple services you need to build each one into docker image then you can encapsulate all images into docker-compose.yml file.

    I think you can do the following:

    1. Build the npm project into a docker image
    2. Build the Gradle project into a docker image
    3. Write the docker-compose.yml file and put both images.

    Once you have done it, the pipeline calls the docker-compose.yml file.

    I hope this will be helpful.

    Login or Signup to reply.
  2. Consider a few suggestions based on the fundamental concepts about the deployment in your CI/CD pipeline:

    1. Remove the services keyword. Reference GitLab’s official documents on what the services keyword inside gitlab-ci.yaml file is not for. The feature is used
      to provide network accessable services to your job runtime (like
      a database): https://docs.gitlab.com/ee/ci/services/index.html
    2. Your project uses npm as a dependency management system, Gradle is
      a build tool. Both of these pieces of software are more than
      appropriate to run on the host operating system of the container
      runtime inside GitLab’s Pipeline job. You need these tools to assemble some build artifact as a result of the job on the same host your code has been downloaded on in the Runner.
    3. Think about the overall size of the base image in your build_project job and consider how time to download the image over the network on to the Runner will impact your job and overall pipeline duration. If performance can be improved by baking build dependencies into a custom Dockerfile do this. If your image is too large, instead use shell commands inside the script keyword block to download them at the runtime of the job. There can be pros and cons for both.
    4. Break shell scripts to one command per line for easier troubleshooting of failures in your scripts. You will be able to see the line number of the command which returned a non-zero exit code in your job logs:
    ...
    script:
     - cd project-server 
     - npm install
     - gradle clean build -Pprod -Pwar -x test -x integrationTest
    ...
    
    1. It’s recommended to use the Gradle wrapper (gradlew) most of the time instead of the gradle executable directly. Configure this within your project and check the configuration files for the wrapper into your version control system and this will simplify your build dependency: https://docs.gradle.org/current/userguide/gradle_wrapper.html
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search