|
| 1 | +# Software Design Document: Passing arguments to a Dockerfile during `az acr build` execution |
| 2 | + |
| 3 | +Reference: An option to pass environment variables while building a Dockerfile |
| 4 | +<br> Authors: Bhargav Nookala, Evan Louie, James Spring, Michael Tarng, Yvonne |
| 5 | +Radsmikham |
| 6 | + |
| 7 | +| Revision | Date | Author | Remarks | |
| 8 | +| -------: | ------------ | ----------------- | ------------- | |
| 9 | +| 0.1 | Mar-26, 2020 | Yvonne Radsmikham | Initial Draft | |
| 10 | + |
| 11 | +## 1. Overview |
| 12 | + |
| 13 | +This design will enhance the developer experience for using |
| 14 | +`spk service create`. Currently, the concern is that building of the Dockerfile |
| 15 | +assumes there are no inputs into the build process. This is handled in a script |
| 16 | +task of the `build` stage in the `build-update-hld.yaml`, which is generated |
| 17 | +when `spk service create` is executed. |
| 18 | + |
| 19 | +Snippet of `build-update-hld.yaml`: |
| 20 | + |
| 21 | +```yaml |
| 22 | + - script: |- |
| 23 | + set -e |
| 24 | + export BUILD_REPO_NAME=$(echo $(Build.Repository.Name)-fabrikam.acme.frontend | tr '[:upper:]' '[:lower:]') |
| 25 | + export IMAGE_TAG=$(echo $(Build.SourceBranchName) | tr / - | tr . - | tr _ - )-$(Build.BuildNumber) |
| 26 | + export IMAGE_NAME=$BUILD_REPO_NAME:$IMAGE_TAG |
| 27 | + echo "Image Name: $IMAGE_NAME" |
| 28 | + cd ./services/fabrikam.acme.frontend |
| 29 | + echo "az acr build -r $(ACR_NAME) --image $IMAGE_NAME ." |
| 30 | + az acr build -r $(ACR_NAME) --image $IMAGE_NAME . |
| 31 | + displayName: ACR Build and Publish |
| 32 | +``` |
| 33 | +
|
| 34 | +## 2. Out of Scope |
| 35 | +
|
| 36 | +This design shall only aim to make user experience better by adding new features |
| 37 | +to service creation. |
| 38 | +
|
| 39 | +## 3. Design Details |
| 40 | +
|
| 41 | +### 3.1 Add new arguments for `spk service create` to link or create variable groups |
| 42 | + |
| 43 | +As part of `spk service create`, add the following features: |
| 44 | + |
| 45 | +- Create a new command argument `--build-vg` to link an existing variable group |
| 46 | + or create a new variable group with variables to be passed into a Docker build |
| 47 | + execution. |
| 48 | + |
| 49 | +- Add argument `--build-variables` to take in an array of key-value pairs for |
| 50 | + all variables to be used in an existing or new variable group. This argument |
| 51 | + will support secret variables if specified. |
| 52 | + |
| 53 | +Example: |
| 54 | + |
| 55 | +```sh |
| 56 | +spk service create "fabrikam.acme.frontend" -d "services" -p "chart" -g "https://dev.azure.com/bedrock/fabrikam/_git/fabrikam2019" -b "master" --build-vg "my-azdo-build-vg" --build-variables [version:0.0.1,path:$HOME,apikey:key:isSecret] |
| 57 | +``` |
| 58 | + |
| 59 | +To do this, we would be leveraging `spk project cvg` or |
| 60 | +`spk variable-group create`. This _can_ be done as a **prerequisite** for using |
| 61 | +the `build-vg` and `--build-variables`. For example, if the user is providing a |
| 62 | +variable group name that does not exist, we may return an error message to |
| 63 | +enforce that they create the variable group first with the appropriate variables |
| 64 | +to use by executing `spk project cvg` or `spk variable-group create` beforehand. |
| 65 | + |
| 66 | +As part of the build-update-hld.yaml, where there is the "ACR Build and Publish" |
| 67 | +task |
| 68 | +([L208](https://github.com/CatalystCode/spk/blob/7fd1606a6e6ad0a4622a8be2f20fe3a0c17e5a82/src/lib/fileutils.ts#L208)-[L217](https://github.com/CatalystCode/spk/blob/7fd1606a6e6ad0a4622a8be2f20fe3a0c17e5a82/src/lib/fileutils.ts#L217)) |
| 69 | +generated by `spk service create`, we can add logic to iterate through the |
| 70 | +variable array assocciated with a variable group and inject variables as |
| 71 | +arguments to be part of the `az acr build` command |
| 72 | +(i.e.`az acr build -r $(ACR_NAME) -f $(DOCKER_BUILD_FILE) --build-arg $key=${value}`). |
| 73 | +In the `build-update-hld.yaml`, this would imply that each variable that needs |
| 74 | +to be passed into the Dockerfile will need to be exported (i.e. |
| 75 | +`export PATH=${PATH}`) as an environment variable so that it can be captured in |
| 76 | +the build command. |
| 77 | + |
| 78 | +The iteration should be conditional, meaning if there are no variable groups |
| 79 | +linked, then the assumption is made that there are no arguments needed to be |
| 80 | +passed into the build process, and in which case the build process will continue |
| 81 | +as how it is working currently. |
| 82 | + |
| 83 | +## 4. Dependencies |
| 84 | + |
| 85 | +None |
| 86 | + |
| 87 | +## 5. Known issues |
| 88 | + |
| 89 | +None |
| 90 | + |
| 91 | +## 6. Risks & Mitigations |
| 92 | + |
| 93 | +There needs to be proper error handling when dealing with naming collisions in |
| 94 | +creating new variable groups or adding new variables to existing variable |
| 95 | +groups. |
| 96 | + |
| 97 | +## 7. Documentation |
| 98 | + |
| 99 | +Documentation should be done in the |
| 100 | +[`md` file](https://github.com/CatalystCode/spk/blob/master/src/commands/service/create.md) |
| 101 | +that are associated with `spk service create` command. |
| 102 | + |
| 103 | +\- end - |
0 commit comments