From 87ab4a70850229ed90bc4fd9060a02af6caa5e91 Mon Sep 17 00:00:00 2001 From: Stanislaw Malinowski Date: Fri, 11 Oct 2024 10:39:09 +0000 Subject: [PATCH 1/8] try out by adding the needs field --- .github/workflows/_container.yml | 39 ++++++++++++++++++++++++++++- .github/workflows/helm.yml | 42 -------------------------------- 2 files changed, 38 insertions(+), 43 deletions(-) delete mode 100644 .github/workflows/helm.yml diff --git a/.github/workflows/_container.yml b/.github/workflows/_container.yml index 4857ee9e6..684cbcf0a 100644 --- a/.github/workflows/_container.yml +++ b/.github/workflows/_container.yml @@ -1,8 +1,13 @@ +name: Build and publish container on: workflow_call: +env: + GCR_IMAGE: ghcr.io/diamondlightsource/blueapi + HELM_VERSION: 3.10.3 + jobs: - build: + build_container: runs-on: ubuntu-latest steps: @@ -54,3 +59,35 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + + helm_publish: + name: publish gcr + runs-on: ubuntu-latest + environment: prod + needs: build_container + steps: + - name: checkout repo + uses: actions/checkout@v3 + + - name: install helm + uses: Azure/setup-helm@v3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + id: install + + - name: login to acr using helm + run: | + echo ${{ secrets.GITHUB_TOKEN }} | helm registry login ${{ env.GCR_IMAGE }} --username ${{ github.repository_owner }} --password-stdin + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@57396166ad8aefe6098280995947635806a0e6ea + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=tag + - name: package chart and push it + run: | + sed -i "$ a appVersion: ${GITHUB_REF##*/}" helm/blueapi/Chart.yaml + helm dependencies update helm/blueapi + helm package helm/blueapi --version ${GITHUB_REF##*/} -d /tmp/ + helm push /tmp/blueapi-${GITHUB_REF##*/}.tgz oci://ghcr.io/diamondlightsource/charts diff --git a/.github/workflows/helm.yml b/.github/workflows/helm.yml deleted file mode 100644 index 8c23d1550..000000000 --- a/.github/workflows/helm.yml +++ /dev/null @@ -1,42 +0,0 @@ -name: Helm CI - -on: - push: - tags: - - "*" - -env: - GCR_IMAGE: ghcr.io/diamondlightsource/blueapi - HELM_VERSION: 3.10.3 - -jobs: - build: - name: publish gcr - runs-on: ubuntu-latest - environment: prod - steps: - - name: checkout repo - uses: actions/checkout@v3 - - - name: install helm - uses: Azure/setup-helm@v3 - with: - token: ${{ secrets.GITHUB_TOKEN }} - id: install - - - name: login to acr using helm - run: | - echo ${{ secrets.GITHUB_TOKEN }} | helm registry login ${{ env.GCR_IMAGE }} --username ${{ github.repository_owner }} --password-stdin - - name: Extract metadata (tags, labels) for Docker - id: meta - uses: docker/metadata-action@57396166ad8aefe6098280995947635806a0e6ea - with: - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} - tags: | - type=ref,event=tag - - name: package chart and push it - run: | - sed -i "$ a appVersion: ${GITHUB_REF##*/}" helm/blueapi/Chart.yaml - helm dependencies update helm/blueapi - helm package helm/blueapi --version ${GITHUB_REF##*/} -d /tmp/ - helm push /tmp/blueapi-${GITHUB_REF##*/}.tgz oci://ghcr.io/diamondlightsource/charts From 779b475b24bda7cf3a2c5ed72caf0ffe7e1ed3f9 Mon Sep 17 00:00:00 2001 From: Stanislaw Malinowski Date: Wed, 16 Oct 2024 15:36:22 +0100 Subject: [PATCH 2/8] sed magic --- .github/workflows/_container.yml | 33 +++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/.github/workflows/_container.yml b/.github/workflows/_container.yml index 684cbcf0a..741845317 100644 --- a/.github/workflows/_container.yml +++ b/.github/workflows/_container.yml @@ -16,6 +16,22 @@ jobs: with: # Need this to get version number from last tag fetch-depth: 0 + - name: Validate PEP 440 version compliance + run: | + python -m pip install packaging + python -c " + import re + from packaging.version import Version, InvalidVersion + + ref = '${GITHUB_REF##*/}' + try: + version = Version(ref) + print(f'PEP 440 compliant version: {version}') + except InvalidVersion: + print(f'Invalid PEP 440 version: {ref}') + exit(1) + " + - name: Set up Docker Buildx id: buildx @@ -85,9 +101,20 @@ jobs: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=ref,event=tag + + - name: Convert PEP 440 version to SemVer + run: | + ref="${GITHUB_REF##*/}" + # Convert alpha/beta/rc versions from PEP 440 to SemVer + if [[ "$ref" =~ ([0-9]+\.[0-9]+\.[0-9]+)(a|b|rc)([0-9]+) ]]; then + ref="${BASH_REMATCH[1]}-${BASH_REMATCH[2]}${BASH_REMATCH[3]}" + fi + echo "Converted version: $ref" + echo "VERSION=$ref" >> $GITHUB_ENV + - name: package chart and push it run: | - sed -i "$ a appVersion: ${GITHUB_REF##*/}" helm/blueapi/Chart.yaml + sed -i "$ a appVersion: ${VERSION}" helm/blueapi/Chart.yaml helm dependencies update helm/blueapi - helm package helm/blueapi --version ${GITHUB_REF##*/} -d /tmp/ - helm push /tmp/blueapi-${GITHUB_REF##*/}.tgz oci://ghcr.io/diamondlightsource/charts + helm package helm/blueapi --version ${VERSION} -d /tmp/ + helm push /tmp/blueapi-${VERSION}.tgz oci://ghcr.io/diamondlightsource/charts From cb8a05b663141c6be429d360e421648dc0538704 Mon Sep 17 00:00:00 2001 From: Stanislaw Malinowski Date: Thu, 24 Oct 2024 11:22:48 +0000 Subject: [PATCH 3/8] change pep440 call from python to regex --- .github/workflows/_container.yml | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/.github/workflows/_container.yml b/.github/workflows/_container.yml index 741845317..997ca7383 100644 --- a/.github/workflows/_container.yml +++ b/.github/workflows/_container.yml @@ -5,6 +5,8 @@ on: env: GCR_IMAGE: ghcr.io/diamondlightsource/blueapi HELM_VERSION: 3.10.3 + PEP_440_REGEX: v?(?:(?:(?[0-9]+)!)?(?[0-9]+(?:\.[0-9]+)*)(?
[-_\.]?(?(a|b|c|rc|alpha|beta|pre|preview))[-_\.]?(?[0-9]+)?)?(?(?:-(?[0-9]+))|(?:[-_\.]?(?post|rev|r)[-_\.]?(?[0-9]+)?))?(?[-_\.]?(?dev)[-_\.]?(?[0-9]+)?)?)(?:\+(?[a-z0-9]+(?:[-_\.][a-z0-9]+)*))?
+  # https://regex101.com/library/M7QMAp
 
 jobs:
   build_container:
@@ -18,21 +20,14 @@ jobs:
           fetch-depth: 0
       - name: Validate PEP 440 version compliance
         run: |
-          python -m pip install packaging
-          python -c "
-      import re
-      from packaging.version import Version, InvalidVersion
-
-      ref = '${GITHUB_REF##*/}'
-      try:
-          version = Version(ref)
-          print(f'PEP 440 compliant version: {version}')
-      except InvalidVersion:
-          print(f'Invalid PEP 440 version: {ref}')
-          exit(1)
-        "
-
-
+          ref="${{ github.ref_name }}"
+          my_regex=${{env.PEP_440_REGEX}}
+          if [[ "$ref" =~ $my_regex ]]; then
+            echo "PEP 440 compliant version: $ref"
+          else
+            echo "Invalid PEP 440 version: $ref"
+            exit 1
+          fi
       - name: Set up Docker Buildx
         id: buildx
         uses: docker/setup-buildx-action@v3

From c61b3310882433cd429d9d970c6a1498f7d72f55 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stanis=C5=82aw=20Malinowski?=
 <56644812+stan-dot@users.noreply.github.com>
Date: Fri, 1 Nov 2024 13:55:48 +0000
Subject: [PATCH 4/8] Replace PEP check with a SemVer check

---
 .github/workflows/_container.yml | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)

diff --git a/.github/workflows/_container.yml b/.github/workflows/_container.yml
index 997ca7383..003a0d4fc 100644
--- a/.github/workflows/_container.yml
+++ b/.github/workflows/_container.yml
@@ -5,8 +5,8 @@ on:
 env:
   GCR_IMAGE: ghcr.io/diamondlightsource/blueapi
   HELM_VERSION: 3.10.3
-  PEP_440_REGEX: v?(?:(?:(?[0-9]+)!)?(?[0-9]+(?:\.[0-9]+)*)(?
[-_\.]?(?(a|b|c|rc|alpha|beta|pre|preview))[-_\.]?(?[0-9]+)?)?(?(?:-(?[0-9]+))|(?:[-_\.]?(?post|rev|r)[-_\.]?(?[0-9]+)?))?(?[-_\.]?(?dev)[-_\.]?(?[0-9]+)?)?)(?:\+(?[a-z0-9]+(?:[-_\.][a-z0-9]+)*))?
-  # https://regex101.com/library/M7QMAp
+  SEMVER_REGEX: ^(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)(?:-(?P(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$
+  # https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
 
 jobs:
   build_container:
@@ -18,14 +18,14 @@ jobs:
         with:
           # Need this to get version number from last tag
           fetch-depth: 0
-      - name: Validate PEP 440 version compliance
+      - name: Validate SemVer2 version compliance
         run: |
           ref="${{ github.ref_name }}"
-          my_regex=${{env.PEP_440_REGEX}}
+          my_regex=${{env.SEMVER_REGEX}}
           if [[ "$ref" =~ $my_regex ]]; then
-            echo "PEP 440 compliant version: $ref"
+            echo "SemVer compliant version: $ref"
           else
-            echo "Invalid PEP 440 version: $ref"
+            echo "Invalid SemVer version: $ref"
             exit 1
           fi
       - name: Set up Docker Buildx
@@ -97,16 +97,6 @@ jobs:
           tags: |
             type=ref,event=tag
 
-      - name: Convert PEP 440 version to SemVer
-        run: |
-          ref="${GITHUB_REF##*/}"
-          # Convert alpha/beta/rc versions from PEP 440 to SemVer
-          if [[ "$ref" =~ ([0-9]+\.[0-9]+\.[0-9]+)(a|b|rc)([0-9]+) ]]; then
-            ref="${BASH_REMATCH[1]}-${BASH_REMATCH[2]}${BASH_REMATCH[3]}"
-          fi
-          echo "Converted version: $ref"
-          echo "VERSION=$ref" >> $GITHUB_ENV
-
       - name: package chart and push it
         run: |
           sed -i "$ a appVersion: ${VERSION}" helm/blueapi/Chart.yaml

From 791bf0354f9da22eb0426d7285ed12ecd33c5dc2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stanis=C5=82aw=20Malinowski?=
 <56644812+stan-dot@users.noreply.github.com>
Date: Fri, 1 Nov 2024 14:17:58 +0000
Subject: [PATCH 5/8] Add quotations right

---
 .github/workflows/_container.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/_container.yml b/.github/workflows/_container.yml
index 003a0d4fc..0c136f02e 100644
--- a/.github/workflows/_container.yml
+++ b/.github/workflows/_container.yml
@@ -21,7 +21,7 @@ jobs:
       - name: Validate SemVer2 version compliance
         run: |
           ref="${{ github.ref_name }}"
-          my_regex=${{env.SEMVER_REGEX}}
+          my_regex="${{env.SEMVER_REGEX}}"
           if [[ "$ref" =~ $my_regex ]]; then
             echo "SemVer compliant version: $ref"
           else

From 4bbce3d523e22af0b9a6c82d0b53d08ce87cf523 Mon Sep 17 00:00:00 2001
From: Stanislaw Malinowski 
Date: Mon, 4 Nov 2024 16:11:14 +0000
Subject: [PATCH 6/8] yml fix

---
 .github/workflows/_container.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/_container.yml b/.github/workflows/_container.yml
index 0c136f02e..6b7f2e4a7 100644
--- a/.github/workflows/_container.yml
+++ b/.github/workflows/_container.yml
@@ -86,7 +86,7 @@ jobs:
           token: ${{ secrets.GITHUB_TOKEN }}
         id: install
 
-      - name: login to acr using helm
+      - name: login to gcr using helm
         run: |
           echo ${{ secrets.GITHUB_TOKEN }} | helm registry login ${{ env.GCR_IMAGE }} --username ${{ github.repository_owner }} --password-stdin
       - name: Extract metadata (tags, labels) for Docker

From 58a32bdc34acb4f9cf5fd9c44deed9d98baa7a47 Mon Sep 17 00:00:00 2001
From: Stanislaw Malinowski 
Date: Mon, 4 Nov 2024 16:31:18 +0000
Subject: [PATCH 7/8] fix regex

---
 .github/workflows/_container.yml | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/_container.yml b/.github/workflows/_container.yml
index 6b7f2e4a7..4d38e154d 100644
--- a/.github/workflows/_container.yml
+++ b/.github/workflows/_container.yml
@@ -5,8 +5,6 @@ on:
 env:
   GCR_IMAGE: ghcr.io/diamondlightsource/blueapi
   HELM_VERSION: 3.10.3
-  SEMVER_REGEX: ^(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)(?:-(?P(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$
-  # https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
 
 jobs:
   build_container:
@@ -19,6 +17,9 @@ jobs:
           # Need this to get version number from last tag
           fetch-depth: 0
       - name: Validate SemVer2 version compliance
+        if: startsWith(github.ref, 'refs/tags/')
+        env:
+          SEMVER_REGEX: ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$
         run: |
           ref="${{ github.ref_name }}"
           my_regex="${{env.SEMVER_REGEX}}"

From 05940c0f67c22d6b83c1d3238e0c30d673c6b183 Mon Sep 17 00:00:00 2001
From: Stanislaw Malinowski 
Date: Mon, 4 Nov 2024 16:37:59 +0000
Subject: [PATCH 8/8] add an if branch to the helm publish line

---
 .github/workflows/_container.yml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.github/workflows/_container.yml b/.github/workflows/_container.yml
index 4d38e154d..70ce21742 100644
--- a/.github/workflows/_container.yml
+++ b/.github/workflows/_container.yml
@@ -75,6 +75,7 @@ jobs:
   helm_publish:
     name: publish gcr
     runs-on: ubuntu-latest
+    if: startsWith(github.ref, 'refs/tags/')
     environment: prod
     needs: build_container
     steps: