Skip to content

Commit

Permalink
Building inside docker container (#293)
Browse files Browse the repository at this point in the history
Building inside a docker container

Adding support for running CI pipelines inside a docker container
Added
- Dockerfile based on Ubuntu 22.04
- Docker installs dependencies and prebuilds toolchain
- Workflow for generating docker image
- Workflow that runs build and tests using the docker container
  • Loading branch information
vmilosevic authored Aug 8, 2024
1 parent a4ac702 commit e61ea78
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 10 deletions.
90 changes: 90 additions & 0 deletions .github/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
FROM ubuntu:22.04
SHELL ["/bin/bash", "-c"]

# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive
ENV TTMLIR_TOOLCHAIN_DIR=/opt/ttmlir-toolchain

# Install dependencies
RUN apt-get update && apt-get install -y \
software-properties-common \
build-essential \
python3-dev \
python3-venv \
python3-pip \
git \
git-lfs \
libhwloc-dev \
pandoc \
libtbb-dev \
libcapstone-dev \
pkg-config \
linux-tools-generic \
ninja-build \
wget \
libgtest-dev \
cmake \
ccache \
doxygen \
graphviz \
patchelf \
libyaml-cpp-dev \
libboost-all-dev

# Install clang 17
RUN wget https://apt.llvm.org/llvm.sh && \
chmod u+x llvm.sh && \
./llvm.sh 17 && \
apt install -y libc++-17-dev libc++abi-17-dev && \
ln -s /usr/bin/clang-17 /usr/bin/clang && \
ln -s /usr/bin/clang++-17 /usr/bin/clang++

# Install python packages
RUN pip install cmake

# Install Googletest
RUN git clone https://github.com/google/googletest.git -b release-1.12.1 && \
cd googletest && \
mkdir build && \
cd build && \
cmake .. -DBUILD_GMOCK=OFF && \
make && \
make install && \
cd ../.. && \
rm -rf googletest

# Create a directory for the build and toolchain
ARG BUILD_DIR=/home/build
RUN mkdir -p $BUILD_DIR && \
mkdir -p $TTMLIR_TOOLCHAIN_DIR

# Clone the project and update submodules
RUN git clone https://github.com/tenstorrent/tt-mlir.git $BUILD_DIR/tt-mlir && \
cd $BUILD_DIR/tt-mlir && \
git submodule update --init --recursive -f

# Build the toolchain
WORKDIR $BUILD_DIR/tt-mlir
RUN cmake -B env/build env && \
cmake --build env/build

# Build project to test the container
RUN source env/activate && \
cmake -G Ninja \
-B build \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
-DTTMLIR_ENABLE_RUNTIME=ON \
-DTTMLIR_ENABLE_RUNTIME_TESTS=ON && \
cmake --build build --config Release

# Run clang-tidy
RUN cmake --build build -- clang-tidy || true

# Run the tests
RUN cmake --build build -- check-ttmlir || true

# Clean up the build directory
RUN rm -rf $BUILD_DIR/tt-mlir
37 changes: 37 additions & 0 deletions .github/workflows/build-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Build and Publish Docker Image

on:
workflow_dispatch:
workflow_call:

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
sparse-checkout: |
.github/Dockerfile
sparse-checkout-cone-mode: false

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .github
file: .github/Dockerfile
push: true
tags: |
ghcr.io/${{ github.repository }}/tt-mlir-ubuntu-22-04:${{ github.sha }}
ghcr.io/${{ github.repository }}/tt-mlir-ubuntu-22-04:latest
90 changes: 90 additions & 0 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Build in Docker

on:
workflow_dispatch:
workflow_call:

jobs:

build-and-test:

strategy:
fail-fast: false
matrix:
image: ["ubuntu-22-04"]
build: [
{runs-on: ubuntu-latest, build_type: Release, enable_runtime: OFF},
{runs-on: self-hosted, build_type: Release, enable_runtime: ON},
]

runs-on: ${{ matrix.build.runs-on }}

container:
image: ghcr.io/${{ github.repository }}/tt-mlir-${{ matrix.image }}:latest
options: --user root

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history and tags

- name: Set reusable strings
id: strings
shell: bash
run: |
echo "work-dir=$(pwd)" >> "$GITHUB_OUTPUT"
echo "build-output-dir=$(pwd)/build" >> "$GITHUB_OUTPUT"
- name: Git safe dir
run: git config --global --add safe.directory ${{ steps.strings.outputs.work-dir }}

- name: ccache
uses: hendrikmuhs/[email protected]
with:
create-symlink: true
key: ${{ matrix.build.runs-on }}-runtime-${{ matrix.build.enable_runtime }}-${{ env.SDK_VERSION }}

- name: Configure CMake
shell: bash
run: |
source env/activate
cmake -G Ninja \
-B ${{ steps.strings.outputs.build-output-dir }} \
-DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_BUILD_TYPE=${{ matrix.build.build_type }} \
-DTTMLIR_ENABLE_RUNTIME=${{ matrix.build.enable_runtime }} \
-DTTMLIR_ENABLE_RUNTIME_TESTS=${{ matrix.build.enable_runtime }} \
-S ${{ steps.strings.outputs.work-dir }}
- name: Build
shell: bash
run: |
source env/activate
cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build.build_type }}
- name: Lint
shell: bash
run: |
source env/activate
cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build.build_type }} -- clang-tidy
continue-on-error: true

- name: Run Test
shell: bash
run: |
source env/activate
cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build.build_type }} -- check-ttmlir
- name: Upload Test Report
uses: actions/upload-artifact@v4
with:
name: test-reports-${{ matrix.build.runs-on }}
path: build/test/report.xml

- name: Show Test Report
uses: mikepenz/action-junit-report@v4
if: success() || failure()
with:
report_paths: build/test/report.xml
check_name: MLIR Tests
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build
name: Build on macos-latest

on:
workflow_dispatch:
Expand All @@ -13,7 +13,6 @@ jobs:
fail-fast: false
matrix:
build: [
{runs-on: ubuntu-20.04, c_compiler: clang, cpp_compiler: clang++, build_type: Release, enable_runtime: ON},
{runs-on: macos-latest, c_compiler: clang, cpp_compiler: clang++, build_type: Release, enable_runtime: OFF}
]
runs-on: ${{ matrix.build.runs-on }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
name: On PR and push to main
name: On PR

on:
workflow_dispatch:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

Expand All @@ -14,6 +12,9 @@ jobs:
spdx:
uses: ./.github/workflows/spdx.yml
secrets: inherit
build:
uses: ./.github/workflows/build.yml
macos-build:
uses: ./.github/workflows/macos-build.yml
secrets: inherit
docker-build:
uses: ./.github/workflows/docker-build.yml
secrets: inherit
20 changes: 20 additions & 0 deletions .github/workflows/on-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: On push

on:
workflow_dispatch:
push:
branches: [ "main" ]

jobs:
pre-commit:
uses: ./.github/workflows/pre-commit.yml
secrets: inherit
spdx:
uses: ./.github/workflows/spdx.yml
secrets: inherit
macos-build:
uses: ./.github/workflows/macos-build.yml
secrets: inherit
docker-build:
uses: ./.github/workflows/docker-build.yml
secrets: inherit
63 changes: 60 additions & 3 deletions docs/src/internal-build.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,66 @@
# Internal Build Notes / IRD

- When building the runtime we must use Ubuntu 20.04 docker image
- When building the runtime we must use Ubuntu 22.04 docker image
- When making an IRD reservation use `--docker-image
yyz-gitlab.local.tenstorrent.com:5005/tenstorrent/infra/ird-ubuntu-20-04-amd64:latest`
- You'll have to manaully install a newer version of cmake, at least 3.20, the easiest way to do this is to `pip install cmake` and make sure this one is in your path
yyz-gitlab.local.tenstorrent.com:5005/tenstorrent/infra/ird-ubuntu-22-04-amd64:latest`
- You'll have to manaully install a newer version of cmake, at least 3.22, the easiest way to do this is to `pip install cmake` and make sure this one is in your path
- You'll want LLVM installation to persist IRD reservations, you can achieve this by:
- mkdir /localdev/$USER/ttmlir-toolchain
- When requesting an IRD use `--volumes /localdev/$USER/ttmlir-toolchain:/opt/ttmlir-toolchain`

## Working with Docker Images

Components:
- Dockerfile
- Workflow for building Docker image
- Project build using Docker image

Overview:

The [Dockerfile](.github/Dockerfile) describes how to create an image for building the tt-mlir project file. It starts with a supported base image (Ubuntu 22.04) and installs the necessary packages. The purpose of the Docker build is to:

- Set up build dependencies
- Prepare the tt-mlir toolchain

During the Docker build, the project is built and tests are run to ensure that everything is set up correctly. If any dependencies are missing, the Docker build will fail.

This process also prepopulates caches for Python packages and the ccache cache in the image, which should make subsequent builds faster.

### Building the Docker Image using GitHub Actions

The GitHub Actions workflow [Build and Publish Docker Image](.github/workflows/build-image.yml) builds the Docker image and uploads it to GitHub Packages at https://github.com/orgs/tenstorrent/packages?repo_name=tt-mlir. The image name is tt-mlir-ubuntu-22-04, and we use the git SHA we build from as the tag.

### Building the Docker Image Locally

To test the changes and build the image locally, use the following command:
```bash
docker build -f .github/Dockerfile -t tt-mlir-ubuntu-22-04:latest .
```

### Pushing the Docker Image to GitHub

Images built locally can be pushed to GitHub. First, we need to generate a PAT token with the "write:packages" access enabled. Go to GitHub -> Settings -> Developer settings -> Personal access tokens -> Generate new token.

Authenticate with GitHub Container Registry:
```bash
echo "<my-github-pat>" | docker login ghcr.io -u <username> --password-stdin
```

Add a tag to the built image:
```bash
docker tag tt-mlir-ubuntu-22-04:latest ghcr.io/tenstorrent/tt-mlir/tt-mlir-ubuntu-22-04:latest
```

Push the image:
```bash
docker push ghcr.io/tenstorrent/tt-mlir/tt-mlir-ubuntu-22-04:latest
```

### Using the Image in GitHub Actions Jobs

The GitHub Actions workflow [Build in Docker](.github/workflows/docker-build.yml) uses a Docker container for building:
```yaml
container:
image: ghcr.io/${{ github.repository }}/tt-mlir-ubuntu-22-04:latest
options: --user root
```

0 comments on commit e61ea78

Please sign in to comment.