From acb2a3a81dbdd4ecae3dff747a503b21a90ba992 Mon Sep 17 00:00:00 2001 From: Daan Persoons <143718186+daanpersoons@users.noreply.github.com> Date: Sat, 23 Nov 2024 01:16:08 +0100 Subject: [PATCH] ENG-1501 - Add support for image variants/monorepos (#9) --- ...workflow-build-and-deploy-digitalocean.yml | 45 +++++++++++++++++-- .../workflow-build-and-deploy-scaleway.yml | 45 +++++++++++++++++-- ...ow-promote-to-environment-digitalocean.yml | 44 ++++++++++++++++-- ...rkflow-promote-to-environment-scaleway.yml | 44 ++++++++++++++++-- .../workflow-build-and-deploy-digitalocean.md | 1 + Docs/workflow-build-and-deploy-scaleway.md | 1 + ...low-promote-to-environment-digitalocean.md | 1 + ...orkflow-promote-to-environment-scaleway.md | 1 + 8 files changed, 170 insertions(+), 12 deletions(-) diff --git a/.github/workflows/workflow-build-and-deploy-digitalocean.yml b/.github/workflows/workflow-build-and-deploy-digitalocean.yml index fe66102..655b61b 100644 --- a/.github/workflows/workflow-build-and-deploy-digitalocean.yml +++ b/.github/workflows/workflow-build-and-deploy-digitalocean.yml @@ -23,6 +23,10 @@ on: description: Image name (fallback to repository name) type: string required: false + image-variants: + description: List of variants to build (folders in monorepo), separated by commas + type: string + required: false secrets: DIGITALOCEAN_API_USER: description: DigitalOcean user email @@ -32,14 +36,48 @@ on: required: true jobs: + generate-matrix: + name: Generate job matrices + runs-on: ubuntu-latest + outputs: + variants: ${{ steps.monorepo.outputs.matrix || steps.normalrepo.outputs.matrix }} + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + sparse-checkout: .github + - name: Monorepo Matrix check + id: monorepo + if: hashFiles('.github/monorepo-rollout-filters.yaml') != '' + uses: wisemen-digital/devops-ga-changed-paths-filter@main + with: + filter-file: .github/monorepo-rollout-filters.yaml + changes-override: ${{ inputs.image-variants }} + - name: Normal Repo Fallback + id: normalrepo + if: hashFiles('.github/monorepo-rollout-filters.yaml') == '' + run: echo 'matrix=[{"environment":"","path":""}]' >> $GITHUB_OUTPUT; + build-and-deploy: - name: Build & Deploy to ${{ inputs.environment }} + name: Build & Deploy ${{ matrix.variant.path }} to ${{ inputs.environment }} + needs: [generate-matrix] + if: ${{ needs.generate-matrix.outputs.variants != '[]' }} + strategy: + matrix: + variant: ${{ fromJson(needs.generate-matrix.outputs.variants) }} runs-on: ubuntu-latest environment: ${{ inputs.environment }} steps: - name: Prepare environment run: | echo "image=`basename ${{ inputs.image || github.repository }}`" >> "$GITHUB_ENV" + if [ -z "${{ matrix.variant.path }}" ]; then + echo "image_suffix=" >> "$GITHUB_ENV" + echo "rollout_labels=${{ vars.K8S_LABELS }}" >> "$GITHUB_ENV" + else + echo "image_suffix=-${{ matrix.variant.path }}" >> "$GITHUB_ENV" + echo "rollout_labels=${{ vars.K8S_LABELS }},variant=${{ matrix.variant.path }}" >> "$GITHUB_ENV" + fi - name: Docker build & push uses: wisemen-digital/devops-ga-docker-build-push@main with: @@ -47,15 +85,16 @@ jobs: container-registry-username: ${{ secrets.DIGITALOCEAN_API_USER }} container-registry-password: ${{ secrets.DIGITALOCEAN_API_TOKEN }} github-access-token: ${{ secrets.GITHUB_TOKEN }} - image: ${{ env.image }} + image: ${{ env.image }}${{ env.image_suffix }} tag: ${{ inputs.environment }} platforms: ${{ vars.CONTAINER_PLATFORMS }} + target: ${{ matrix.variant.path }} - name: Rollout on cluster uses: wisemen-digital/devops-ga-k8s-rollout@main with: vendor: digitalocean cluster-id: ${{ vars.K8S_CLUSTER_ID }} rollout-deployments: ${{ vars.K8S_DEPLOYMENTS }} - rollout-labels: ${{ vars.K8S_LABELS }} + rollout-labels: ${{ env.rollout_labels }} rollout-namespace: ${{ vars.K8S_NAMESPACE }} digitalocean-api-token: ${{ secrets.DIGITALOCEAN_API_TOKEN }} diff --git a/.github/workflows/workflow-build-and-deploy-scaleway.yml b/.github/workflows/workflow-build-and-deploy-scaleway.yml index 3a10970..7e2f930 100644 --- a/.github/workflows/workflow-build-and-deploy-scaleway.yml +++ b/.github/workflows/workflow-build-and-deploy-scaleway.yml @@ -26,6 +26,10 @@ on: description: Image name (fallback to repository name) type: string required: false + image-variants: + description: List of variants to build (folders in monorepo), separated by commas + type: string + required: false # Legacy parameters cluster-deployments: description: UNUSED @@ -60,14 +64,48 @@ on: required: true jobs: + generate-matrix: + name: Generate job matrices + runs-on: ubuntu-latest + outputs: + variants: ${{ steps.monorepo.outputs.matrix || steps.normalrepo.outputs.matrix }} + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + sparse-checkout: .github + - name: Monorepo Matrix check + id: monorepo + if: hashFiles('.github/monorepo-rollout-filters.yaml') != '' + uses: wisemen-digital/devops-ga-changed-paths-filter@main + with: + filter-file: .github/monorepo-rollout-filters.yaml + changes-override: ${{ inputs.image-variants }} + - name: Normal Repo Fallback + id: normalrepo + if: hashFiles('.github/monorepo-rollout-filters.yaml') == '' + run: echo 'matrix=[{"environment":"","path":""}]' >> $GITHUB_OUTPUT; + build-and-deploy: - name: Build & Deploy to ${{ inputs.environment }} + name: Build & Deploy ${{ matrix.variant.path }} to ${{ inputs.environment }} + needs: [generate-matrix] + if: ${{ needs.generate-matrix.outputs.variants != '[]' }} + strategy: + matrix: + variant: ${{ fromJson(needs.generate-matrix.outputs.variants) }} runs-on: ubuntu-latest environment: ${{ inputs.environment }} steps: - name: Prepare environment run: | echo "image=`basename ${{ inputs.image || github.repository }}`" >> "$GITHUB_ENV" + if [ -z "${{ matrix.variant.path }}" ]; then + echo "image_suffix=" >> "$GITHUB_ENV" + echo "rollout_labels=${{ vars.K8S_LABELS }}" >> "$GITHUB_ENV" + else + echo "image_suffix=-${{ matrix.variant.path }}" >> "$GITHUB_ENV" + echo "rollout_labels=${{ vars.K8S_LABELS }},variant=${{ matrix.variant.path }}" >> "$GITHUB_ENV" + fi - name: Docker build & push uses: wisemen-digital/devops-ga-docker-build-push@main with: @@ -75,16 +113,17 @@ jobs: container-registry-username: nologin container-registry-password: ${{ secrets.SCALEWAY_SECRET_KEY }} github-access-token: ${{ secrets.GITHUB_TOKEN }} - image: ${{ env.image }} + image: ${{ env.image }}${{ env.image_suffix }} tag: ${{ inputs.environment }} platforms: ${{ vars.CONTAINER_PLATFORMS }} + target: ${{ matrix.variant.path }} - name: Rollout on cluster uses: wisemen-digital/devops-ga-k8s-rollout@main with: vendor: scaleway cluster-id: ${{ vars.K8S_CLUSTER_ID }} rollout-deployments: ${{ vars.K8S_DEPLOYMENTS }} - rollout-labels: ${{ vars.K8S_LABELS }} + rollout-labels: ${{ env.rollout_labels }} rollout-namespace: ${{ vars.K8S_NAMESPACE }} scaleway-organization-id: ${{ vars.SCALEWAY_ORGANIZATION_ID }} scaleway-project-id: ${{ vars.SCALEWAY_PROJECT_ID }} diff --git a/.github/workflows/workflow-promote-to-environment-digitalocean.yml b/.github/workflows/workflow-promote-to-environment-digitalocean.yml index ab032a3..9f96658 100644 --- a/.github/workflows/workflow-promote-to-environment-digitalocean.yml +++ b/.github/workflows/workflow-promote-to-environment-digitalocean.yml @@ -30,6 +30,10 @@ on: description: Image name (fallback to repository name) type: string required: false + image-variants: + description: List of variants to roll out (folders in monorepo), separated by commas + type: string + required: false secrets: DIGITALOCEAN_API_USER: description: DigitalOcean user email @@ -39,21 +43,55 @@ on: required: true jobs: + generate-matrix: + name: Generate job matrices + runs-on: ubuntu-latest + outputs: + variants: ${{ steps.monorepo.outputs.matrix || steps.normalrepo.outputs.matrix }} + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + sparse-checkout: .github + - name: Monorepo Matrix check + id: monorepo + if: hashFiles('.github/monorepo-rollout-filters.yaml') != '' + uses: wisemen-digital/devops-ga-changed-paths-filter@main + with: + filter-file: .github/monorepo-rollout-filters.yaml + changes-override: ${{ inputs.image-variants }} + - name: Normal Repo Fallback + id: normalrepo + if: hashFiles('.github/monorepo-rollout-filters.yaml') == '' + run: echo 'matrix=[{"environment":"","path":""}]' >> $GITHUB_OUTPUT; + promote-tag: - name: Promote to ${{ inputs.environment-target }} + name: Promote ${{ matrix.variant.path }} to ${{ inputs.environment-target }} + needs: [generate-matrix] + if: ${{ needs.generate-matrix.outputs.variants != '[]' }} + strategy: + matrix: + variant: ${{ fromJson(needs.generate-matrix.outputs.variants) }} runs-on: ubuntu-latest environment: ${{ inputs.environment-target }} steps: - name: Prepare environment run: | echo "image=`basename ${{ inputs.image || github.repository }}`" >> "$GITHUB_ENV" + if [ -z "${{ matrix.variant.path }}" ]; then + echo "image_suffix=" >> "$GITHUB_ENV" + echo "rollout_labels=${{ vars.K8S_LABELS }}" >> "$GITHUB_ENV" + else + echo "image_suffix=-${{ matrix.variant.path }}" >> "$GITHUB_ENV" + echo "rollout_labels=${{ vars.K8S_LABELS }},variant=${{ matrix.variant.path }}" >> "$GITHUB_ENV" + fi - name: Re-tag image uses: wisemen-digital/devops-ga-docker-promote-tag@main with: container-registry: ${{ vars.CONTAINER_REGISTRY_ENDPOINT }} container-registry-username: ${{ secrets.DIGITALOCEAN_API_USER }} container-registry-password: ${{ secrets.DIGITALOCEAN_API_TOKEN }} - image: ${{ env.image }} + image: ${{ env.image }}${{ env.image_suffix }} source-tag: ${{ inputs.environment-source }} target-tag: ${{ inputs.environment-target }} tag-map: ${{ inputs.environment-map }} @@ -63,6 +101,6 @@ jobs: vendor: digitalocean cluster-id: ${{ vars.K8S_CLUSTER_ID }} rollout-deployments: ${{ vars.K8S_DEPLOYMENTS }} - rollout-labels: ${{ vars.K8S_LABELS }} + rollout-labels: ${{ env.rollout_labels }} rollout-namespace: ${{ vars.K8S_NAMESPACE }} digitalocean-api-token: ${{ secrets.DIGITALOCEAN_API_TOKEN }} diff --git a/.github/workflows/workflow-promote-to-environment-scaleway.yml b/.github/workflows/workflow-promote-to-environment-scaleway.yml index 9b41f5c..93ecef3 100644 --- a/.github/workflows/workflow-promote-to-environment-scaleway.yml +++ b/.github/workflows/workflow-promote-to-environment-scaleway.yml @@ -33,6 +33,10 @@ on: description: Image name (fallback to repository name) type: string required: false + image-variants: + description: List of variants to roll out (folders in monorepo), separated by commas + type: string + required: false # Legacy parameters cluster-deployments: description: UNUSED @@ -67,21 +71,55 @@ on: required: true jobs: + generate-matrix: + name: Generate job matrices + runs-on: ubuntu-latest + outputs: + variants: ${{ steps.monorepo.outputs.matrix || steps.normalrepo.outputs.matrix }} + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + sparse-checkout: .github + - name: Monorepo Matrix check + id: monorepo + if: hashFiles('.github/monorepo-rollout-filters.yaml') != '' + uses: wisemen-digital/devops-ga-changed-paths-filter@main + with: + filter-file: .github/monorepo-rollout-filters.yaml + changes-override: ${{ inputs.image-variants }} + - name: Normal Repo Fallback + id: normalrepo + if: hashFiles('.github/monorepo-rollout-filters.yaml') == '' + run: echo 'matrix=[{"environment":"","path":""}]' >> $GITHUB_OUTPUT; + promote-tag: - name: Promote to ${{ inputs.environment-target }} + name: Promote ${{ matrix.variant.path }} to ${{ inputs.environment-target }} + needs: [generate-matrix] + if: ${{ needs.generate-matrix.outputs.variants != '[]' }} + strategy: + matrix: + variant: ${{ fromJson(needs.generate-matrix.outputs.variants) }} runs-on: ubuntu-latest environment: ${{ inputs.environment-target }} steps: - name: Prepare environment run: | echo "image=`basename ${{ inputs.image || github.repository }}`" >> "$GITHUB_ENV" + if [ -z "${{ matrix.variant.path }}" ]; then + echo "image_suffix=" >> "$GITHUB_ENV" + echo "rollout_labels=${{ vars.K8S_LABELS }}" >> "$GITHUB_ENV" + else + echo "image_suffix=-${{ matrix.variant.path }}" >> "$GITHUB_ENV" + echo "rollout_labels=${{ vars.K8S_LABELS }},variant=${{ matrix.variant.path }}" >> "$GITHUB_ENV" + fi - name: Re-tag image uses: wisemen-digital/devops-ga-docker-promote-tag@main with: container-registry: ${{ vars.CONTAINER_REGISTRY_ENDPOINT }} container-registry-username: nologin container-registry-password: ${{ secrets.SCALEWAY_SECRET_KEY }} - image: ${{ env.image }} + image: ${{ env.image }}${{ env.image_suffix }} source-tag: ${{ inputs.environment-source }} target-tag: ${{ inputs.environment-target }} tag-map: ${{ inputs.environment-map }} @@ -91,7 +129,7 @@ jobs: vendor: scaleway cluster-id: ${{ vars.K8S_CLUSTER_ID }} rollout-deployments: ${{ vars.K8S_DEPLOYMENTS }} - rollout-labels: ${{ vars.K8S_LABELS }} + rollout-labels: ${{ env.rollout_labels }} rollout-namespace: ${{ vars.K8S_NAMESPACE }} scaleway-organization-id: ${{ vars.SCALEWAY_ORGANIZATION_ID }} scaleway-project-id: ${{ vars.SCALEWAY_PROJECT_ID }} diff --git a/Docs/workflow-build-and-deploy-digitalocean.md b/Docs/workflow-build-and-deploy-digitalocean.md index 47362b3..288ba63 100644 --- a/Docs/workflow-build-and-deploy-digitalocean.md +++ b/Docs/workflow-build-and-deploy-digitalocean.md @@ -12,6 +12,7 @@ Note: this workflow is meant for deployments to **DigitalOcean**. | ----- | ----------- | | `environment` | Target environment to deploy to, usually `development` | | `image` | Image name (fallback to repository name) | +| `image-variants` | List of variants to build (folders in monorepo), separated by commas | | Secret | Description | | ------ | ----------- | diff --git a/Docs/workflow-build-and-deploy-scaleway.md b/Docs/workflow-build-and-deploy-scaleway.md index 4208fd3..be5f709 100644 --- a/Docs/workflow-build-and-deploy-scaleway.md +++ b/Docs/workflow-build-and-deploy-scaleway.md @@ -12,6 +12,7 @@ Note: this workflow is meant for deployments to **Scaleway**. | ----- | ----------- | | `environment` | Target environment to deploy to, usually `development` | | `image` | Image name (fallback to repository name) | +| `image-variants` | List of variants to build (folders in monorepo), separated by commas | | Secret | Description | | ------ | ----------- | diff --git a/Docs/workflow-promote-to-environment-digitalocean.md b/Docs/workflow-promote-to-environment-digitalocean.md index f3471c6..a23faf8 100644 --- a/Docs/workflow-promote-to-environment-digitalocean.md +++ b/Docs/workflow-promote-to-environment-digitalocean.md @@ -16,6 +16,7 @@ Note: this workflow is meant for deployments to **DigitalOcean**. | `environment-target` | Target environment to deploy to | | `environment-map` | Custom environment mapping (JSON object) | | `image` | Image name (fallback to repository name) | +| `image-variants` | List of variants to roll out (folders in monorepo), separated by commas | | Secret | Description | | ------ | ----------- | diff --git a/Docs/workflow-promote-to-environment-scaleway.md b/Docs/workflow-promote-to-environment-scaleway.md index a163f78..2dfbe8d 100644 --- a/Docs/workflow-promote-to-environment-scaleway.md +++ b/Docs/workflow-promote-to-environment-scaleway.md @@ -16,6 +16,7 @@ Note: this workflow is meant for deployments to **Scaleway**. | `environment-target` | Target environment to deploy to | | `environment-map` | Custom environment mapping (JSON object) | | `image` | Image name (fallback to repository name) | +| `image-variants` | List of variants to roll out (folders in monorepo), separated by commas | | Secret | Description | | ------ | ----------- |