CircleCi 2.0 如何与位于子目录内的项目一起工作

44

我正试图将我的 springboot 教程项目与 CircleCi 集成。

我的项目在 Github 存储库的子目录中,但是我从 CircleCi 得到了以下错误:

需要一个项目来执行目标,但此目录中没有 POM (/home/circleci/recipe)。请验证您是否从正确的目录调用了 Maven。

我无法弄清楚如何告诉 Circle-Ci 我的项目在子目录中。 我尝试了几种方法,包括尝试在“recipe”内部使用 cd 命令,但都不起作用或者感觉不对。

这是我的项目结构:

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

这是我的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
4个回答

70

我已经成功修复了问题。我相信是以下组合导致的

    working_directory: ~/spring-tutorial/recipe
and of
  - checkout:
      path: ~/spring-tutorial

成功地实现了。

这是我的可运行的 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

2
为什么这个能够工作?working_Directory不是已经设置了工作目录吗?为什么我们还需要先检出到文件夹,然后再进入子目录呢? - Jwan622
1
根据circleci的工作目录文档指定步骤运行的目录。checkout是从Git仓库签出源代码。 - Kevin Amiranoff
2
这个设置给了我一个目录 (/home/circleci/spring-tutorial),你试图检出的目录不是空的,也不是一个 git 仓库。这是因为 spring-tutorial 中存在 recipe 文件夹。 - Ville
@Ville 在我的情况下,spring-tutorial 是一个 git 存储库。 - Kevin Amiranoff
1
一样。但是如果你定义了工作目录为~/spring-tutorial/recipe,由于spring-tutorial已经有一个文件夹(recipe),所以你不能在那里进行checkout,因为它不是空的,对吧? - Ville
1
我遇到了相同的问题“不为空且不是git存储库”。似乎指定working_directory: ~/spring-tutorial/recipe会创建两个目录,导致检出失败,因为recipe已经存在。 - simpleuser

18

我有点困惑,所以我想更明确一下

构建子目录的关键解决方案是定义您的工作空间,在这种情况下,它的子文件夹会被定义为:

 working_directory: ~/repo/sub_folder

并结账到

- checkout:
     path: ~/repo

像这样

# 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

5
如果有人尝试运行npm命令,但cd命令不按预期工作,例如:
cd subdir && npm run command

你可以使用npm的--prefix选项。
- 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

我不使用npm,因为它不是JavaScript。 - LukeN

1

我的代码库有一个名为web的目录,整个Web应用程序都在其中,包括package.jsonpackage-lock.json、Webpack配置等。我的应用程序必须在web目录内构建,并从在其下创建的dist中提供服务。我希望CircleCI在安装所需依赖项后,在该web子目录中运行测试。 接受的答案在CircleCI 2.1 Config中对我不起作用。这是我尝试过的config.yml

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" }}

它给了我一个错误

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

对我最终起作用的是利用run步骤支持的working_directory属性。更新后的CI配置如下所示。

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" }}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接