Skip to content

Commit 2231b2f

Browse files
committed
X-Smart-Squash: Squashed 50 commits:
0116bf7 ccache 7fe4a87 typo 4ba08ca ansible format 31fb575 add .ccache to .gitignore f5e5fd3 ccache on builder image 4c75f3a debug logs for builder 3412c94 create logs 29778a0 typo b22b325 print 3ba1265 key a414bb1 remove debug d12f47b enable s390x caching 5578333 typo 10b4a40 s390x 9c4ffa1 skip docker inject action on s390x 66d4e7f fix path 4a58a89 builtin f16c110 fix deda8ba format 23d5ed4 var ef8e17a order 65b1816 fmt 2378492 simplify 89c69c6 scripts 4170881 version 1b191f8 path be6e04b task time b8f61ba quote 30382e5 cache file for remote vm b8c982b fmt 15c70af var a2f148b update a927675 check dir a77bd93 enable builder, s390x collector 0ba7d0a typo 664e31a dbg 28ace91 broken cache f822219 ls 9d5ccdf rm 8ce9e57 slash e80f8f5 slash 1482ea1 permissions and fix builder ccache 3fb4a1a s390x 4ee116e s390x 0887d1d root 6bb2700 dbg b9e8ed1 Revert "dbg" ab60dff rm opts c85e21e collector bdb810b no ccache avail
1 parent 1e46b0c commit 2231b2f

20 files changed

+353
-4
lines changed

.github/workflows/collector-builder.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,27 @@ jobs:
7474
- name: Set up Docker Buildx
7575
uses: docker/setup-buildx-action@v3
7676

77+
- name: Set up builder ccache
78+
uses: actions/cache@v4
79+
with:
80+
path: ${{ github.workspace }}/builder/.ccache
81+
key: builder-ccache-${{ matrix.arch }}-${{ github.head_ref || github.ref_name }}-${{ github.sha }}
82+
restore-keys: |
83+
builder-ccache-${{ matrix.arch }}-${{ github.head_ref || github.ref_name }}-
84+
builder-ccache-${{ matrix.arch }}-master-
85+
86+
- name: Setup builder ccache in docker cache
87+
if: |
88+
(github.event_name != 'pull_request' && matrix.arch != 's390x') ||
89+
matrix.arch == 'amd64' ||
90+
(contains(github.event.pull_request.labels.*.name, 'run-multiarch-builds') && matrix.arch != 's390x')
91+
uses: reproducible-containers/buildkit-cache-dance@v3.1.2
92+
with:
93+
cache-map: |
94+
{
95+
"${{ github.workspace }}/builder/.ccache": "/root/.ccache"
96+
}
97+
7798
- uses: actions/setup-python@v5
7899
with:
79100
python-version: "3.10"
@@ -152,18 +173,30 @@ jobs:
152173
(contains(github.event.pull_request.labels.*.name, 'run-multiarch-builds') && matrix.arch == 's390x')
153174
timeout-minutes: 480
154175
run: |
176+
[ ! -d "${{ github.workspace }}/builder/.ccache" ] && rm -f "${{ github.workspace }}/builder/.ccache"
177+
ls -al "${{ github.workspace }}/builder/.ccache" || true
155178
ansible-playbook \
156179
-i ansible/ci \
157180
-e build_hosts='job_id_${{ env.JOB_ID }}' \
158181
-e arch='${{ matrix.arch }}' \
182+
-e github_workspace='${{ github.workspace }}' \
159183
-e @'${{ github.workspace }}/ansible/secrets.yml' \
160184
ansible/ci-build-builder.yml
185+
ls -al "${{ github.workspace }}/builder/.ccache" || true
161186
162187
- name: Destroy VMs
163188
if: always() && matrix.arch == 's390x'
164189
run: |
165190
make -C ansible destroy-vms
166191
192+
- name: Store artifacts
193+
if: always()
194+
uses: actions/upload-artifact@v4
195+
with:
196+
name: builder-${{ matrix.arch }}-logs
197+
path: |
198+
${{ github.workspace }}/build_builder_image.log
199+
167200
create-multiarch-manifest:
168201
needs:
169202
- build-builder-image

.github/workflows/collector.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ jobs:
4545
with:
4646
submodules: true
4747

48+
- name: Set up ccache
49+
uses: hendrikmuhs/ccache-action@v1.2
50+
with:
51+
key: ccache-${{ matrix.arch }}-${{ github.head_ref || github.ref_name }}-${{ github.sha }}
52+
restore-keys: |
53+
ccache-${{ matrix.arch }}-${{ github.head_ref || github.ref_name }}-
54+
ccache-${{ matrix.arch }}-master-
55+
4856
- name: Set up QEMU
4957
uses: docker/setup-qemu-action@v3
5058

@@ -84,6 +92,10 @@ jobs:
8492
-e @'${{ github.workspace }}/ansible/secrets.yml' \
8593
ansible/ci-build-collector.yml
8694
95+
- name: Stats for ccache after build
96+
run: |
97+
ccache -s "${{ github.workspace }}/.ccache"
98+
8799
build-collector-image-remote-vm:
88100
name: Build Collector on a remote VM
89101
runs-on: ubuntu-latest
@@ -123,6 +135,15 @@ jobs:
123135
vm-type: rhel-${{ matrix.arch }}
124136
job-tag: builder
125137

138+
- name: Set up ccache
139+
uses: actions/cache@v4
140+
with:
141+
path: ${{ github.workspace }}/.ccache
142+
key: ccache-${{ matrix.arch }}-${{ github.head_ref || github.ref_name }}-${{ github.sha }}
143+
restore-keys: |
144+
ccache-${{ matrix.arch }}-${{ github.head_ref || github.ref_name }}-
145+
ccache-${{ matrix.arch }}-master-
146+
126147
- name: Create Build VMs
127148
run: |
128149
make -C "${{ github.workspace }}/ansible" create-build-vms
@@ -152,6 +173,7 @@ jobs:
152173
-i ansible/ci \
153174
-e arch='${{ matrix.arch }}' \
154175
-e build_hosts='job_id_${{ env.JOB_ID }}' \
176+
-e github_workspace='${{ github.workspace }}' \
155177
-e @'${{ github.workspace }}/ansible/secrets.yml' \
156178
ansible/ci-build-collector.yml
157179

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.idea/
22
.rox/
3+
.ccache/
4+
builder/.ccache/
35

46
integration-tests/container-logs/
57
integration-tests/*.log

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ NPROCS ?= $(shell nproc)
66
DEV_SSH_SERVER_KEY ?= $(CURDIR)/.collector_dev_ssh_host_ed25519_key
77
BUILD_BUILDER_IMAGE ?= false
88

9+
CCACHE_DIR?=$(CURDIR)/.ccache
10+
911
export COLLECTOR_VERSION := $(COLLECTOR_TAG)
1012

1113
.PHONY: tag
@@ -25,9 +27,11 @@ container-dockerfile-dev:
2527
builder:
2628
ifneq ($(BUILD_BUILDER_IMAGE), false)
2729
docker buildx build --load --platform ${PLATFORM} \
30+
--build-arg USE_CCACHE="${USE_CCACHE}" \
2831
-t quay.io/stackrox-io/collector-builder:$(COLLECTOR_BUILDER_TAG) \
2932
-f "$(CURDIR)/builder/Dockerfile" \
30-
"$(CURDIR)/builder"
33+
"$(CURDIR)/builder" \
34+
2>&1 | tee build_builder_image.log
3135
endif
3236

3337
collector: check-builder
@@ -84,6 +88,7 @@ start-builder: builder teardown-builder
8488
--name $(COLLECTOR_BUILDER_NAME) \
8589
--pull missing \
8690
--platform ${PLATFORM} \
91+
-v $(CCACHE_DIR):/root/.ccache \
8792
-v $(CURDIR):$(CURDIR) \
8893
$(if $(LOCAL_SSH_PORT),-p $(LOCAL_SSH_PORT):22 )\
8994
-w $(CURDIR) \

Makefile-constants.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ BPF_DEBUG_MODE ?= false
1919

2020
COLLECTOR_BUILD_CONTEXT = collector/
2121
COLLECTOR_BUILDER_NAME ?= collector_builder_$(HOST_ARCH)
22+
USE_CCACHE ?= true

ansible/ansible.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ display_skipped_hosts = false
55
host_key_checking = false
66
remote_tmp = /tmp/ansible
77
forks = 20
8+
callbacks_enabled = profile_tasks
89

910
[ssh_connection]
1011
ssh_args = -o StrictHostKeyChecking=no -C -o ControlMaster=auto -o ControlPersist=60s -o ServerAliveInterval=30 -o ServerAliveCountMax=10

ansible/ci-build-builder.yml

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
- name: Build and push collector image
2+
- name: Build and push builder image
33
hosts: "{{ build_hosts | default('all') }}"
44

55
environment:
@@ -10,6 +10,10 @@
1010
vars:
1111
collector_root: "{{ ansible_env.HOME }}/collector"
1212
local_branch: local
13+
ccache_dir: "builder/.ccache"
14+
ccache_archive: "docker.tar.gz"
15+
ccache_path: "{{ ccache_dir }}/{{ ccache_archive }}"
16+
container_ccache_dir: "/root/.ccache"
1317

1418
tasks:
1519
- name: Clone repository
@@ -21,13 +25,55 @@
2125
# than with commit hashes, prevents "reference is not a tree" errors
2226
version: "{{ local_branch }}"
2327
refspec: "+{{ collector_git_ref | replace('refs/', '') }}:{{ local_branch }}"
28+
depth: 1
2429
recursive: true
2530
when: arch == "s390x"
2631

32+
- name: Check if ccache archive exists
33+
delegate_to: localhost
34+
ansible.builtin.stat:
35+
path: "{{ github_workspace }}/{{ ccache_path }}"
36+
register: ccache_check
37+
when: arch == "s390x"
38+
39+
- name: Copy ccache to build
40+
ansible.builtin.copy:
41+
src: "{{ github_workspace }}/{{ ccache_path }}"
42+
dest: "{{ collector_root }}/{{ ccache_dir }}/"
43+
when: arch == "s390x" and ccache_check.stat.exists
44+
45+
- name: Inject docker cache
46+
ansible.builtin.shell:
47+
cmd: ansible/scripts/inject_docker_cache.sh "{{ collector_root }}/{{ ccache_dir }}" "{{ container_ccache_dir }}"
48+
chdir: "{{ collector_root }}"
49+
when: arch == "s390x" and ccache_check.stat.exists
50+
2751
- name: Build the collector builder image
2852
community.general.make:
2953
chdir: "{{ ansible_env.GITHUB_WORKSPACE | default(collector_root) }}"
3054
target: builder
55+
params:
56+
USE_CCACHE: "true"
57+
58+
- name: Extract docker cache
59+
ansible.builtin.shell:
60+
cmd: ansible/scripts/extract_docker_cache.sh "{{ collector_root }}/{{ ccache_dir }}" "{{ container_ccache_dir }}"
61+
chdir: "{{ collector_root }}"
62+
when: arch == "s390x"
63+
64+
- name: Fetch ccache
65+
ansible.builtin.fetch:
66+
src: "{{ collector_root }}/{{ ccache_path }}"
67+
dest: "{{ github_workspace }}/{{ ccache_dir }}/"
68+
flat: yes
69+
when: arch == "s390x"
70+
71+
- name: Fetch build log
72+
ansible.builtin.fetch:
73+
src: "{{ collector_root }}/build_builder_image.log"
74+
dest: "{{ github_workspace }}/"
75+
flat: yes
76+
when: arch == "s390x"
3177

3278
- name: Retag collector builder image to arch specific
3379
community.docker.docker_image:

ansible/ci-build-collector.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
vars:
1313
collector_root: "{{ ansible_env.HOME }}/collector"
1414
local_branch: local
15+
ccache_dir: ".ccache"
16+
ccache_archive: "docker.tar.gz"
1517

1618
tasks:
1719
- debug: var=collector_root
@@ -25,8 +27,29 @@
2527
version: "{{ local_branch }}"
2628
refspec: "+{{ collector_git_ref | replace('refs/', '') }}:{{ local_branch }}"
2729
recursive: true
30+
depth: 1
2831
when: arch == "s390x"
2932

33+
- name: Check if remote ccache archive exists on controller
34+
delegate_to: localhost
35+
ansible.builtin.stat:
36+
path: "{{ github_workspace }}/{{ ccache_dir }}/{{ ccache_archive }}"
37+
register: ccache_check
38+
when: arch == "s390x"
39+
40+
- name: Copy ccache from the controller to build VM
41+
ansible.builtin.copy:
42+
src: "{{ github_workspace }}/{{ ccache_dir }}/{{ ccache_archive }}"
43+
dest: "{{ collector_root }}/"
44+
when: arch == "s390x" and ccache_check.stat.exists
45+
46+
- name: Extract ccache archive if found
47+
ansible.builtin.unarchive:
48+
src: "{{ collector_root }}/{{ ccache_archive }}"
49+
dest: "{{ collector_root }}/{{ ccache_dir }}"
50+
remote_src: true
51+
when: arch == "s390x" and ccache_check.stat.exists
52+
3053
- name: Run the builder image
3154
community.general.make:
3255
chdir: "{{ ansible_env.GITHUB_WORKSPACE | default(collector_root) }}"
@@ -36,10 +59,27 @@
3659
community.general.make:
3760
chdir: "{{ ansible_env.GITHUB_WORKSPACE | default(collector_root) }}"
3861
target: image
62+
params:
63+
CCACHE_DIR: "{{ ansible_env.GITHUB_WORKSPACE | default(collector_root) }}/.ccache"
64+
USE_CCACHE: true
3965
register: build_result
4066
# ensure this action is printed
4167
tags: [print_action]
4268

69+
- name: Create ccache archive
70+
ansible.builtin.shell: |
71+
rm -f "{{ collector_root }}/{{ ccache_archive }}"
72+
cd "{{ collector_root }}/{{ ccache_dir }}"
73+
tar czf "{{ collector_root }}/{{ ccache_archive }}" .
74+
when: arch == "s390x"
75+
76+
- name: Copy ccache from build machine to controller
77+
ansible.builtin.fetch:
78+
src: "{{ collector_root }}/{{ ccache_archive }}"
79+
dest: "{{ github_workspace }}/{{ ccache_dir }}/"
80+
flat: yes
81+
when: arch == "s390x"
82+
4383
- name: Retag collector image to arch specific
4484
community.docker.docker_image:
4585
name: "{{ collector_image }}"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/bash
2+
# based on https://github.com/reproducible-containers/buildkit-cache-dance
3+
4+
set -e
5+
6+
usage() {
7+
echo "Usage: $0 <cache_destination_directory> <target_path_inside_container>"
8+
exit 1
9+
}
10+
11+
if [ "$#" -ne 2 ]; then
12+
usage
13+
fi
14+
15+
CACHE_DEST="$1"
16+
TARGET_PATH="$2"
17+
18+
CCACHE_ARCHIVE="docker.tar.gz"
19+
20+
SCRATCH_DIR=$(mktemp -d)
21+
SCRATCH_ARCHIVE=$(mktemp)
22+
# shellcheck disable=SC2064
23+
trap "rm -rf ${SCRATCH_DIR}; rm -f ${SCRATCH_ARCHIVE}" EXIT
24+
25+
# Timestamp to bust cache
26+
date -Iseconds > "${SCRATCH_DIR}/buildstamp"
27+
28+
cat << EOF > "${SCRATCH_DIR}/Dockerfile.extract"
29+
FROM busybox:1
30+
COPY buildstamp buildstamp
31+
RUN --mount=type=cache,target=${TARGET_PATH} \\
32+
mkdir -p /var/docker-cache/ \\
33+
&& cp -p -R ${TARGET_PATH}/. /var/docker-cache/ || true
34+
EOF
35+
36+
echo "Generated Dockerfile.extract"
37+
cat "${SCRATCH_DIR}/Dockerfile.extract"
38+
39+
# Build the image and load it into Docker
40+
docker buildx build -f "${SCRATCH_DIR}/Dockerfile.extract" --tag cache:extract --load "${SCRATCH_DIR}"
41+
docker images
42+
43+
# Remove any existing cache-container
44+
docker rm -f cache-container || true
45+
46+
# Create a container from cache:extract
47+
docker create --name cache-container cache:extract
48+
docker ps
49+
50+
# Extract the cache from the container
51+
docker cp -L cache-container:/var/docker-cache - | tar -H posix -x -C "${SCRATCH_DIR}"
52+
ls "${SCRATCH_DIR}"
53+
54+
# Compress the cache from the container
55+
(cd "${SCRATCH_DIR}/docker-cache" && chmod -R 777 . && tar czf "${SCRATCH_ARCHIVE}" .)
56+
57+
# Move the cache into its dest
58+
rm -f "${CACHE_DEST}/${CCACHE_ARCHIVE}"
59+
mv "${SCRATCH_ARCHIVE}" "${CACHE_DEST}/${CCACHE_ARCHIVE}"
60+
chmod 666 "${CACHE_DEST}/${CCACHE_ARCHIVE}"
61+
62+
echo "Docker cache extraction completed successfully."

0 commit comments

Comments
 (0)