Skip to content

Commit 9f93adc

Browse files
committed
[Github][CI] Add separate container for code-format premerge job
1 parent 0fc6213 commit 9f93adc

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: Build CI Container
2+
3+
permissions:
4+
contents: read
5+
6+
on:
7+
push:
8+
branches:
9+
- main
10+
paths:
11+
- .github/workflows/build-ci-container-code-format.yml
12+
- '.github/workflows/containers/github-action-ci-code-format/**'
13+
- llvm/utils/git/code-format-helper.py
14+
- llvm/utils/git/requirements_formatting.txt
15+
- llvm/utils/git/requirements_formatting.txt.in
16+
pull_request:
17+
paths:
18+
- .github/workflows/build-ci-container-code-format.yml
19+
- '.github/workflows/containers/github-action-ci-code-format/**'
20+
- llvm/utils/git/code-format-helper.py
21+
- llvm/utils/git/requirements_formatting.txt
22+
- llvm/utils/git/requirements_formatting.txt.in
23+
24+
jobs:
25+
build-ci-container-code-format:
26+
if: github.repository_owner == 'llvm'
27+
runs-on: depot-ubuntu-24.04-16
28+
steps:
29+
- name: Checkout LLVM
30+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
31+
with:
32+
sparse-checkout: .github/workflows/containers/github-action-ci-code-format/
33+
- name: Write Variables
34+
id: vars
35+
run: |
36+
tag=$(git rev-parse --short=12 HEAD)
37+
container_name="ghcr.io/$GITHUB_REPOSITORY_OWNER/amd64/ci-ubuntu-24.04-code-format"
38+
echo "container-name=$container_name" >> $GITHUB_OUTPUT
39+
echo "container-name-tag=$container_name:$tag" >> $GITHUB_OUTPUT
40+
echo "container-filename=$(echo $container_name:$tag | sed -e 's/\//-/g' -e 's/:/-/g').tar" >> $GITHUB_OUTPUT
41+
- name: Build container
42+
run: |
43+
podman build --target ci-container-code-format \
44+
-f .github/workflows/containers/github-action-ci-code-format/Dockerfile \
45+
-t ${{ steps.vars.outputs.container-name-tag }} .
46+
47+
# Save the container so we have it in case the push fails. This also
48+
# allows us to separate the push step into a different job so we can
49+
# maintain minimal permissions while building the container.
50+
- name: Save container image
51+
run: |
52+
podman save ${{ steps.vars.outputs.container-name-tag }} > ${{ steps.vars.outputs.container-filename }}
53+
54+
- name: Upload container image
55+
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
56+
with:
57+
name: container-amd64
58+
path: "*.tar"
59+
retention-days: 14
60+
61+
- name: Test Container
62+
run: |
63+
for image in ${{ steps.vars.outputs.container-name-tag }}; do
64+
# Use --pull=never to ensure we are testing the just built image.
65+
podman run --pull=never --rm -it $image /usr/bin/bash -x -c 'cd $HOME && clang-format --version | grep version'
66+
done
67+
68+
push-ci-container:
69+
if: github.event_name == 'push'
70+
needs:
71+
- build-ci-container-code-format
72+
permissions:
73+
packages: write
74+
runs-on: ubuntu-24.04
75+
env:
76+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
77+
steps:
78+
- name: Download container
79+
uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
80+
81+
- name: Push Container
82+
run: |
83+
function push_container {
84+
image_name=$1
85+
latest_name=$(echo $image_name | sed 's/:[a-f0-9]\+$/:latest/g')
86+
podman tag $image_name $latest_name
87+
echo "Pushing $image_name ..."
88+
podman push $image_name
89+
echo "Pushing $latest_name ..."
90+
podman push $latest_name
91+
}
92+
93+
podman login -u ${{ github.actor }} -p $GITHUB_TOKEN ghcr.io
94+
for f in $(find . -iname *.tar); do
95+
image_name=$(podman load -q -i $f | sed 's/Loaded image: //g')
96+
push_container $image_name
97+
98+
if echo $image_name | grep '/amd64/'; then
99+
# For amd64, create an alias with the arch component removed.
100+
# This matches the convention used on dockerhub.
101+
default_image_name=$(echo $(dirname $(dirname $image_name))/$(basename $image_name))
102+
podman tag $image_name $default_image_name
103+
push_container $default_image_name
104+
fi
105+
done
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
FROM docker.io/library/ubuntu:24.04 AS base
2+
ENV LLVM_SYSROOT=/opt/llvm
3+
4+
FROM base AS clang-format-toolchain
5+
ENV LLVM_VERSION=21.1.1
6+
7+
RUN apt-get update && \
8+
apt-get install -y \
9+
wget \
10+
gcc \
11+
g++ \
12+
cmake \
13+
ninja-build \
14+
python3 \
15+
git \
16+
curl \
17+
zlib1g-dev && \
18+
apt-get clean && \
19+
rm -rf /var/lib/apt/lists/*
20+
21+
RUN curl -O -L https://github.com/llvm/llvm-project/archive/refs/tags/llvmorg-$LLVM_VERSION.tar.gz && \
22+
tar -xf llvmorg-$LLVM_VERSION.tar.gz && \
23+
rm -f llvmorg-$LLVM_VERSION.tar.gz
24+
25+
WORKDIR /llvm-project-llvmorg-$LLVM_VERSION
26+
27+
RUN cmake -B ./build -G Ninja ./llvm \
28+
-DCMAKE_BUILD_TYPE=Release \
29+
-DCMAKE_INSTALL_PREFIX="$LLVM_SYSROOT" \
30+
-DLLVM_ENABLE_PROJECTS="clang" \
31+
-DLLVM_DISTRIBUTION_COMPONENTS="clang-format"
32+
33+
RUN ninja -C ./build install-distribution
34+
35+
FROM base AS ci-container-code-format
36+
37+
COPY --from=clang-format-toolchain $LLVM_SYSROOT $LLVM_SYSROOT
38+
39+
# Need nodejs for some of the GitHub actions.
40+
# Need git for git-clang-format.
41+
RUN apt-get update && \
42+
DEBIAN_FRONTEND=noninteractive apt-get install -y \
43+
# binutils \
44+
git \
45+
nodejs \
46+
# python3-psutil \
47+
sudo \
48+
# These are needed by the premerge pipeline. Pip and venv are used to
49+
# install dependent python packages.
50+
# Having a symlink from python to python3 enables code sharing between
51+
# the Linux and Windows pipelines.
52+
python3-pip \
53+
python3-venv \
54+
python-is-python3 && \
55+
apt-get clean && \
56+
rm -rf /var/lib/apt/lists/*
57+
58+
ENV LLVM_SYSROOT=$LLVM_SYSROOT
59+
ENV PATH=${LLVM_SYSROOT}/bin:${PATH}
60+
61+
# Create a new user to avoid test failures related to a lack of expected
62+
# permissions issues in some tests. Set the user id to 1001 as that is the
63+
# user id that Github Actions uses to perform the checkout action.
64+
RUN useradd gha -u 1001 -m -s /bin/bash
65+
66+
# Also add the user to passwordless sudoers so that we can install software
67+
# later on without having to rebuild the container.
68+
RUN adduser gha sudo
69+
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
70+
71+
USER gha
72+
WORKDIR /home/gha
73+
74+
COPY llvm/utils/git/requirements_formatting.txt /home/gha/requirements_formatting.txt
75+
RUN python -m venv venv && \
76+
venv/bin/pip install -r /home/gha/requirements_formatting.txt && \
77+
rm /home/gha/requirements_formatting.txt

0 commit comments

Comments
 (0)