Skip to content

Commit

Permalink
Implemented conditional Windows Docker image pulling based on file mo…
Browse files Browse the repository at this point in the history
…dification

Added functionality to check for modifications in specific files using Git and set a PULL_MASK accordingly.
This update ensures that images are only updated based on actual changes to relevant files in CI.

Signed-off-by: Shuyang Xin <[email protected]>
  • Loading branch information
XinShuYang committed Apr 26, 2024
1 parent a6ee62e commit 1080b45
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
5 changes: 1 addition & 4 deletions build/images/base-windows/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ RUN Invoke-WebRequest -UseBasicParsing -Uri https://www.7-zip.org/a/7z2107-x64.e
Invoke-WebRequest -UseBasicParsing -Uri https://cfhcable.dl.sourceforge.net/project/mingw-w64/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/seh/x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z -OutFile mingw.7z; \
7z x mingw.7z; cp c:/mingw64/bin/mingw32-make.exe c:/mingw64/bin/make.exe; \
Invoke-WebRequest -UseBasicParsing -Uri https://github.com/git-for-windows/git/releases/download/v2.35.1.windows.2/PortableGit-2.35.1.2-64-bit.7z.exe -OutFile git.exe; \
7z x git.exe -oC:\git; \
mkdir C:\wins; \
Invoke-WebRequest -UseBasicParsing -Uri https://github.com/rancher/wins/releases/download/v0.0.4/wins.exe -OutFile C:/wins/wins.exe
7z x git.exe -oC:\git;

FROM golang:${GO_VERSION}-nanoserver as windows-golang

Expand All @@ -57,7 +55,6 @@ COPY --from=windows-utility-base ["C:\\\\opt\\\\cni\\\\bin","C:\\\\opt\\\\cni\\\
COPY --from=windows-utility-base ["C:\\\\Program Files\\\\7-Zip","C:\\\\Program Files\\\\7-Zip"]
COPY --from=windows-utility-base ["C:\\\\git","C:\\\\git"]
COPY --from=windows-utility-base ["C:\\\\mingw64","C:\\\\mingw64"]
COPY --from=windows-utility-base ["C:\\\\wins","C:\\\\wins"]
COPY --from=windows-golang ["C:\\\\Program Files\\\\Go", "C:\\\\Program Files\\\\Go"]

RUN setx /m PATH "C:\Program Files\7-Zip;C:\git\bin;C:\git\usr\bin;C:\mingw64\bin;C:\Program Files\Go\bin;%GOPATH\bin%;%PATH%"
19 changes: 18 additions & 1 deletion ci/jenkins/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ IP_MODE=""
K8S_VERSION="1.28.2-00"
WINDOWS_YAML_SUFFIX="windows"
WIN_IMAGE_NODE=""
PULL_MASK=0
echo "" > WIN_DHCP
GOLANG_RELEASE_DIR=${WORKDIR}/golang-releases

Expand Down Expand Up @@ -461,7 +462,23 @@ function deliver_antrea_linux_containerd {
echo "==== Finish building and delivering Linux containerd images ===="
}

# Function to check file changes using git
check_file_change() {
local file=$1
local bit=$2
# Check if the file has been modified
if git diff --name-only HEAD | grep -q "$file"; then
# Set the corresponding bit in PULL_MASK if the file is modified
PULL_MASK=$((PULL_MASK | $bit))
fi
}

function deliver_antrea_windows_containerd {
echo "=====Check each base image file and update PULL_MASK accordingly====="
check_file_change "build/images/base-windows/Dockerfile" 1 # Corresponds to 0x1
check_file_change "build/images/base-windows/Dockerfile" 2 # Corresponds to 0x2
check_file_change "build/images/base-windows/Dockerfile" 4 # Corresponds to 0x4
check_file_change "build/images/ovs/Dockerfile.windows" 8 # Corresponds to 0x8
echo "===== Build Antrea Windows on Windows Jumper Node ====="
echo "==== Reverting Windows VM ${WIN_IMAGE_NODE} ====="
revert_snapshot_windows ${WIN_IMAGE_NODE}
Expand All @@ -473,7 +490,7 @@ function deliver_antrea_windows_containerd {
timeout 2m scp -o StrictHostKeyChecking=no -T jenkins/antrea_repo.tar.gz Administrator@${IP}: && break
done
ssh -o StrictHostKeyChecking=no -n Administrator@${IP} "docker pull ${DOCKER_REGISTRY}/antrea/golang:${GO_VERSION}-nanoserver && docker tag ${DOCKER_REGISTRY}/antrea/golang:${GO_VERSION}-nanoserver golang:${GO_VERSION}-nanoserver"
ssh -o StrictHostKeyChecking=no -n Administrator@${IP} "rm -rf antrea && mkdir antrea && cd antrea && tar -xzf ../antrea_repo.tar.gz > /dev/null && NO_PULL=${NO_PULL}; DOCKER_NETWORK=host make build-windows && docker save -o antrea-windows.tar antrea/antrea-windows:latest && gzip -f antrea-windows.tar" || true
ssh -o StrictHostKeyChecking=no -n Administrator@${IP} "rm -rf antrea && mkdir antrea && cd antrea && tar -xzf ../antrea_repo.tar.gz > /dev/null && NO_PULL=${NO_PULL}; ./hack/build-windows-all.sh --pull-mask $PULL_MASK --network host && docker save -o antrea-windows.tar antrea/antrea-windows:latest && gzip -f antrea-windows.tar" || true
for i in `seq 2`; do
timeout 2m scp -o StrictHostKeyChecking=no -T Administrator@${IP}:antrea/antrea-windows.tar.gz . && break
done
Expand Down
42 changes: 37 additions & 5 deletions hack/build-antrea-windows-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ _usage="Usage: $0 [--pull] [--push-base-images]
Build the antrea/antrea-windows image, as well as all the base images in the build chain. This is
typically used in CI to build the image with the latest version of all dependencies, taking into
account changes to all Dockerfiles.
--pull Always attempt to pull a newer version of the base images.
--pull Always attempt to pull a newer version of the base images, ignoring pull-mask.
--pull-mask=mask Binary mask to determine which base images to pull.
0x1 - antrea/windows-utility-base
0x2 - antrea/windows-golang
0x4 - antrea/base-windows
0x8 - antrea/windows-ovs
--push-base-images Push built images to the registry. Only base images will be pushed.
This script must run on a Windows machine!"

Expand All @@ -37,6 +42,7 @@ function print_usage {

PULL=false
PUSH=false
NETWORK="default"

while [[ $# -gt 0 ]]
do
Expand All @@ -47,6 +53,14 @@ case $key in
PULL=true
shift
;;
--network)
NETWORK="$2"
shift
;;
--pull-mask=*)
PULL_MASK=$((0x${key#*=}))
shift
;;
--push-base-images)
PUSH=true
shift
Expand Down Expand Up @@ -75,15 +89,30 @@ WIN_BUILD_TAG=$(echo $GO_VERSION $CNI_BINARIES_VERSION $NANOSERVER_VERSION| md5s

echo "WIN_BUILD_TAG=$WIN_BUILD_TAG"

docker pull mcr.microsoft.com/windows/servercore:$NANOSERVER_VERSION
docker pull golang:$GO_VERSION-nanoserver
docker pull mcr.microsoft.com/windows/nanoserver:$NANOSERVER_VERSION
docker pull mcr.microsoft.com/powershell:lts-nanoserver-$NANOSERVER_VERSION

# Pull all images if --pull is specified, otherwise check individual bits for --pull-mask
if $PULL; then
docker pull mcr.microsoft.com/windows/servercore:$NANOSERVER_VERSION
docker pull golang:$GO_VERSION-nanoserver
docker pull mcr.microsoft.com/windows/nanoserver:$NANOSERVER_VERSION
docker pull mcr.microsoft.com/powershell:lts-nanoserver-$NANOSERVER_VERSION
docker pull antrea/windows-utility-base:$WIN_BUILD_TAG || true
docker pull antrea/windows-golang:$WIN_BUILD_TAG || true
docker pull antrea/base-windows:$WIN_BUILD_TAG || true
docker pull antrea/windows-ovs:$WIN_BUILD_OVS_TAG || true
elif [[ $PULL_MASK -ne 0 ]]; then
if (( $PULL_MASK & 0x1 )); then
docker pull antrea/windows-utility-base:$WIN_BUILD_TAG || true
fi
if (( $PULL_MASK & 0x2 )); then
docker pull antrea/windows-golang:$WIN_BUILD_TAG || true
fi
if (( $PULL_MASK & 0x4 )); then
docker pull antrea/base-windows:$WIN_BUILD_TAG || true
fi
if (( $PULL_MASK & 0x8 )); then
docker pull antrea/windows-ovs:$WIN_BUILD_OVS_TAG || true
fi
fi

cd build/images/base-windows
Expand All @@ -92,12 +121,14 @@ docker build --target windows-utility-base \
-t antrea/windows-utility-base:$WIN_BUILD_TAG \
--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION \
--build-arg NANOSERVER_VERSION=$NANOSERVER_VERSION .
--network $NETWORK
docker build --target windows-golang \
--cache-from antrea/windows-golang:$WIN_BUILD_TAG \
-t antrea/windows-golang:$WIN_BUILD_TAG \
--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION \
--build-arg GO_VERSION=$GO_VERSION \
--build-arg NANOSERVER_VERSION=$NANOSERVER_VERSION .
--network $NETWORK
docker build \
--cache-from antrea/windows-utility-base:$WIN_BUILD_TAG \
--cache-from antrea/windows-golang:$WIN_BUILD_TAG \
Expand All @@ -106,6 +137,7 @@ docker build \
--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION \
--build-arg GO_VERSION=$GO_VERSION \
--build-arg NANOSERVER_VERSION=$NANOSERVER_VERSION .
--network $NETWORK
cd -

cd build/images/ovs
Expand Down

0 comments on commit 1080b45

Please sign in to comment.