David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 1 | # Hermetic build |
| 2 | |
| 3 | Hafnium build is not hermetic as it uses some system tools and libraries, e.g. |
| 4 | `bison` and `libssl`. To ensure consistency and repeatability, the team |
| 5 | maintains and periodically publishes a container image as the reference build |
| 6 | environment. The image is hosted on Google Cloud Platform as |
| 7 | `eu.gcr.io/hafnium-build/hafnium_ci`. |
| 8 | |
| 9 | Building inside a container is always enabled only for Kokoro pre-submit tests |
| 10 | but can be enabled for local builds too. It is disabled by default as it |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 11 | requires the use of Docker which currently supports rootless containers only in |
| 12 | nightly builds. As rootless container tools mature, Hafnium may change the |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 13 | default settings. For now, running the hermetic build locally is intended |
| 14 | primarily to reproduce issues in pre-submit tests. |
| 15 | |
Andrew Walbran | b784997 | 2019-11-15 15:23:43 +0000 | [diff] [blame^] | 16 | [TOC] |
| 17 | |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 18 | ## Installing Docker |
| 19 | |
| 20 | ### Stable |
| 21 | |
| 22 | If you don't mind running a Docker daemon with root privileges on your system, |
| 23 | you can follow the [official guide](https://docs.docker.com/install/) to install |
| 24 | Docker, or [go/installdocker](https://goto.google.com/installdocker) if you are |
| 25 | a Googler. |
| 26 | |
| 27 | Because the daemon runs as root, files generated by the container are owned by |
| 28 | root as well. To work around this, the build will automatically derive a local |
| 29 | container image from the base container, adding user `hafnium` with the same |
| 30 | UID/GID as the local user. |
| 31 | |
| 32 | ### Nightly with rootless |
| 33 | |
| 34 | The latest nightly version of Docker has support for running containers with |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 35 | user namespaces, thus eliminating the need for a daemon with root privileges. It |
| 36 | can be installed into the local user's `bin` directory with a script: |
| 37 | |
| 38 | ```shell |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 39 | curl -fsSL https://get.docker.com/rootless -o get-docker.sh |
| 40 | sh get-docker.sh |
| 41 | ``` |
| 42 | |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 43 | The script will also walk you through the installation of dependencies, changes |
| 44 | to system configuration files and environment variable values needed by the |
| 45 | client to discover the rootless daemon. |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 46 | |
| 47 | ## Enabling for local builds |
| 48 | |
| 49 | Hermetic builds are controlled by the `HAFNIUM_HERMETIC_BUILD` environment |
| 50 | variable. Setting it to `true` instructs the build to run commands inside the |
| 51 | container. Any other value disables the feature. |
| 52 | |
| 53 | To always enable hermetic builds, put this line in your `~/.bashrc`: |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 54 | |
| 55 | ```shell |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 56 | export HAFNIUM_HERMETIC_BUILD=true |
| 57 | ``` |
| 58 | |
| 59 | When you now run `make`, you should see the following line: |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 60 | |
| 61 | ```shell |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 62 | $ make |
| 63 | Running in container: make all |
| 64 | ... |
| 65 | ``` |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 66 | |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 67 | ## Running commands inside the container |
| 68 | |
| 69 | An arbitrary command can be executed inside the container with |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 70 | `build/run_in_container.sh [-i] <command> ...`. This is done automatically |
| 71 | inside `Makefile` and `kokoro/ubuntu/build.sh` which detect whether they are |
| 72 | already running inside the container and respawn themselves using |
| 73 | `run_in_container.sh` if not. |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 74 | |
| 75 | For example, you can spawn a shell with: |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 76 | |
| 77 | ```shell |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 78 | ./build/run_in_container.sh -i bash |
| 79 | ``` |
| 80 | |
| 81 | ## Building container image |
| 82 | |
| 83 | The container image is defined in `build/docker/Dockerfile` and can be built |
| 84 | locally: |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 85 | |
| 86 | ```shell |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 87 | ./build/docker/build.sh |
| 88 | ``` |
| 89 | |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 90 | Owners of the `hafnium-build` GCP repository can publish the new image (requires |
| 91 | [go/cloud-sdk](https://goto.google.com/cloud-sdk) installed and authenticated): |
| 92 | |
| 93 | ```shell |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 94 | ./build/docker/publish.sh |
| 95 | ``` |