Skip to content

REFACTOR: use matrix of deploy destinations instead of hardcoding one or two #74

@elrayle

Description

@elrayle

Description

Current needs require a single deploy for dev and one or two for prod. To get the shared workflow for deploys merged, it was setup with a brute force approach. If more than two deploys are needed, then it would be better to go with a matrix of deploy destinations.

Proposed approach

@jmeridth did an analysis and provided an example of how this can be done.

Using an exclude in the matrix for the conditional deploy: may want an exclude for primary also

  deploy-app-to-azure:
    strategy:
      matrix:
        azure-info:
          - name: primary
            publish-profile: AZURE_WEBAPP_PUBLISH_PROFILE
            azure-webapp-name: ${{ inputs.azure-app-name-postfix }}
          - name: secondary
            publish-profile: AZURE_SECONDARY_WEBAPP_PUBLISH_PROFILE
            azure-webapp-name: ${{ inputs.secondary-azure-app-name-postfix }}
        exclude:
          - name: secondary
            if: ${{ inputs.secondary-azure-app-name-postfix == '' }}
    name: Deploy to ${{ matrix.azure-info.name }} Azure app
    needs: [get-version, build-and-publish-image]
    uses: clearlydefined/operations/.github/workflows/app-deploy-to-azure.yml@elr/reusable-deploy-workflow
    secrets:
      AZURE_WEBAPP_PUBLISH_PROFILE: ${{ secrets[matrix.azure-info.publish-profile] }}
      AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}
      DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
      PRODUCTION_DEPLOYERS: ${{ secrets.PRODUCTION_DEPLOYERS }}
    with:
      deploy-env: ${{ inputs.deploy-env }}
      application-type: ${{ inputs.application-type }}
      azure-webapp-name: ${{ inputs.azure-app-base-name }}${{ matrix.azure-info.azure-webapp-name }}
      application-version: ${{ needs.get-version.outputs.version }}
      image-name-with-tag: ${{ needs.build-and-publish-image.outputs.docker-image-name-with-tag }}

Another way I tried but DID NOT work:

  deploy-app-to-azure:
    strategy:
      matrix:
        azure-info:
          - name: primary
            publish-profile: AZURE_WEBAPP_PUBLISH_PROFILE
            azure-webapp-name: ${{ inputs.azure-app-name-postfix }}
            if: ${{ inputs.azure-app-name-postfix != '' }}
          - name: secondary
            publish-profile: AZURE_SECONDARY_WEBAPP_PUBLISH_PROFILE
            azure-webapp-name: ${{ inputs.secondary-azure-app-name-postfix }}
            if: ${{ inputs.secondary-azure-app-name-postfix != '' }}
    name: Deploy to ${{ matrix.azure-info.name }} Azure app
    if: matrix.azure-info.if
    needs: [get-version, build-and-publish-image]
    uses: clearlydefined/operations/.github/workflows/app-deploy-to-azure.yml@elr/reusable-deploy-workflow
    secrets:
      AZURE_WEBAPP_PUBLISH_PROFILE: ${{ secrets[matrix.azure-info.publish-profile] }}
      AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}
      DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
      PRODUCTION_DEPLOYERS: ${{ secrets.PRODUCTION_DEPLOYERS }}
    with:
      deploy-env: ${{ inputs.deploy-env }}
      application-type: ${{ inputs.application-type }}
      azure-webapp-name: ${{ inputs.azure-app-base-name }}${{ matrix.azure-info.azure-webapp-name }}
      application-version: ${{ needs.get-version.outputs.version }}
      image-name-with-tag: ${{ needs.build-and-publish-image.outputs.docker-image-name-with-tag }}

Something like this to replace the primary and secondary deploys. The only gotcha is that the if line (between name and needs, is not allowed because if conditional is evaluated before the matrix. Matrix can't be used at the job level for ifs. I'd suggest, if you want to go this path, you add the if conditional as an argument to your reusable workflow. Passing it in and then handling the conditional in your workflow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions