From 14f2b47add32286c8e1a22319d0ad2ada15c95eb Mon Sep 17 00:00:00 2001
From: Mike Kruskal <62662355+mkruskal-google@users.noreply.github.com>
Date: Mon, 25 Sep 2023 11:46:29 -0700
Subject: [PATCH] Network flake improvements (#29)

* Retry failed docker pulls up to 3 times

* Add a shared checkout action
---
 checkout/action.yml            | 42 ++++++++++++++++++++++++++++++++++
 internal/docker-run/action.yml |  9 ++++++--
 2 files changed, 49 insertions(+), 2 deletions(-)
 create mode 100644 checkout/action.yml

diff --git a/checkout/action.yml b/checkout/action.yml
new file mode 100644
index 0000000..76a4e3b
--- /dev/null
+++ b/checkout/action.yml
@@ -0,0 +1,42 @@
+# Copyright 2023 Google LLC
+#
+# 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.
+
+name: 'Github Checkout'
+description: 'Check out a Github repository'
+inputs:
+  ref:
+    required: true
+    description: "The branch, tag or SHA to checkout"
+  submodules:
+    required: false
+    description: "Whether or not to checkout submodules"
+
+runs:
+  using: 'composite'
+  steps:
+    - name: Checkout pending changes
+      uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
+      with:
+        ref: ${{ inputs.ref }}
+
+    - name: Checkout submodules
+      if: ${{ inputs.submodules }}
+      uses: nick-fields/retry@943e742917ac94714d2f408a0e8320f2d1fcafcd # v2.8.3
+      with:
+        timeout_seconds: 30
+        retry_wait_seconds: 30
+        max_attempts: 5
+        shell: bash
+        command: |
+          git submodule update --init ${{ inputs.submodules == 'recursive' && '--recursive' || '' }}
diff --git a/internal/docker-run/action.yml b/internal/docker-run/action.yml
index c211c4d..fe1c5df 100644
--- a/internal/docker-run/action.yml
+++ b/internal/docker-run/action.yml
@@ -77,9 +77,14 @@ runs:
       run: time docker image load --input ./ci/docker/${{ inputs.image }}.tar
 
     - name: Pull fresh docker image
-      shell: bash
       if: ${{ !inputs.docker-cache }}
-      run: time docker pull -q ${{ inputs.image }}
+      uses: nick-fields/retry@943e742917ac94714d2f408a0e8320f2d1fcafcd # v2.8.3
+      with:
+        timeout_minutes: 5
+        retry_wait_seconds: 30
+        max_attempts: 3
+        shell: bash
+        command: time docker pull -q ${{ inputs.image }}
 
     - name: Forward sccache arguments
       shell: bash