Skip to content

Instantly share code, notes, and snippets.

@ivansnag
Created July 16, 2024 17:27
Show Gist options
  • Save ivansnag/58705ebe7c9c6155cfc3238c07b51954 to your computer and use it in GitHub Desktop.
Save ivansnag/58705ebe7c9c6155cfc3238c07b51954 to your computer and use it in GitHub Desktop.
Example of a Github Actions workflow that gets the name of the services from the folder structure and file changes in the commit when a PR is merged.
name: Scan Changed Files in PR
on:
pull_request:
types:
- closed
branches:
- 'main'
jobs:
scan:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 50 # pick an appropriate number
- name: Get list of changed files
id: getfile
run: |
files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} --)
echo "Changed files: $files"
echo "Changed files: $files" >> changed_files.txt
- name: Identify affected services
id: services
run: |
services=""
while read -r file; do
service=$(echo $file | awk -F'/' '{print $1}')
services="$services $service"
done < changed_files.txt
unique_services=$(echo $services | xargs -n1 | sort -u | xargs)
echo "Affected services: $unique_services"
echo "::set-output name=affected_services::$unique_services"
- name: Post affected services
run: |
json_payload=$(jq -n \
--arg github_pull_id ${{ github.event.pull_request.id }} \
--arg services '[${{ steps.services.outputs.affected_services }}]' \
'{
github_pull_id: $github_pull_id,
services: $services
}')
curl -X POST \
http://yourinstance.getdx.net/api/deployments.setPullServices \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_BEARER_TOKEN_HERE" \
-d "$json_payload"
if [ "$RESPONSE_CODE" -ne 200 ]; then
ERROR_MESSAGE=$(jq -r '.message' response.txt)
ERROR_TYPE=$(jq -r '.error' response.txt)
echo "Error response from API: HTTP status code $RESPONSE"
echo "Error type: $ERROR_TYPE"
echo "Error message: $ERROR_MESSAGE"
exit 1 # Fail the step explicitly if an error is detected
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment