44

I am trying to integrate my springboot tutorial project with CircleCi.

My project is inside a subdirectory inside a Github repository and I get the following error from CircleCi.

Goal requires a project to execute but there is no POM in this directory (/home/circleci/recipe). Please verify you invoked Maven from the correct directory.

I can't figure out how to tell circle-ci my project is inside a subdirectory. I have tried a couple of things, as well as trying to cd inside 'recipe' but it does not work or even feel right.

Here is the structure of my project:

Spring-tutorials
 |
 +-- projectA
 |    
 +-- recipe
 |   | +--pom.xml

Here is my config.yml

# Java Maven CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-java/ for more details
#
version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/openjdk:8-jdk

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      # - image: circleci/postgres:9.4

    working_directory: ~/recipe

    environment:
      # Customize the JVM maximum heap limit
      MAVEN_OPTS: -Xmx3200m

    steps:
      - checkout
      - run: cd recipe/; ls -la; pwd;

      # Download and cache dependencies
      - restore_cache:
          keys:
          - recipe-{{ checksum "pom.xml" }}
          # fallback to using the latest cache if no exact match is found
          - recipe-

      - run: cd recipe; mvn dependency:go-offline

      - save_cache:
          paths:
            - ~/recipe/.m2
          key: recipe-{{ checksum "pom.xml" }}

      # run tests!
      - run: mvn integration-test
kasptom
  • 2,363
  • 2
  • 16
  • 20
Kevin Amiranoff
  • 13,440
  • 11
  • 59
  • 90

4 Answers4

69

I managed to fix the issue. I believe the combination of

    working_directory: ~/spring-tutorial/recipe

and of

  - checkout:
      path: ~/spring-tutorial

made it work.

Here is my working config.yml:

# Java Maven CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-java/ for more details
#
version: 2
jobs:
  build:
    working_directory: ~/spring-tutorial/recipe
    docker:
      # specify the version you desire here
      - image: circleci/openjdk:8-jdk

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      # - image: circleci/postgres:9.4

    environment:
      # Customize the JVM maximum heap limit
      MAVEN_OPTS: -Xmx3200m

    steps:
      - checkout:
          path: ~/spring-tutorial

      # Download and cache dependencies
      - restore_cache:
          keys:
          - recipe-{{ checksum "pom.xml" }}
          # fallback to using the latest cache if no exact match is found
          - recipe-

      - run: mvn dependency:go-offline

      - save_cache:
          paths:
            - ~/.m2
          key: recipe-{{ checksum "pom.xml" }}

      # run tests!
      - run: mvn integration-test
kasptom
  • 2,363
  • 2
  • 16
  • 20
Kevin Amiranoff
  • 13,440
  • 11
  • 59
  • 90
  • 2
    Why does this work? Doesn't working_Directory set the working_directory? Why do we have to checkout also to the folder before the sub_directory? – Jwan622 Nov 06 '19 at 15:38
  • 1
    According to the documentation of [circleci for working_directory](https://circleci.com/docs/2.0/configuration-reference/#executors-requires-version-21): `In which directory to run the steps.` And [checkout](https://circleci.com/docs/2.0/configuration-reference/#checkout) is checking out the source code from a git repository. – Kevin Amiranoff Nov 06 '19 at 15:49
  • 2
    This setup gives me Directory (/home/circleci/spring-tutorial) you are trying to checkout to is not empty and not a git repository. That's because recipe folder exists in spring-tutorial – Ville Sep 30 '21 at 20:36
  • @Ville in my case spring-tutorial was a git repository – Kevin Amiranoff Sep 30 '21 at 20:44
  • 1
    Same. But as you define working_directory: ~/spring-tutorial/recipe, spring-tutorial already has a folder (recipe) so you cannot checkout there since it is not empty, right? – Ville Sep 30 '21 at 20:57
  • 1
    I run into the same issue "is not empty and not a git repository". It seems specifying `working_directory: ~/spring-tutorial/recipe` creates both directories, causing the checkout to fail because `recipe` already exists. – simpleuser Aug 25 '22 at 17:01
18

I got confused a little bit so I would like to clarify it more

the key solution for building subdirectory to define your working-space, in this case, its subfolder so it will defined like this

 working_directory: ~/repo/sub_folder

and checkout to

- checkout:
     path: ~/repo

to be like this

# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/node:7.10

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      # - image: circleci/mongo:3.4.4

    working_directory: ~/repo/sub_folder

    steps:
      - checkout:
            path: ~/repo

      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "package.json" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-

      - run: npm install

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}

      # run tests!
      - run: npm test
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
5

If anyone tries running npm command and cd is not working as expected, i.e like:

cd subdir && npm run command

You can use --prefix option of npm.

- run:
      name: Run start command in recipe folder
      command: npm --prefix ./recipe run start

# then in same file
- run:
      name: Run test command in app folder
      command: npm --prefix ./app run test
Artur Aleksanyan
  • 480
  • 5
  • 10
1

My codebase has a web directory under which the entire web application resides including the package.json, package-lock.json, Webpack config etc. My application has to be built inside the web directory and served from the dist that gets created under it. I wanted CircleCI to run tests inside this web subdirectory after installing the required dependencies in it. The accepted answer did not work for me in CircleCI 2.1 Config. This is the config.yml I tried

version: 2.1

jobs:
  test:
    docker:
      - image: cimg/node:16.15.1

    # Added working directory pointing to `web`
    working_directory: ~/app/web

    steps:
      - checkout:
          # Checking out to the parent directory
          path: ~/app

      - restore_cache:
          keys:
            - npm-packages-{{ checksum "package-lock.json" }}
            - npm-packages-

      - run:
          name: Installing packages
          command: npm install

      - run:
          name: Run tests
          command: npm run test

      - save_cache:
          paths:
            - node_modules
          key: npm-packages-{{ checksum "package-lock.json" }}

It gave me the error

Directory you are trying to checkout to is not empty and not a git repository

What finally worked for me was by making use of the working_directory property supported by the run step. The updated CI config is as given below.

version: 2.1

jobs:
  test:
    docker:
      - image: cimg/node:16.15.1

    # Kept the working directory as such
    working_directory: ~/app

    steps:
      # Kept checkout as such
      - checkout

      - restore_cache:
          keys:
            # Restoring cache using web/package-lock.json
            - npm-packages-{{ checksum "web/package-lock.json" }}
            - npm-packages-

      # Running npm install in the web directory using working_directory
      # property of the run step
      - run:
          name: Installing packages
          working_directory: ~/app/web
          command: npm install

      # Running npm test in the web directory using working_directory
      # property of the run step
      - run:
          name: Run tests
          working_directory: ~/app/web
          command: npm test

      - save_cache:
          paths:
            - node_modules
          # Saving cache using web/package-lock.json
          key: npm-packages-{{ checksum "web/package-lock.json" }}
Jophin Joseph
  • 2,864
  • 4
  • 27
  • 40