在Circle CI中设置构建目录



由于某些原因,我无法在使用Circle CI构建管道时更改目录。

在循环CI中也缺少make命令:(

文件…

# Use the latest 2.1 version of CircleCI pipeline process engine.
# See: https://circleci.com/docs/2.0/configuration-reference
version: 2.1
# Define a job to be invoked later in a workflow.
# See: https://circleci.com/docs/2.0/configuration-reference/#jobs
jobs:
api:
working_directory: ~/api
docker:
- image: golang:1.19.3-alpine3.16
steps:
- checkout:
path: ~/api
-   setup_remote_docker: # (2)
docker_layer_caching: true # (3)
# specify any bash command here prefixed with `run: `
-   run: pwd
-   run: ls -all
#-   run: make mod-vendor
-   run: go get -v -t -d ./...
-   run: go test -v ./...
-   run: go build -o main .
-   deploy:
command: |
if [ "${CIRCLE_BRANCH}" == "circle-ci-setup" ]; then
docker build -t ${DOCKER_REPO} .
docker login -u ${DOCKER_USER} -p ${DOCKER_PASS} https://index.docker.io/v1/
docker push ${DOCKER_REPO}
fi
client:
working_directory: ~/client
# Specify the execution environment. You can specify an image from Dockerhub or use one of our Convenience Images from CircleCI's Developer Hub.
# See: https://circleci.com/docs/2.0/configuration-reference/#docker-machine-macos-windows-executor
docker:
- image: cimg/node:16.1.0
#- image: nginx:1.21.3-alpine
# Add steps to the job
# See: https://circleci.com/docs/2.0/configuration-reference/#steps
steps:
- checkout:
path: ~/client
#- restore_cache:
#     name: Restore Yarn Package Cache
#     keys:
#         - yarn-packages-{{ checksum "yarn.lock" }}
-   run: pwd
-   run: ls -all
- run:
name: Install Dependencies
command: yarn install --immutable
- save_cache:
name: Save Yarn Package Cache
key: yarn-packages-{{ checksum "yarn.lock" }}
paths:
- .yarn/cache
- .yarn/unplugged
- run:
name: Installing the package.json dependencies
command: yarn
# run build!
#-   run: make build-client
- run: yarn build
-   persist_to_workspace:
root: .
paths: dist
# Invoke jobs via workflows
# See: https://circleci.com/docs/2.0/configuration-reference/#workflows
workflows:
hobby-project-setup:
jobs:
- client
- api

客户端产生读取

的错误
yarn run v1.22.10
error Couldn't find a package.json file in "/home/circleci/client"
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Exited with code exit status 1
CircleCI received exit code 1

和api

#!/bin/sh -eo pipefail
cd api && go get -v -t -d ./...
go: go.mod file not found in current directory or any parent directory.
'go get' is no longer supported outside a module.
To build and install a command, use 'go install' with a version,
like 'go install example.com/cmd@latest'
For more information, see https://golang.org/doc/go-get-install-deprecation
or run 'go help get' or 'go help install'.
Exited with code exit status 1

ls-all

#!/bin/sh -eo pipefail
ls -all
total 32
drwxr-xr-x    7 root     root          4096 Dec  4 12:12 .
drwx------    1 root     root            33 Dec  4 12:12 ..
drwxr-xr-x    2 root     root            24 Dec  4 12:12 .circleci
drwxr-xr-x    4 root     root            72 Dec  4 12:12 .git
-rw-r--r--    1 root     root            13 Dec  4 12:12 .gitignore
-rw-r--r--    1 root     root          1369 Dec  4 12:12 Makefile
drwxr-xr-x    6 root     root            95 Dec  4 12:12 api
-rw-r--r--    1 root     root          5302 Dec  4 12:12 api.swagger.json
drwxr-xr-x    5 root     root          4096 Dec  4 12:12 client
-rw-r--r--    1 root     root           424 Dec  4 12:12 digital-ocean-infra-config.sh
-rw-r--r--    1 root     root           263 Dec  4 12:12 docker-compose.yml
drwxr-xr-x    6 root     root           104 Dec  4 12:12 infra

这是客户端和api的相同输出…

api 的和PWD

#!/bin/sh -eo pipefail
pwd
/root/api
CircleCI received exit code 0

和客户端PWD

#!/bin/bash -eo pipefail
pwd
/home/circleci/client
CircleCI received exit code 0

执行cd client && yarncd api && go build确实有效,但这不是正确的方法。

如何为每个作业设置工作目录?

虽然您的管道中有一些错误,但您最终将cd放入子文件夹并执行所需命令的结论似乎是从我的测试中正确的。当前文件夹不能以您想要的方式使用working_directory: ~/api更改,因为下载代码的checkout步骤发生在设置工作目录之后,并且checkout.path被解释为与文档中的工作目录相对,而不是像~/api那样作为绝对路径。因此,如果工作目录为~/api,并且签出步骤没有path,那么您的代码将被克隆到~/api。但是此时,您想要执行go build命令的文件夹将是~/api/api,因此您仍然需要先运行cd api。类似地,如果您在checkout.path中使用相对路径api,工作目录为~/api,那么代码将被克隆到~/api/api,因此现在您必须在运行构建之前运行cd api/api

这些可以用下面测试管道的输出来说明:

version: 2.1
jobs:
test:
docker:
- image: golang:1.19.3-alpine3.16
auth:
username: $DOCKER_USERNAME
password: $DOCKER_PASSWORD
resource_class: small
working_directory: ~/test
steps:
- checkout:
path: app
- run: pwd
- run: ls -all
- run: cd app && ls -all
workflows:
version: 2.1
pr_check:
jobs:
- test:
context:
- docker-credentials
filters:
branches:
only: circleci

pwd步骤输出:(打印工作目录/root/test)

#!/bin/sh -eo pipefail
pwd
/root/test
CircleCI received exit code 0

ls步骤输出:(显示子文件夹app)

#!/bin/sh -eo pipefail
ls -all
total 4
drwxr-xr-x    3 root     root            17 Jun 11 01:29 .
drwx------    1 root     root            30 Jun 11 01:29 ..
drwxr-xr-x    6 root     root          4096 Jun 11 01:29 app
CircleCI received exit code 0

cd输出&ls:(显示在/root/test/app中克隆的代码)

#!/bin/sh -eo pipefail
cd app && ls -all
total 40
drwxr-xr-x    6 root     root          4096 Jun 11 01:29 .
drwxr-xr-x    3 root     root            17 Jun 11 01:29 ..
drwxr-xr-x    2 root     root            24 Jun 11 01:29 .circleci
drwxr-xr-x    4 root     root            72 Jun 11 01:29 .git
-rw-r--r--    1 root     root           154 Jun 11 01:29 .gitattributes
-rw-r--r--    1 root     root           154 Jun 11 01:29 .gitignore
-rw-r--r--    1 root     root          1068 Jun 11 01:29 LICENSE
-rw-r--r--    1 root     root            44 Jun 11 01:29 README.md
-rw-r--r--    1 root     root          1058 Jun 11 01:29 build.gradle
drwxr-xr-x    3 root     root            21 Jun 11 01:29 gradle
-rwxr-xr-x    1 root     root          8070 Jun 11 01:29 gradlew
-rw-r--r--    1 root     root          2674 Jun 11 01:29 gradlew.bat
-rw-r--r--    1 root     root            72 Jun 11 01:29 lombok.config
drwxr-xr-x    4 root     root            30 Jun 11 01:29 src
CircleCI received exit code 0

make命令不存在也不是CircleCI限制。当您将docker执行器与CircleCI一起使用时,您只能使用该docker映像中存在的命令。在此例中,镜像golang:1.19.3-alpine3.16不包含make。我通过在本地机器上运行docker run --rm golang:1.19.3-alpine3.16 make来确认,它返回了以下错误:

docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "make": executable file not found in $PATH: unknown.

如果您需要带有make的go图像,CircleCI确实为go提供了自己的方便图像。1.19.3的CircleCI图像将是cimg/go:1.19.3,其中包含make

相关内容

  • 没有找到相关文章

最新更新