Azure / acr-builder

Azure Container Registry Build Runner
MIT License
38 stars 35 forks source link

workingDirectory: support relative path in build step #503

Closed northtyphoon closed 1 year ago

northtyphoon commented 5 years ago

The default workingDirecotry for each step is /workspace. For build step, if remote context is used, the source code will be cloned to /workspace/<step_id> folder which is also the workingDirectory for the corresponding build step.

In the following example, I want to use the child folder baseimages/docker-cli as the workingDirectory which will become the docker build context.

- build: .
  workingDirectory: baseimages/docker-cli

Other thought, if workingDirectory doesn't support relative path, the code should explicitly call it out in the error message to clarify that.

chloeyin commented 1 year ago

Relative path is supported.

Remote context:

tree
.
├── CHANGELOG.md
├── CONTRIBUTING.md
├── Dockerfile
├── Dockerfile-app
├── Dockerfile-base
├── LICENSE.md
├── README.md
├── package.json
├── server.js
├── subfolder
│   ├── subfolder2
│   │   └── Dockerfile
│   └── taskmulti.yaml
├── taskmulti-multiregistry.yaml
└── taskmulti.yaml

The task file in root directory(taskmulti.yaml) will set the build step working directory to the subfolder/subfolder2:

version: v1.0.0
steps:
# Build target image
- build: .
  workingDirectory: subfolder/subfolder2

Run the task

az acr task create -n $name -r $registry -c $myGithubRepo -f taskmulti.yaml  \
--base-image-trigger-enabled false --commit-trigger-enabled false

az acr task run -n $name -r $registry
Queued a run with ID: cd30
Waiting for an agent...
2023/02/17 08:34:49 Downloading source code...
2023/02/17 08:34:51 Finished downloading source code
2023/02/17 08:34:51 Creating Docker network: acb_default_network, driver: 'bridge'
2023/02/17 08:34:51 Successfully set up Docker network: acb_default_network
2023/02/17 08:34:51 Setting up Docker configuration...
2023/02/17 08:34:52 Successfully set up Docker configuration
2023/02/17 08:34:52 Logging in to registry: yzhregistry.azurecr.io
2023/02/17 08:34:53 Successfully logged into yzhregistry.azurecr.io
2023/02/17 08:34:53 Executing step ID: acb_step_0. Timeout(sec): 600, Working directory: 'subfolder/subfolder2', Network: 'acb_default_network'
2023/02/17 08:34:53 Scanning for dependencies...
2023/02/17 08:34:53 Successfully scanned dependencies
2023/02/17 08:34:53 Launching container with name: acb_step_0
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM golang:1.20.1-alpine3.17
1.20.1-alpine3.17: Pulling from library/golang
# ignore the output

As shown in the log, the task can find the correct Dockerfile.

Here is the code https://github.com/Azure/acr-builder/blob/7470945443e0c1e69b09147ec8a63d51af051f8a/builder/builder.go#L292-L313 In this case, the builder will invoke docker run to build the image in a container and set the container working directory to the workingDirectory in the yaml file. If the workingDirectory is a relative path, the actual working directory in the container is containerWorkspaceDir/workingDirectory, this is done by doing a normalization https://github.com/Azure/acr-builder/blob/7470945443e0c1e69b09147ec8a63d51af051f8a/builder/context.go#L285-L292 Since the builder also mount the volume to the containerWorkspaceDir, the directory docker build sees is the relative path given in the yaml file, which is the correct path relative to the source code.