Skip to content

Commit 4cbe88d

Browse files
pavoljuhasmhucka
andauthored
Skip cirq pre-release if identical to the last version (#7737)
1. Improve the produce-package.sh script for wheel building - add `-h, --help` option to show usage information - add `-c, --commit=COMMIT` option for building from sources at `COMMIT` instead of `HEAD` - respect `SOURCE_DATE_EPOCH` if available as environment variable. Otherwise get it from the commit time of the sources. - do not purge temporary files if script exits with an error 2. Update the release-main workflow to skip release if identical to the last one --------- Co-authored-by: Michael Hucka <mhucka@google.com>
1 parent 21ee064 commit 4cbe88d

File tree

2 files changed

+109
-35
lines changed

2 files changed

+109
-35
lines changed

.github/workflows/release-main.yml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ jobs:
1717
NAME: dev-release
1818
steps:
1919
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
20+
with:
21+
fetch-depth: 2
2022
- uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
2123
with:
2224
python-version: '3.11'
@@ -34,7 +36,25 @@ jobs:
3436
- name: Build and publish
3537
run: |
3638
CIRQ_PRE_RELEASE_VERSION=$(dev_tools/packaging/generate-dev-version-id.sh)
37-
[[ "$CIRQ_PRE_RELEASE_VERSION" =~ .*dev.* ]] && echo "Deploying dev version '$CIRQ_PRE_RELEASE_VERSION'" || (echo "not dev version"; exit 1)
38-
out_dir="${PWD}/dist"
39-
dev_tools/packaging/produce-package.sh ${out_dir} $CIRQ_PRE_RELEASE_VERSION
39+
if [[ "${CIRQ_PRE_RELEASE_VERSION}" != *.dev* ]]; then
40+
echo "Not a dev version"
41+
exit 1
42+
fi
43+
echo "Building wheels for the dev version '${CIRQ_PRE_RELEASE_VERSION}'"
44+
THIS_DATE_EPOCH=$(git log -1 --pretty="%ct")
45+
out_dir="${HOME}/cirq-dist"
46+
out_dir_last="${HOME}/cirq-dist-last"
47+
dev_tools/packaging/produce-package.sh "${out_dir}" "${CIRQ_PRE_RELEASE_VERSION}"
48+
# disregard errors from building wheels at the previous commit
49+
SOURCE_DATE_EPOCH=${THIS_DATE_EPOCH} dev_tools/packaging/produce-package.sh \
50+
--commit="HEAD~1" "${out_dir_last}" "${CIRQ_PRE_RELEASE_VERSION}" || true
51+
echo "Comparing wheels with the build at previous commit"
52+
if diff -q -r "${out_dir_last}" "${out_dir}"; then
53+
echo "Wheels are identical - skipping the release"
54+
echo "### Skipped identical release" >> ${GITHUB_STEP_SUMMARY}
55+
echo "Cirq wheels for ${CIRQ_PRE_RELEASE_VERSION} (${GITHUB_SHA})" \
56+
"are identical to their build at the previous commit." >> ${GITHUB_STEP_SUMMARY}
57+
exit 0
58+
fi
59+
echo "Deploying dev version '$CIRQ_PRE_RELEASE_VERSION'"
4060
twine upload "${out_dir}/*"

dev_tools/packaging/produce-package.sh

Lines changed: 86 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,62 +14,116 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
################################################################################
18-
# Produces wheels that can be uploaded to the pypi package repository.
19-
#
20-
# First argument must be the output directory. Second argument is an optional
21-
# version specifier. If not set, the version from `_version.py` is used. If set,
22-
# it overwrites `_version.py`.
23-
#
24-
# Usage:
25-
# dev_tools/packaging/produce-package.sh output_dir [version]
26-
################################################################################
17+
set -o errexit
18+
set -o nounset
2719

28-
set -e
2920
trap "{ echo -e '\033[31mFAILED\033[0m'; }" ERR
3021

31-
if [ -z "${1}" ]; then
32-
echo -e "\033[31mNo output directory given.\033[0m"
33-
exit 1
34-
fi
35-
out_dir=$(realpath "${1}")
22+
DOC="\
23+
usage: $0 [options] output_dir [version]
24+
25+
Produces wheels that can be uploaded to the pypi package repository.
26+
27+
First argument must be the output directory. Second argument is an optional
28+
version specifier, which overwrites the version in _version.py files.
29+
If not set, the version from _version.py is used as is.
3630
37-
SPECIFIED_VERSION="${2}"
31+
Options:
32+
33+
-c, --commit=COMMIT create wheels from sources at COMMIT instead of HEAD
34+
-h, --help display this message and exit.
35+
"
36+
37+
out_dir=""
38+
specified_version=""
39+
commitish=HEAD
40+
41+
42+
die() {
43+
( shift; echo -e "\033[31m${*}\033[0m" )
44+
exit "$1"
45+
}
3846

3947
# Helper to isolate dev_tools/modules.py from Python environment variables
4048
my_dev_tools_modules() {
4149
python3 -E dev_tools/modules.py "$@"
4250
}
4351

44-
# Get the working directory to the repo root.
52+
# Process command-line arguments
53+
while (( $# )); do
54+
case "$1" in
55+
-h|--help)
56+
echo "$DOC"
57+
exit 0
58+
;;
59+
-c?*)
60+
commitish="${1#-c}"
61+
;;
62+
--commit=?*)
63+
commitish="${1#*=}"
64+
;;
65+
-c|--commit)
66+
shift
67+
(( $# )) || die 2 "Option '-c,--commit' requires an argument."
68+
commit="$1"
69+
;;
70+
*)
71+
if [[ -z "${out_dir}" ]]; then
72+
out_dir=$(realpath "${1}")
73+
elif [[ -z "${specified_version}" ]]; then
74+
specified_version="${1}"
75+
else
76+
die 2 "Unrecognized argument '$1'."
77+
fi
78+
;;
79+
esac
80+
shift
81+
done
82+
83+
if [[ -z "${out_dir}" ]]; then
84+
die 2 "No output directory given."
85+
fi
86+
87+
# Change to the root of the Cirq git repository
4588
thisdir=$(dirname "${BASH_SOURCE[0]:?}")
4689
repo_dir=$(git -C "${thisdir}" rev-parse --show-toplevel)
4790
cd "${repo_dir}"
4891

49-
# Make a clean copy of HEAD, without files ignored by git (but potentially kept by setup.py).
50-
if [ -n "$(git status --short)" ]; then
92+
# Validate and resolve the commit value
93+
commit=$(git rev-parse --verify --quiet "${commitish}^{commit}") ||
94+
die "$?" "Invalid commit identifier '${commitish}'"
95+
96+
# Make a pristine temporary clone of the Cirq repository
97+
if [[ -n "$(git status --short)" ]]; then
5198
echo -e "\033[31mWARNING: You have uncommitted changes. They won't be included in the package.\033[0m"
5299
fi
100+
53101
tmp_git_dir=$(mktemp -d "/tmp/produce-package-git.XXXXXXXXXXXXXXXX")
54-
trap '{ rm -rf "${tmp_git_dir}"; }' EXIT
55102
echo "Creating pristine repository clone at ${tmp_git_dir}"
56-
git clone --shared --quiet "${repo_dir}" "${tmp_git_dir}"
103+
git clone --shared --quiet --no-checkout "${repo_dir}" "${tmp_git_dir}"
57104
cd "${tmp_git_dir}"
58-
if [ -n "${SPECIFIED_VERSION}" ]; then
59-
CURRENT_VERSION=$(my_dev_tools_modules print_version)
60-
my_dev_tools_modules replace_version --old="${CURRENT_VERSION}" --new="${SPECIFIED_VERSION}"
105+
git checkout --quiet "${commit}"
106+
107+
if [[ -n "${specified_version}" ]]; then
108+
current_version=$(my_dev_tools_modules print_version)
109+
my_dev_tools_modules replace_version --old="${current_version}" --new="${specified_version}"
61110
fi
62111

63112
# Python 3 wheel.
64-
echo "Producing python 3 package files."
113+
echo "Producing Python 3 package files."
65114

66-
CIRQ_MODULES=$(my_dev_tools_modules list --mode folder --include-parent)
67-
date_epoch=$(git log -1 --pretty="%ct")
115+
# Reuse SOURCE_DATE_EPOCH if specified in the caller environment
116+
date_epoch=${SOURCE_DATE_EPOCH:-$(git log -1 --pretty="%ct")}
117+
cirq_modules=$(my_dev_tools_modules list --mode folder --include-parent)
68118

69-
for m in $CIRQ_MODULES; do
70-
echo "creating wheel for ${m}"
71-
SOURCE_DATE_EPOCH="${date_epoch}" \
72-
python3 -m pip wheel --no-deps --wheel-dir="${out_dir}" "./${m}"
119+
for m in ${cirq_modules}; do
120+
echo "creating wheel for ${m}"
121+
SOURCE_DATE_EPOCH="${date_epoch}" \
122+
python3 -m pip wheel --no-deps --wheel-dir="${out_dir}" "./${m}"
73123
done
74124

75125
ls "${out_dir}"
126+
127+
# Final clean up (all is well if we got here)
128+
cd "${repo_dir}"
129+
rm -rf "${tmp_git_dir}"

0 commit comments

Comments
 (0)