[infra] Deflake Go / generate (pull_request) GitHub action.

As you can see https://github.com/google/gvisor/commits/master, there are a lot
of red commits. This is because the Go / generate GitHub action flakes.

On merge, two variants of this workflow run:
- one triggered by the pull request (copybara force pushes to the PR right
  before merge)
- one triggered by the push (merge)

If the push action ends up finishing before the pull request action can run
go_branch.sh, then the changes that go_branch.sh makes is already pushed to
the remote go branch. Consequently, the pull request action ends up having
nothing to commit causing this action to fail.

This change also fixes lint warnings.

Now we skip running the go_branch.sh if we find that our current working commit
has already been committed to remote.

PiperOrigin-RevId: 339586760
This commit is contained in:
Ayush Ranjan 2020-10-28 19:52:32 -07:00 committed by gVisor bot
parent 265f1eb2c7
commit 37b57ecd04
1 changed files with 33 additions and 12 deletions

View File

@ -14,23 +14,30 @@
# See the License for the specific language governing permissions and
# limitations under the License.
set -xeo pipefail
set -xeou pipefail
# Discovery the package name from the go.mod file.
declare -r module=$(cat go.mod | grep -E "^module" | cut -d' ' -f2)
declare -r origpwd=$(pwd)
declare -r othersrc=("go.mod" "go.sum" "AUTHORS" "LICENSE")
declare module origpwd othersrc
module=$(cat go.mod | grep -E "^module" | cut -d' ' -f2)
origpwd=$(pwd)
othersrc=("go.mod" "go.sum" "AUTHORS" "LICENSE")
readonly module origpwd othersrc
# Check that gopath has been built.
declare -r gopath_dir="$(pwd)/bazel-bin/gopath/src/${module}"
if ! [ -d "${gopath_dir}" ]; then
declare gopath_dir
gopath_dir="$(pwd)/bazel-bin/gopath/src/${module}"
readonly gopath_dir
if ! [[ -d "${gopath_dir}" ]]; then
echo "No gopath directory found; build the :gopath target." >&2
exit 1
fi
# Create a temporary working directory, and ensure that this directory and all
# subdirectories are cleaned up upon exit.
declare -r tmp_dir=$(mktemp -d)
declare tmp_dir
tmp_dir=$(mktemp -d)
readonly tmp_dir
finish() {
cd # Leave tmp_dir.
rm -rf "${tmp_dir}"
@ -38,21 +45,35 @@ finish() {
trap finish EXIT
# Record the current working commit.
declare -r head=$(git describe --always)
declare head
head=$(git describe --always)
readonly head
# We expect to have an existing go branch that we will use as the basis for this
# commit. That branch may be empty, but it must exist. We search for this branch
# using the local branch, the "origin" branch, and other remotes, in order.
git fetch --all
declare -r go_branch=$( \
declare go_branch
go_branch=$( \
git show-ref --hash refs/heads/go || \
git show-ref --hash refs/remotes/origin/go || \
git show-ref --hash go | head -n 1 \
)
readonly go_branch
declare commit
commit=$(git rev-parse HEAD)
readonly commit
if [[ -n "$(git branch --contains="${commit}" go)" ]]; then
# The go branch already has the commit.
exit 0
fi
# Clone the current repository to the temporary directory, and check out the
# current go_branch directory. We move to the new repository for convenience.
declare -r repo_orig="$(pwd)"
declare repo_orig
repo_orig="$(pwd)"
readonly repo_orig
declare -r repo_new="${tmp_dir}/repository"
git clone . "${repo_new}"
cd "${repo_new}"
@ -68,8 +89,8 @@ git checkout -b go "${go_branch}"
#
# N.B. The git behavior changed at some point and the relevant flag was added
# to allow for override, so try the only behavior first then pass the flag.
git merge --no-commit --strategy ours ${head} || \
git merge --allow-unrelated-histories --no-commit --strategy ours ${head}
git merge --no-commit --strategy ours "${head}" || \
git merge --allow-unrelated-histories --no-commit --strategy ours "${head}"
# Normalize the permissions on the old branch. Note that they should be
# normalized if constructed by this tool, but we do so before the rsync.