Skip to content

Commit

Permalink
PI-2628 Only run deploy action for projects that are enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl committed Nov 11, 2024
1 parent 2042868 commit 2fa3f41
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
16 changes: 16 additions & 0 deletions .github/actions/merge-changes/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ outputs:
changes:
description: A JSON array of projects where changes were reported by the build
value: ${{ steps.check-changes.outputs.changes }}
dev:
description: A JSON array of projects where changes should be deployed to dev
value: ${{ steps.deployments.outputs.dev }}
preprod:
description: A JSON array of projects where changes should be deployed to preprod
value: ${{ steps.deployments.outputs.preprod }}
prod:
description: A JSON array of projects where changes should be deployed to prod
value: ${{ steps.deployments.outputs.prod }}

runs:
using: "composite"
Expand All @@ -21,3 +30,10 @@ runs:
id: check-changes
shell: bash
run: echo "changes=$(jq -s '[.[][]]' changes/**/*.json | jq -c '. // []')" | tee -a "$GITHUB_OUTPUT"

- name: Check which services should be deployed
id: deployments
shell: bash
run: ./.github/actions/merge-changes/check-deployments.sh
env:
PROJECTS: ${{ steps.check-changes.outputs.changes }}
21 changes: 21 additions & 0 deletions .github/actions/merge-changes/check-deployments.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

function fromJsonArray() {
jq --raw-output '.[]'
}

function toJsonArray() {
jq --raw-input . | jq --slurp --compact-output .
}

function deploymentEnabled() {
env=$1
while IFS= read -r project; do
file="projects/$project/deploy/values-$env.yml"
if [ -f "$file" ] && ! grep -q 'enabled: false' "$file"; then echo "$project"; fi
done
}

echo "dev=$(echo -n "$PROJECTS" | fromJsonArray | deploymentEnabled dev | toJsonArray)" | tee -a "$GITHUB_OUTPUT"
echo "prepod=$(echo -n "$PROJECTS" | fromJsonArray | deploymentEnabled preprod | toJsonArray)" | tee -a "$GITHUB_OUTPUT"
echo "prod=$(echo -n "$PROJECTS" | fromJsonArray | deploymentEnabled prod | toJsonArray)" | tee -a "$GITHUB_OUTPUT"
22 changes: 16 additions & 6 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
needs: build
outputs:
changes: ${{ steps.merge-changes.outputs.changes }}
dev_deployments: ${{ steps.merge-changes.outputs.dev }}
preprod_deployments: ${{ steps.merge-changes.outputs.preprod }}
prod_deployments: ${{ steps.merge-changes.outputs.prod }}
version: ${{ needs.build.outputs.version }}
steps:
- uses: actions/checkout@v4
Expand All @@ -49,22 +52,22 @@ jobs:
name: Deploy to test
uses: ./.github/workflows/deploy.yml
needs: post-build
if: ${{ needs.post-build.outputs.changes != '[]' }}
if: ${{ needs.post-build.outputs.dev_deployments != '[]' }}
with:
environment: test
version: ${{ needs.post-build.outputs.version }}
projects: ${{ needs.post-build.outputs.changes }}
projects: ${{ needs.post-build.outputs.dev_deployments }}
secrets: inherit

deploy-to-preprod:
name: Deploy to preprod
uses: ./.github/workflows/deploy.yml
needs: post-build
if: ${{ needs.post-build.outputs.changes != '[]'}}
if: ${{ needs.post-build.outputs.preprod_deployments != '[]'}}
with:
environment: preprod
version: ${{ needs.post-build.outputs.version }}
projects: ${{ needs.post-build.outputs.changes }}
projects: ${{ needs.post-build.outputs.preprod_deployments }}
secrets: inherit

end-to-end-tests:
Expand All @@ -78,6 +81,7 @@ jobs:
outputs:
failed-projects: ${{ steps.run.outputs.failed-projects }}
passed-projects: ${{ steps.run.outputs.passed-projects }}
deploy-to-prod: ${{ steps.intersect.outputs.prod }}
report-url: ${{ steps.run.outputs.report-url }}
steps:
- uses: actions/create-github-app-token@v1
Expand All @@ -92,6 +96,12 @@ jobs:
with:
projects: ${{ needs.post-build.outputs.changes }}
token: ${{ steps.app-token.outputs.token }}
- name: Output projects with changes for production where the tests have passed
id: intersect
run: echo "prod=$(jq -crn --argjson changes "$CHANGES" --argjson passed "$PASSED" '$changes - ($changes - $passed)')" | tee -a "$GITHUB_OUTPUT"
env:
CHANGES: ${{ needs.post-build.outputs.prod_deployments }}
PASSED: ${{ steps.run.outputs.passed-projects }}

report-test-failures:
name: Failed testing
Expand All @@ -109,12 +119,12 @@ jobs:
deploy-to-prod:
name: Deploy to production
uses: ./.github/workflows/deploy.yml
if: ${{ (success() || (failure() && needs.end-to-end-tests.outputs.passed-projects != '[]')) && github.ref == 'refs/heads/main' }}
if: ${{ (success() || (failure() && needs.end-to-end-tests.outputs.deploy-to-prod != '[]')) }} # TODO testing from branch: && github.ref == 'refs/heads/main' }}
needs:
- post-build
- end-to-end-tests
with:
environment: prod
version: ${{ needs.post-build.outputs.version }}
projects: ${{ needs.end-to-end-tests.outputs.passed-projects }}
projects: ${{ needs.end-to-end-tests.outputs.deploy-to-prod }}
secrets: inherit

0 comments on commit 2fa3f41

Please sign in to comment.