Store latest builds in a special release (#8337)
authorGereon Kremer <gkremer@cs.stanford.edu>
Wed, 23 Mar 2022 04:11:04 +0000 (05:11 +0100)
committerGitHub <noreply@github.com>
Wed, 23 Mar 2022 04:11:04 +0000 (04:11 +0000)
This PR refactors our current github ci action that takes care of storing binaries for releases. It now stores binaries both in the current release (if we are in a release build) or attaches them to a special latest tag. For any regular build on the master branch, it moves the latest tag to the current commit, weeds out the current assets attached to that latest release and adds the current binary. This makes sure we always have the equivalent of our current nightly builds in one place.

Co-authored-by: Andres Noetzli <andres.noetzli@gmail.com>
.github/actions/add-to-release/action.yml [deleted file]
.github/actions/store-binary/action.yml [new file with mode: 0644]
.github/workflows/ci.yml

diff --git a/.github/actions/add-to-release/action.yml b/.github/actions/add-to-release/action.yml
deleted file mode 100644 (file)
index 0bad076..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-name: Add binary to release
-description: Add cvc5 binary to the current release
-inputs:
-  binary:
-    description: file name of binary
-  release-name:
-    description: name of binary in release
-  github-token:
-    description: token to upload binary
-runs:
-  using: composite
-  steps:
-    - name: Rename binaries for release
-      shell: bash
-      run: |
-        cp ${{ inputs.binary }} ${{ inputs.release-name }}
-
-    - name: Add binaries to release
-      uses: softprops/action-gh-release@v1
-      with:
-        token: ${{ inputs.github-token }}
-        files: ${{ inputs.release-name }}
diff --git a/.github/actions/store-binary/action.yml b/.github/actions/store-binary/action.yml
new file mode 100644 (file)
index 0000000..bd18093
--- /dev/null
@@ -0,0 +1,99 @@
+name: Store binary
+description: Store cvc5 binary to the latest tag or the current release
+inputs:
+  binary:
+    description: file name of binary
+  binary-name:
+    description: target name of binary
+  github-token:
+    description: token to upload binary
+runs:
+  using: composite
+  steps:
+    - name: Rename binary
+      shell: bash
+      run: |
+        cp ${{ inputs.binary }} ${{ inputs.binary-name }}
+
+    - name: install pyGithub
+      shell: bash
+      run: |
+        python3 -m pip install pyGithub
+
+    - name: store to latest
+      if: github.ref == 'refs/heads/master'
+      shell: 'python3 {0}'
+      env:
+        GITHUB_TOKEN: ${{ inputs.github-token }}
+        BINARY: ${{ inputs.binary-name }}
+      run: |
+        import datetime
+        import os
+        from github import Github
+
+        sha = os.getenv('GITHUB_SHA')
+
+        gh = Github(os.getenv('GITHUB_TOKEN'))
+        repo = gh.get_repo(os.getenv('GITHUB_REPOSITORY'))
+        
+        try:
+          # update "latest" to current commit
+          repo.get_git_ref('tags/latest').edit(sha)
+        except:
+          print('tag `latest` does not exist.')
+          exit
+
+        try:
+          rel = repo.get_release('latest')
+        except:
+          print('New `latest` release')
+          rel = repo.create_git_release('latest', 'latest', 'Latest builds')
+
+        # generate new filename
+        binary = os.getenv('BINARY')
+        name,ext = os.path.splitext(binary)
+        curtime = repo.get_git_commit(sha).committer.date.strftime('%Y-%m-%d')
+        samedayprefix = '{}-{}-'.format(name, curtime)
+        filename = '{}-{}-{}{}'.format(name, curtime, sha[:7], ext)
+
+        # prune old commits
+        assets = list(rel.get_assets())
+        assets.sort(key=lambda x: x.created_at, reverse=True)
+
+        for cnt,asset in enumerate(assets):
+          delete = False
+          if cnt >= 30:
+            delete = True
+          if asset.name.startswith(samedayprefix):
+            delete = True
+          age = datetime.datetime.now() - asset.created_at
+          if age.days > 7:
+            delete = True
+          if delete:
+            asset.delete_asset()
+
+        # upload as asset with proper name
+        rel.upload_asset(binary, name=filename)
+    - name: store to release
+      if: startsWith(github.ref, 'refs/tags/')
+      shell: 'python3 {0}'
+      env:
+        GITHUB_TOKEN: ${{ inputs.github-token }}
+        BINARY: ${{ inputs.binary-name }}
+      run: |
+        import os
+        from github import Github
+
+        refname = os.getenv('GITHUB_REF_NAME')
+        gh = Github(os.getenv('GITHUB_TOKEN'))
+        repo = gh.get_repo(os.getenv('GITHUB_REPOSITORY'))
+        try:
+          rel = repo.get_release(refname)
+        except:
+          print("New release from " + refname)
+          ref = repo.get_git_ref('tags/' + refname)
+          commit = repo.get_git_commit(ref.object.sha)
+          rel = repo.create_git_release(refname, refname, commit.message)
+        rel.upload_asset(os.getenv('BINARY'))
+
index d1376754802aa5cc2d9ad3cd2c438df4de524211..bea9bec6de086c13312e23e473ffeadf873d16f8 100644 (file)
@@ -14,7 +14,7 @@ jobs:
             python-bindings: true
             build-documentation: true
             check-examples: true
-            store-to-release-name: cvc5-Linux
+            binary-name: cvc5-Linux
             exclude_regress: 3-4
             run_regression_args: --tester base --tester model --tester synth --tester abduct --tester dump
 
@@ -25,7 +25,7 @@ jobs:
             strip-bin: strip
             python-bindings: true
             check-examples: true
-            store-to-release-name: cvc5-macOS
+            binary-name: cvc5-macOS
             exclude_regress: 3-4
             run_regression_args: --tester base --tester model --tester synth --tester abduct --tester dump
 
@@ -35,7 +35,7 @@ jobs:
             cache-key: productionwin64
             strip-bin: x86_64-w64-mingw32-strip
             windows-build: true
-            store-to-release-name: cvc5-Win64.exe
+            binary-name: cvc5-Win64.exe
             binary-ext: .exe
 
           - name: ubuntu:production-clang
@@ -126,15 +126,14 @@ jobs:
       with:
         build-dir: ${{ steps.configure-and-build.outputs.shared-build-dir }}
 
-    - name: Add binary to release
-      if: matrix.store-to-release-binary && startsWith(github.ref, 'refs/tags/')
-      uses: ./.github/actions/add-to-release
+    - name: Add binary to latest and release
+      if: matrix.binary-name && (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/'))
+      uses: ./.github/actions/store-binary
       with:
         binary: ${{ steps.configure-and-build.outputs.static-build-dir }}/bin/cvc5${{ matrix.binary-ext }}
-        release-name: ${{ matrix.store-to-release-name }}
+        binary-name: ${{ matrix.binary-name }}
         github-token: ${{ secrets.GITHUB_TOKEN }}
 
-
   update-pr:
     runs-on: ubuntu-latest
     if: github.repository == 'cvc5/cvc5' && github.event_name == 'push'