setting the build directory in Circle CI

868 Views Asked by At

For come reason I am not able to change my directory when build my pipeline in with Circle CI.

Also the command make is missing in Circle CI :(

file...

# 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

The client part produces an error that reads

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

and the 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

This is the same output for client and api...

and pwd for api

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

and pwd for client

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

Doing cd client && yarn or cd api && go build does work but this cannot be the correct way.

How can I set my working directory for each job?

1

There are 1 best solutions below

0
devatherock On

Though there are a few mistakes in your pipeline, your eventual conclusion to cd into the subfolder and execute the required command appears to be the correct one from my testing. The current folder cannot be changed the way you want using working_directory: ~/api, because the checkout step, that downloads the code, happens after the working directory is set and the checkout.path is interpreted as relative to the working directory as per the docs, not as an absolute path like ~/api. So with the working directory as ~/api and a checkout step without path, your code will be cloned to ~/api. But at that point the folder you want to execute the go build command from would be ~/api/api, so you'll still need to run cd api first. Similarly if you had used the relative path api in checkout.path with the working directory as ~/api, the code would have been cloned to ~/api/api, so now you'd have to cd api/api before running the build.

These can be illustrated with the outputs from the below test pipeline:

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

Output of pwd step: (Prints the working directory /root/test)

#!/bin/sh -eo pipefail
pwd

/root/test

CircleCI received exit code 0

Output of ls step: (Shows the subfolder 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

Output of cd & ls: (Shows the code cloned inside /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

Also the make command not being present is not a CircleCI limitation. When you use a docker executor with CircleCI, you are limited to the commands that are present in that docker image. In this case, the image golang:1.19.3-alpine3.16 does not contain make. I confirmed that by running docker run --rm golang:1.19.3-alpine3.16 make from my local machine which returned the below error:

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.

If a go image with make is what you need, CircleCI does provide its own convenience images for go. The CircleCI image for go 1.19.3 would be cimg/go:1.19.3, which does contain make.