Skip to content

Commit e9cafe6

Browse files
authored
Merge pull request #37 from xcp-ng/gln/build-env-improvements-lzqx
2 parents 8e0426d + 1c18d26 commit e9cafe6

File tree

7 files changed

+168
-187
lines changed

7 files changed

+168
-187
lines changed

.github/workflows/docker.yml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: Build and Push Docker Image to GHCR
2+
3+
on: push
4+
5+
permissions:
6+
contents: read # Required to checkout the repo code
7+
packages: write # Required to push packages to GHCR
8+
9+
jobs:
10+
xcp-ng-build-env-82:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: docker/setup-buildx-action@v3
15+
with:
16+
driver: docker-container
17+
- uses: docker/login-action@v3
18+
if: github.ref == 'refs/heads/master'
19+
with:
20+
registry: ghcr.io
21+
username: ${{ github.actor }} # Uses the GitHub user/org name that triggered the workflow
22+
password: ${{ secrets.GITHUB_TOKEN }} # Automatically provided by GitHub
23+
- uses: docker/build-push-action@v5 # Using v5 for latest features
24+
with:
25+
context: ./src/xcp_ng_dev/
26+
file: ./src/xcp_ng_dev/files/Dockerfile-8.x
27+
push: ${{ github.ref == 'refs/heads/master' }}
28+
tags: ghcr.io/${{ github.repository }}:8.2
29+
cache-from: type=gha,scope=${{ github.ref_name }}-82 # Cache layers to speed up builds
30+
cache-to: type=gha,mode=max,scope=${{ github.ref_name }}-82 # Store layers in cache for future builds
31+
build-args: |
32+
XCP_NG_BRANCH=8.2
33+
platforms: |
34+
linux/amd64
35+
36+
xcp-ng-build-env-83:
37+
runs-on: ubuntu-latest
38+
steps:
39+
- uses: actions/checkout@v4
40+
- uses: docker/setup-buildx-action@v3
41+
with:
42+
driver: docker-container
43+
- uses: docker/login-action@v3
44+
if: github.ref == 'refs/heads/master'
45+
with:
46+
registry: ghcr.io
47+
username: ${{ github.actor }} # Uses the GitHub user/org name that triggered the workflow
48+
password: ${{ secrets.GITHUB_TOKEN }} # Automatically provided by GitHub
49+
- uses: docker/build-push-action@v5 # Using v5 for latest features
50+
with:
51+
context: ./src/xcp_ng_dev/
52+
file: ./src/xcp_ng_dev/files/Dockerfile-8.x
53+
push: ${{ github.ref == 'refs/heads/master' }}
54+
tags: ghcr.io/${{ github.repository }}:8.3
55+
cache-from: type=gha,scope=${{ github.ref_name }}-83 # Cache layers to speed up builds
56+
cache-to: type=gha,mode=max,scope=${{ github.ref_name }}-83 # Store layers in cache for future builds
57+
platforms: |
58+
linux/amd64
59+
60+
# TODO: uncomment once we have a public xcp-ng 9.0 repository
61+
# xcp-ng-build-env-90:
62+
# runs-on: ubuntu-latest
63+
# steps:
64+
# - uses: actions/checkout@v4
65+
# - uses: docker/setup-buildx-action@v3
66+
# with:
67+
# driver: docker-container
68+
# - uses: docker/login-action@v3
69+
# if: github.ref == 'refs/heads/master'
70+
# with:
71+
# registry: ghcr.io
72+
# username: ${{ github.actor }} # Uses the GitHub user/org name that triggered the workflow
73+
# password: ${{ secrets.GITHUB_TOKEN }} # Automatically provided by GitHub
74+
# - uses: docker/build-push-action@v5 # Using v5 for latest features
75+
# with:
76+
# context: ./src/xcp_ng_dev/
77+
# file: ./src/xcp_ng_dev/files/Dockerfile-9.x
78+
# platforms: |
79+
# linux/amd64/v2
80+
# push: ${{ github.ref == 'refs/heads/master' }}
81+
# tags: ghcr.io/${{ github.repository }}:9.0
82+
# cache-from: type=gha,scope=${{ github.ref_name }}-90 # Cache layers to speed up builds
83+
# cache-to: type=gha,mode=max,scope=${{ github.ref_name }}-90 # Store layers in cache for future builds

src/xcp_ng_dev/build.sh

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ fi
6767

6868
cd $(dirname "$0")
6969

70-
CUSTOM_ARGS=()
71-
7270
ALMA_VERSION=
7371
CENTOS_VERSION=
7472
case "$1" in
@@ -81,38 +79,14 @@ case "$1" in
8179
DOCKERFILE=files/Dockerfile-8.x
8280
: ${PLATFORM:=linux/amd64}
8381
;;
84-
7.*)
85-
DOCKERFILE=Dockerfile-7.x
86-
: ${PLATFORM:=linux/amd64}
87-
;;
8882
*)
8983
echo >&2 "Unsupported release '$1'"
9084
exit 1
9185
;;
9286
esac
9387

94-
CUSTOM_UID="$(id -u)"
95-
CUSTOM_GID="$(id -g)"
96-
97-
if [ "${CUSTOM_UID}" -eq 0 ] || [ "${CUSTOM_GID}" -eq 0 ]; then
98-
if [ -z "${SUDO_GID}" ] || [ -z "${SUDO_UID}" ] || [ -z "${SUDO_USER}" ] || \
99-
[ -z "${SUDO_COMMAND}" ] || [ "${SUDO_GID}" -eq 0 ] || [ "${SUDO_UID}" -eq 0 ]; then
100-
echo -e "[ERROR] This operation cannot be performed by the 'root' user directly:"
101-
echo -e "\tplease use an unprivileged user (eventually with 'sudo')"
102-
exit 1
103-
fi
104-
CUSTOM_UID="${SUDO_UID}"
105-
CUSTOM_GID="${SUDO_GID}"
106-
fi
107-
108-
# Support for seamless use of current host user
109-
# and Docker user "builder" inside the image
110-
CUSTOM_ARGS+=( "--build-arg" "CUSTOM_BUILDER_UID=${CUSTOM_UID}" )
111-
CUSTOM_ARGS+=( "--build-arg" "CUSTOM_BUILDER_GID=${CUSTOM_GID}" )
112-
11388
"$RUNNER" build \
11489
--platform "$PLATFORM" \
115-
"${CUSTOM_ARGS[@]}" \
11690
-t ghcr.io/xcp-ng/xcp-ng-build-env:${1} \
11791
--build-arg XCP_NG_BRANCH=${1} \
11892
--ulimit nofile=1024 \

src/xcp_ng_dev/cli.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,17 @@ def buildparser():
144144
return parser
145145

146146
def container(args):
147-
docker_args = [RUNNER, "run", "-i", "-t",
148-
"-u", "builder",
149-
]
147+
docker_args = [RUNNER, "run", "-i", "-t"]
148+
150149
if is_podman(RUNNER):
151-
docker_args += ["--userns=keep-id", "--security-opt", "label=disable"]
150+
# With podman we use the `--userns` option to map the builder user to the user on the system.
151+
# The container will start with that user and not as root as with docker
152+
docker_args += ["--userns=keep-id:uid=1000,gid=1000", "--security-opt", "label=disable"]
153+
else:
154+
# With docker, the container starts as root and modify the builder user in the entrypoint to
155+
# match the uid:gid of the user launching the container, and then continue with the builder
156+
# user thanks to gosu.
157+
docker_args += ["-e", f'BUILDER_UID={os.getuid()}', "-e", f'BUILDER_GID={os.getgid()}']
152158

153159
# common args
154160
if args.no_exit:

src/xcp_ng_dev/files/Dockerfile-7.x

Lines changed: 0 additions & 78 deletions
This file was deleted.

src/xcp_ng_dev/files/Dockerfile-8.x

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,32 @@ ARG CENTOS_VERSION=7.5.1804
22

33
FROM centos:${CENTOS_VERSION}
44

5-
ARG CUSTOM_BUILDER_UID=""
6-
ARG CUSTOM_BUILDER_GID=""
7-
85
# Remove all repositories
96
RUN rm /etc/yum.repos.d/*
107

118
# Add only the specific CentOS 7.5 repositories, because that's what XS used for the majority of packages
129
ARG CENTOS_VERSION
1310
COPY files/CentOS-Vault.repo.in /etc/yum.repos.d/CentOS-Vault-7.5.repo
14-
RUN sed -e "s/@CENTOS_VERSION@/${CENTOS_VERSION}/g" -i /etc/yum.repos.d/CentOS-Vault-7.5.repo
11+
RUN sed -i -e "s/@CENTOS_VERSION@/${CENTOS_VERSION}/g" /etc/yum.repos.d/CentOS-Vault-7.5.repo
1512

1613
# Add our repositories
1714
# Repository file depends on the target version of XCP-ng, and is pre-processed by build.sh
1815
ARG XCP_NG_BRANCH=8.3
1916
COPY files/xcp-ng.repo.8.x.in /etc/yum.repos.d/xcp-ng.repo
20-
RUN sed -e "s/@XCP_NG_BRANCH@/${XCP_NG_BRANCH}/g" -i /etc/yum.repos.d/xcp-ng.repo
17+
RUN sed -i -e "s/@XCP_NG_BRANCH@/${XCP_NG_BRANCH}/g" /etc/yum.repos.d/xcp-ng.repo
2118

2219
# Install GPG key
2320
RUN curl -sSf https://xcp-ng.org/RPM-GPG-KEY-xcpng -o /etc/pki/rpm-gpg/RPM-GPG-KEY-xcpng
2421

25-
# Fix invalid rpmdb checksum error with overlayfs, see https://github.com/docker/docker/issues/10180
26-
# (still needed?)
27-
RUN yum install -y yum-plugin-ovl
28-
29-
# Use priorities so that packages from our repositories are preferred over those from CentOS repositories
30-
RUN yum install -y yum-plugin-priorities
31-
3222
# Update
33-
RUN yum update -y
34-
35-
# Common build requirements
36-
RUN yum install -y \
23+
RUN yum update -y \
24+
# Fix invalid rpmdb checksum error with overlayfs, see https://github.com/docker/docker/issues/10180
25+
# (still needed?)
26+
&& yum install -y yum-plugin-ovl \
27+
# Use priorities so that packages from our repositories are preferred over those from CentOS repositories
28+
&& yum install -y yum-plugin-priorities \
29+
# Common build requirements
30+
&& yum install -y \
3731
gcc \
3832
gcc-c++ \
3933
git \
@@ -44,37 +38,30 @@ RUN yum install -y \
4438
sudo \
4539
yum-utils \
4640
epel-release \
47-
epel-rpm-macros
48-
49-
# Niceties
50-
RUN yum install -y \
41+
epel-rpm-macros \
42+
# Niceties
43+
&& yum install -y \
5144
vim \
5245
wget \
53-
which
54-
55-
# clean package cache to avoid download errors
56-
RUN yum clean all
46+
which \
47+
# clean package cache to avoid download errors
48+
&& yum clean all
5749

5850
# OCaml in XS may be older than in CentOS
5951
RUN sed -i "/gpgkey/a exclude=ocaml*" /etc/yum.repos.d/Cent* /etc/yum.repos.d/epel*
6052

61-
# Set up the builder user
62-
RUN bash -c ' \
63-
OPTS=(); \
64-
if [ -n "${CUSTOM_BUILDER_UID}" ]; then \
65-
OPTS+=("-u" "${CUSTOM_BUILDER_UID}"); \
66-
fi; \
67-
if [ -n "${CUSTOM_BUILDER_GID}" ]; then \
68-
OPTS+=("-g" "${CUSTOM_BUILDER_GID}"); \
69-
if ! getent group "${CUSTOM_BUILDER_GID}" >/dev/null; then \
70-
groupadd -g "${CUSTOM_BUILDER_GID}" builder; \
71-
fi; \
72-
fi; \
73-
useradd "${OPTS[@]}" builder; \
74-
' \
53+
# create the builder user
54+
RUN groupadd -g 1000 builder \
55+
&& useradd -u 1000 -g 1000 builder \
7556
&& echo "builder:builder" | chpasswd \
7657
&& echo "builder ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers
7758

7859
RUN mkdir -p /usr/local/bin
60+
RUN curl -fsSL "https://github.com/tianon/gosu/releases/download/1.17/gosu-amd64" -o /usr/local/bin/gosu \
61+
&& chmod +x /usr/local/bin/gosu
7962
COPY files/init-container.sh /usr/local/bin/init-container.sh
80-
COPY files/rpmmacros /home/builder/.rpmmacros
63+
COPY files/entrypoint.sh /usr/local/bin/entrypoint.sh
64+
COPY --chown=builder:builder files/rpmmacros /home/builder/.rpmmacros
65+
66+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
67+
CMD ["bash"]

0 commit comments

Comments
 (0)