1+ #! /bin/bash
2+
3+ # Copyright 2023 Google Inc.
4+ #
5+ # Licensed under the Apache License, Version 2.0 (the "License");
6+ # you may not use this file except in compliance with the License.
7+ # You may obtain a copy of the License at
8+ #
9+ # http://www.apache.org/licenses/LICENSE-2.0
10+ #
11+ # Unless required by applicable law or agreed to in writing, software
12+ # distributed under the License is distributed on an "AS IS" BASIS,
13+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ # See the License for the specific language governing permissions and
15+ # limitations under the License.
16+
17+
18+ # ##################################### Outputs #####################################
19+
20+ # 1. version: The version of this release including the 'v' prefix (e.g. v1.2.3).
21+ # 2. changelog: Formatted changelog text for this release.
22+
23+ # ###################################################################################
24+
25+ set -e
26+ set -u
27+
28+ function echo_info() {
29+ local MESSAGE=$1
30+ echo " [INFO] ${MESSAGE} "
31+ }
32+
33+ function echo_warn() {
34+ local MESSAGE=$1
35+ echo " [WARN] ${MESSAGE} "
36+ }
37+
38+ function terminate() {
39+ echo " "
40+ echo_warn " --------------------------------------------"
41+ echo_warn " PREFLIGHT FAILED"
42+ echo_warn " --------------------------------------------"
43+ exit 1
44+ }
45+
46+
47+ echo_info " Starting publish preflight check..."
48+ echo_info " Git revision : ${GITHUB_SHA} "
49+ echo_info " Workflow triggered by : ${GITHUB_ACTOR} "
50+ echo_info " GitHub event : ${GITHUB_EVENT_NAME} "
51+
52+
53+ echo_info " "
54+ echo_info " --------------------------------------------"
55+ echo_info " Extracting release version"
56+ echo_info " --------------------------------------------"
57+ echo_info " "
58+
59+ readonly INIT_FILE=" src/firebase_functions/__init__.py"
60+ echo_info " Loading version from: ${INIT_FILE} "
61+
62+ readonly RELEASE_VERSION=` grep " __version__" ${INIT_FILE} | awk ' {print $3}' | tr -d \" ` || true
63+ if [[ -z " ${RELEASE_VERSION} " ]]; then
64+ echo_warn " Failed to extract release version from: ${INIT_FILE} "
65+ terminate
66+ fi
67+
68+ if [[ ! " ${RELEASE_VERSION} " =~ ^[0-9]+\. [0-9]+\. [0-9]+ ([a-zA-Z0-9]+)? $ ]]; then
69+ echo_warn " Malformed release version string: ${RELEASE_VERSION} . Exiting."
70+ terminate
71+ fi
72+
73+ echo_info " Extracted release version: ${RELEASE_VERSION} "
74+ echo " version=${RELEASE_VERSION} " >> " $GITHUB_OUTPUT "
75+
76+
77+ echo_info " "
78+ echo_info " --------------------------------------------"
79+ echo_info " Check release artifacts"
80+ echo_info " --------------------------------------------"
81+ echo_info " "
82+
83+ if [[ ! -d dist ]]; then
84+ echo_warn " dist directory does not exist."
85+ terminate
86+ fi
87+
88+ readonly BIN_DIST=" dist/firebase_functions-${RELEASE_VERSION} -py3-none-any.whl"
89+ if [[ -f " ${BIN_DIST} " ]]; then
90+ echo_info " Found binary distribution (bdist_wheel): ${BIN_DIST} "
91+ else
92+ echo_warn " Binary distribution ${BIN_DIST} not found."
93+ terminate
94+ fi
95+
96+ readonly SRC_DIST=" dist/firebase_functions-${RELEASE_VERSION} .tar.gz"
97+ if [[ -f " ${SRC_DIST} " ]]; then
98+ echo_info " Found source distribution (sdist): ${SRC_DIST} "
99+ else
100+ echo_warn " Source distribution ${SRC_DIST} not found."
101+ terminate
102+ fi
103+
104+ readonly ARTIFACT_COUNT=` ls dist/ | wc -l`
105+ if [[ $ARTIFACT_COUNT -ne 2 ]]; then
106+ echo_warn " Unexpected artifacts in the distribution directory."
107+ ls -l dist
108+ terminate
109+ fi
110+
111+
112+ echo_info " "
113+ echo_info " --------------------------------------------"
114+ echo_info " Checking previous releases"
115+ echo_info " --------------------------------------------"
116+ echo_info " "
117+
118+ readonly PYPI_URL=" https://pypi.org/pypi/firebase-functions/${RELEASE_VERSION} /json"
119+ readonly PYPI_STATUS=` curl -s -o /dev/null -L -w " %{http_code}" ${PYPI_URL} `
120+ if [[ $PYPI_STATUS -eq 404 ]]; then
121+ echo_info " Release version ${RELEASE_VERSION} not found in Pypi."
122+ elif [[ $PYPI_STATUS -eq 200 ]]; then
123+ echo_warn " Release version ${RELEASE_VERSION} already present in Pypi."
124+ terminate
125+ else
126+ echo_warn " Unexpected ${PYPI_STATUS} response from Pypi. Exiting."
127+ terminate
128+ fi
129+
130+
131+ echo_info " "
132+ echo_info " --------------------------------------------"
133+ echo_info " Checking release tag"
134+ echo_info " --------------------------------------------"
135+ echo_info " "
136+
137+ readonly EXISTING_TAG=` git rev-parse -q --verify " refs/tags/v${RELEASE_VERSION} " ` || true
138+ if [[ -n " ${EXISTING_TAG} " ]]; then
139+ echo_warn " Tag v${RELEASE_VERSION} already exists. Exiting."
140+ echo_warn " If the tag was created in a previous unsuccessful attempt, delete it and try again."
141+ echo_warn " $ git tag -d v${RELEASE_VERSION} "
142+ echo_warn " $ git push --delete origin v${RELEASE_VERSION} "
143+
144+ readonly RELEASE_URL=" https://github.com/firebase/firebase-functions-python/releases/tag/v${RELEASE_VERSION} "
145+ echo_warn " Delete any corresponding releases at ${RELEASE_URL} ."
146+ terminate
147+ fi
148+
149+ echo_info " Tag v${RELEASE_VERSION} does not exist."
150+
151+
152+ echo_info " "
153+ echo_info " --------------------------------------------"
154+ echo_info " Generating changelog"
155+ echo_info " --------------------------------------------"
156+ echo_info " "
157+
158+ echo_info " ---< git fetch origin main >---"
159+ git fetch origin main
160+ echo " "
161+
162+ echo_info " Generating changelog from history..."
163+ readonly CURRENT_DIR=$( dirname " $0 " )
164+ readonly CHANGELOG=` ${CURRENT_DIR} /generate_changelog.sh`
165+ echo " $CHANGELOG "
166+
167+ # Parse and preformat the text to handle multi-line output.
168+ https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-of-a-multiline-string
169+ FILTERED_CHANGELOG=` echo " $CHANGELOG " | grep -v " \\ [INFO\\ ]" `
170+ EOF=$( dd if=/dev/urandom bs=15 count=1 status=none | base64)
171+ echo " changelog=<<$EOF " >> " $GITHUB_OUTPUT "
172+ echo $CHANGELOG >> " $GITHUB_OUTPUT "
173+ echo $EOF >> " $GITHUG_OUTPUT "
174+
175+
176+ echo " "
177+ echo_info " --------------------------------------------"
178+ echo_info " PREFLIGHT SUCCESSFUL"
179+ echo_info " --------------------------------------------"
0 commit comments