Update complete_flow.yml #21
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy and Trigger on Version Change | |
on: | |
push: | |
branches: [main] | |
jobs: | |
read-and-detect-versions: | |
runs-on: ubuntu-latest | |
outputs: | |
changed_matrix: ${{ steps.collect-changes.outputs.changed_matrix }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Read versions from file | |
id: get-versions | |
run: | | |
echo "Reading versions from versions.json" | |
matrix=$(jq -c 'to_entries | map({service: .key, version: .value})' versions.json) | |
echo "::set-output name=matrix::$matrix" | |
- name: Get previous commit hash | |
id: previous_commit | |
run: | | |
previous_commit_hash=$(git log --pretty=format:%H -n 2 | tail -n 1) | |
echo "::set-output name=commit_hash::${previous_commit_hash}" | |
- name: Check for version changes | |
id: check-versions | |
run: | | |
matrix=${{ steps.get-versions.outputs.matrix }} | |
changed_matrix="[]" | |
echo $matrix | jq -c '.[]' | while read -r service_version; do | |
service=$(echo "$service_version" | jq -r '.service') | |
current_version=$(echo "$service_version" | jq -r '.version') | |
previous_version=$(git show ${{ steps.previous_commit.outputs.commit_hash }}:versions.json | jq -r --arg service "$service" '.[$service]') | |
if [[ "$current_version" != "$previous_version" ]]; then | |
changed_matrix=$(echo "$changed_matrix" | jq --arg service "$service" --arg version "$current_version" '. + [{"service": $service, "version": $version}]') | |
fi | |
done | |
echo "::set-output name=changed_matrix::$changed_matrix" | |
- name: Log changed matrix | |
run: | | |
echo "Changed Matrix: ${{ steps.check-versions.outputs.changed_matrix }}" | |
process-versions: | |
needs: read-and-detect-versions | |
if: ${{ needs.read-and-detect-versions.outputs.changed_matrix != '[]' }} | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
service_version: ${{ fromJson(needs.read-and-detect-versions.outputs.changed_matrix) }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Echo Version | |
run: echo "Processing ${{ matrix.service_version.service }}" | |
- name: Get current version for service | |
id: get-version | |
run: | | |
current_version=$(jq -c --arg service "${{ matrix.service_version.service }}" '.[$service]' versions.json) | |
echo "::set-output name=current_version::${current_version}" | |
- name: Trigger workflow in another repo | |
uses: benc-uk/workflow-dispatch@v1 | |
with: | |
workflow: blank.yml | |
repo: soringumeni1/multiple-env-poc | |
inputs: | | |
{ | |
"env": "preprod", | |
"tag_version": ${{ steps.get-version.outputs.current_version }} | |
} | |
token: "${{ secrets.MY_TOKEN }}" |