55 branches : [main]
66
77jobs :
8- read-and-detect- versions :
8+ read-versions :
99 runs-on : ubuntu-latest
1010 outputs :
11- changed_services : ${{ steps.collect-changes .outputs.changed_services }}
11+ matrix : ${{ steps.get-versions .outputs.matrix }}
1212 steps :
1313 - name : Checkout
1414 uses : actions/checkout@v4
15- with :
16- fetch-depth : 0
1715
1816 - name : Read versions from file
1917 id : get-versions
2018 run : |
2119 echo "Reading versions from versions.json"
2220 matrix=$(jq -c 'to_entries | map({service: .key, version: .value})' versions.json)
23- echo $matrix > matrix.json
24- echo "::set-output name=matrix::$(cat matrix.json)"
21+ echo "::set-output name=matrix::$matrix"
22+
23+ - name : Log matrix
24+ id : log-matrix
25+ run : |
26+ echo "Matrix: :${{ steps.get-versions.outputs.matrix }}"
27+
28+ process-versions :
29+ needs : read-versions
30+ runs-on : ubuntu-latest
31+ strategy :
32+ matrix :
33+ service_version : ${{ fromJson(needs.read-versions.outputs.matrix) }}
34+ steps :
35+ - name : Checkout
36+ uses : actions/checkout@v4
37+ with :
38+ ref : " main"
39+ fetch-depth : 0
2540
2641 - name : Get previous commit hash
2742 id : previous_commit
2843 run : |
2944 previous_commit_hash=$(git log --pretty=format:%H -n 2 | tail -n 1)
3045 echo "::set-output name=commit_hash::${previous_commit_hash}"
31-
46+
3247 - name : Get current version
3348 id : current_versions
3449 run : |
35- current_version=$(jq -c --argjson matrix "${{ steps.get-versions.outputs.matrix }}" '.[$matrix[].service]' versions.json)
36- echo $current_version > current_version.json
37- echo "::set-output name=current_version::$(cat current_version.json)"
50+ current_version=$(jq -c --arg service "${{ matrix.service_version.service }}" '.[$service]' versions.json)
51+ echo "::set-output name=current_version::${current_version}"
3852
3953 - name : Get previous version
4054 id : previous_versions
41- run : |
42- previous_version=$(git show ${{ steps.previous_commit.outputs.commit_hash }}:versions.json | jq -c --argjson matrix "${{ steps.get-versions.outputs.matrix }}" '.[$matrix[].service]')
43- echo $previous_version > previous_version.json
44- echo "::set-output name=previous_version::$(cat previous_version.json)"
45-
55+ run : |
56+ previous_version=$(git show ${{ steps.previous_commit.outputs.commit_hash }}:versions.json | jq -c --arg service "${{ matrix.service_version.service }}" '.[$service]')
57+ echo "::set-output name=previous_version::${previous_version}"
58+
4659 - name : Check for version changes
4760 id : version_check
4861 run : |
@@ -53,44 +66,29 @@ jobs:
5366 echo "Previous Version: $previous_version"
5467
5568 if [[ "$current_version" != "$previous_version" ]]; then
56- echo "${{ steps.get-versions.outputs.matrix | fromJson | .[].service }}" >> changed_services.txt
57- fi
58-
59- - name : Collect changes
60- id : collect-changes
61- run : |
62- if [ -f changed_services.txt ]; then
63- changed_services=$(jq -R -s -c 'split("\n")[:-1]' changed_services.txt)
64- echo "::set-output name=changed_services::${changed_services}"
69+ echo "::set-output name=should_deploy::true"
6570 else
66- echo "::set-output name=changed_services::[]"
67- fi
71+ echo "::set-output name=should_deploy::false"
6872
69- - name : Log changed matrix
70- run : |
71- echo "Changed Matrix: ${{ steps.collect-changes.outputs.changed_services }}"
72-
73- process-versions :
74- needs : read-and-detect-versions
75- if : ${{ needs.read-and-detect-versions.outputs.changed_services != '[]' }}
76- runs-on : ubuntu-latest
77- strategy :
78- matrix :
79- service : ${{ fromJson(needs.read-and-detect-versions.outputs.changed_services) }}
80- steps :
81- - name : Checkout
82- uses : actions/checkout@v4
73+ - name : Process changed services
74+ if : ${{ steps.version_check.outputs.should_deploy == 'true' }}
75+ run : echo "Processing ${{ matrix.service_version.service }}"
8376
84- - name : Process each service
77+ - name : Get current version for service
78+ id : get-version
8579 run : |
86- echo "Processing each service in changed matrix"
87- changed_matrix=${{ needs.read-and-detect-versions.outputs.changed_services }}
88- echo "$changed_matrix" | jq -c '.[]' | while read -r service_version; do
89- service=$(echo "$service_version" | jq -r '.service')
90- version=$(echo "$service_version" | jq -r '.version')
91- echo "Processing service: $service with version: $version"
92-
93- # Place your steps here to process each service
94- echo "Triggering workflow for $service with version $version"
95- gh workflow run blank.yml -r soringumeni1/multiple-env-poc -f env="preprod" -f tag_version="$version" -f service="$service"
96- done
80+ current_version=$(jq -c --arg service "${{ matrix.service_version.service }}" '.[$service]' versions.json)
81+ echo "::set-output name=current_version::${current_version}"
82+
83+ - name : Trigger workflow in another repo
84+ if : ${{ steps.version_check.outputs.should_deploy == 'true' }}
85+ uses : benc-uk/workflow-dispatch@v1
86+ with :
87+ workflow : blank.yml
88+ repo : soringumeni1/multiple-env-poc
89+ inputs : |
90+ {
91+ "env": "preprod",
92+ "tag_version": ${{ steps.get-version.outputs.current_version }}
93+ }
94+ token : " ${{ secrets.MY_TOKEN }}"
0 commit comments