Skip to content

Commit

Permalink
add C# "build_package" task
Browse files Browse the repository at this point in the history
  • Loading branch information
jtattermusch committed Sep 28, 2023
1 parent c28380f commit 416041e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 8 deletions.
9 changes: 8 additions & 1 deletion tools/bazelify_tests/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,14 @@ def grpc_run_simple_command_test(name, args = [], data = [], size = "medium", ti
env = {}
_dockerized_sh_test(name = name, srcs = srcs, args = args, data = data, size = size, timeout = timeout, tags = tags, exec_compatible_with = exec_compatible_with, flaky = flaky, docker_image_version = docker_image_version, env = env, docker_run_as_root = False)

def grpc_build_artifact_task(name, timeout = None, tags = [], exec_compatible_with = [], flaky = None, docker_image_version = None, build_script = None):
def grpc_build_artifact_task(name, timeout = None, artifact_deps = [], tags = [], exec_compatible_with = [], flaky = None, docker_image_version = None, build_script = None):
"""Execute a build artifact task and a corresponding 'build test'
Args:
name: The name of the target.
timeout: The test timeout for the build.
artifact_deps: List of dependencies on artifacts built by another grpc_build_artifact_task.
tags: The tags for the target.
exec_compatible_with: A list of constraint values that must be
satisifed for the platform.
Expand All @@ -280,6 +281,12 @@ def grpc_build_artifact_task(name, timeout = None, tags = [], exec_compatible_wi

cmd = "$(location //tools/bazelify_tests:grpc_build_artifact_task.sh) $(location //tools/bazelify_tests:grpc_repo_archive_with_submodules.tar.gz) $(location " + build_script + ") $(location " + out_exitcode_file + ") $(location " + out_build_log + ") $(location " + out_archive_name + ")"

# for each artifact task we depends on, use the correponding tar.gz as extra src and pass its location as an extra cmdline arg.
for dep in artifact_deps:
dep_archive_name = str(dep + ".tar.gz")
cmd = cmd + " $(location " + dep_archive_name + ")"
genrule_srcs.append(dep_archive_name)

_dockerized_genrule(name = name, cmd = cmd, outs = genrule_outs, srcs = genrule_srcs, timeout = timeout, tags = tags, exec_compatible_with = exec_compatible_with, flaky = flaky, docker_image_version = docker_image_version, docker_run_as_root = False)

test_name = str(name + "_build_test")
Expand Down
17 changes: 16 additions & 1 deletion tools/bazelify_tests/grpc_build_artifact_task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,27 @@ shift 5
tar -xopf ${ARCHIVE_WITH_SUBMODULES}
cd grpc

# Extract all input archives with artifacts into input_artifacts directory
mkdir -p input_artifacts
pushd input_artifacts >/dev/null
# all remaining args are .tar.gz archives with input artifacts
for input_artifact_archive in "$@"
do
# extract the .tar.gz with artifacts into a directory named after a basename
# of the archive itself (and strip the "artifact/" prefix)
# Note that input artifacts from different dependencies can have files
# with the same name, so disambiguating through the name of the archive
# is important.
tar --strip-components=1 --one-top-level -xopf ../../${input_artifact_archive}
done
popd >/dev/null

mkdir -p artifacts

# Run the build script with args, storing its stdout and stderr
# in a log file.
SCRIPT_EXIT_CODE=0
../"${BUILD_SCRIPT}" "$@" >"../${SCRIPT_LOG_FILE}" 2>&1 || SCRIPT_EXIT_CODE="$?"
../"${BUILD_SCRIPT}" >"../${SCRIPT_LOG_FILE}" 2>&1 || SCRIPT_EXIT_CODE="$?"

# Store build script's exitcode in a file.
# Note that the build atifacts task will terminate with success even when
Expand Down
15 changes: 9 additions & 6 deletions tools/bazelify_tests/grpc_build_artifact_task_build_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ shift 3

BUILD_ARTIFACT_EXITCODE="$(cat ${EXIT_CODE_FILE})"

echo "Build artifact task for '${ARTIFACTS_ARCHIVE}' has finished with exitcode ${BUILD_ARTIFACT_EXITCODE}."
echo "Build artifact/package task for '${ARTIFACTS_ARCHIVE}' has finished with exitcode ${BUILD_ARTIFACT_EXITCODE}."

echo "BUILD LOG"
echo "--------------"
Expand All @@ -35,18 +35,21 @@ mkdir -p input_artifacts
pushd input_artifacts >/dev/null
echo "Artifacts that were built by the build artifact task:"
echo "--------------"
# TODO(jtattermusch): strip top level artifacts/ directory from the archive?
tar -xopvf ../${ARTIFACTS_ARCHIVE}
echo "--------------"
popd >/dev/null

# TODO(jtattermusch): consider adding the contents of artifacts archive
# to bazel "undeclared test outputs" directory to make them available in the resultstore UI
# easily. See docs for TEST_UNDECLARED_OUTPUTS_DIR for details.
# Add artifact archive to the "undeclared test outputs" directory
# to make it readily available in the resultstore UI.
# See bazel docs for TEST_UNDECLARED_OUTPUTS_DIR.
mkdir -p "${TEST_UNDECLARED_OUTPUTS_DIR}"
cp "${ARTIFACTS_ARCHIVE}" "${TEST_UNDECLARED_OUTPUTS_DIR}" || true

if [ "${BUILD_ARTIFACT_EXITCODE}" -eq "0" ]
then
echo "SUCCESS: Artifact build task for '${ARTIFACTS_ARCHIVE}' to build 'ran successfully."
echo "SUCCESS: Build artifact/package task for '${ARTIFACTS_ARCHIVE}' ran successfully."
else
echo "FAIL: Artifact build task for '${ARTIFACTS_ARCHIVE}' failed with exitcode ${BUILD_ARTIFACT_EXITCODE}."
echo "FAIL: Build artifact/package task for '${ARTIFACTS_ARCHIVE}' failed with exitcode ${BUILD_ARTIFACT_EXITCODE}."
exit 1
fi
15 changes: 15 additions & 0 deletions tools/bazelify_tests/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ grpc_build_artifact_task(

# TODO(jtattermusch): add more grpc_build_artifact_task targets for existing python artifacts from artifact_targets.py

# C# package build tasks

grpc_build_artifact_task(
name = "package_csharp_linux",
# csharp package needs pre-built protoc and protoc plugin binaries
artifact_deps = [
"artifact_protoc_linux_x64",
"artifact_protoc_linux_x86",
"artifact_protoc_linux_aarch64",
],
build_script = "build_package_csharp_linux.sh",
docker_image_version = "tools/dockerfile/test/csharp_debian11_x64.current_version",
)

# TODO(jtattermusch): add grpc_build_artifact_task targets for ruby artifacts (which is tricky, since ruby artifact builds do not run under docker since they invoke docker themselves)

test_suite(
Expand All @@ -290,6 +304,7 @@ test_suite(
":artifact_protoc_linux_x64_build_test",
":artifact_protoc_linux_x86_build_test",
":artifact_python_linux_x64_manylinux2014_cp311_build_test",
":package_csharp_linux_build_test",
],
)

Expand Down
43 changes: 43 additions & 0 deletions tools/bazelify_tests/test/build_package_csharp_linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash
# Copyright 2023 The gRPC Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -ex

mkdir -p artifacts

# List all input artifacts we obtained for easier troubleshooting.
ls -lR input_artifacts

# Put the input artifacts where the legacy logic for building
# C# package expects to find them.
# See artifact_targets.py and package_targets.py for details.
# TODO(jtattermusch): get rid of the manual renames of artifact directories.
export EXTERNAL_GIT_ROOT="$(pwd)"
mv input_artifacts/artifact_protoc_linux_aarch64 input_artifacts/protoc_linux_aarch64 || true
mv input_artifacts/artifact_protoc_linux_x64 input_artifacts/protoc_linux_x64 || true
mv input_artifacts/artifact_protoc_linux_x86 input_artifacts/protoc_linux_x86 || true

# In the bazel workflow, we only have linux protoc artifact at hand,
# so we can only build a "singleplatform" version of the C# package.
export GRPC_CSHARP_BUILD_SINGLE_PLATFORM_NUGET=1

# TODO(jtattermusch): when building the C# nugets, the current git commit SHA
# is retrieved and stored as package metadata. But when running
# as bazelified test, this is not possible since we're not in a git
# workspace when running the build. This is ok for testing purposes
# but would be a problem if building a production package
# for the end users.

src/csharp/build_nuget.sh

0 comments on commit 416041e

Please sign in to comment.