Introducing env_mapping #19
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: Update Env Template | |
on: | |
pull_request: | |
types: [opened, edited, synchronize] | |
jobs: | |
update-env-template: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Parse PR description | |
id: parse_pr | |
run: | | |
# Extract the value of "Environment Variables Changes" (yes/no) | |
ENV_CHANGES=$(echo "${{ github.event.pull_request.body }}" | grep -A 1 "Environment Variables Changes" | tail -n 1 | grep -o "yes\|no" || echo "no") | |
# Check if the user specified any added or removed env variables | |
ADDED_ENVS=$(echo "${{ github.event.pull_request.body }}" | sed -n '/Added Environment Variables/,/Removed Environment Variables/p' | grep '^+' | sed 's/+ //g') | |
REMOVED_ENVS=$(echo "${{ github.event.pull_request.body }}" | sed -n '/Removed Environment Variables/,/Other Notes/p' | grep '^-' | sed 's/- //g') | |
# Save the results for later steps | |
echo "::set-output name=env_changes::$ENV_CHANGES" | |
echo "::set-output name=added_envs::$ADDED_ENVS" | |
echo "::set-output name=removed_envs::$REMOVED_ENVS" | |
- name: Check if env changes are required | |
if: steps.parse_pr.outputs.env_changes == 'yes' | |
run: echo "Proceeding with environment variable changes..." | |
- name: Update env.template with added variables | |
if: steps.parse_pr.outputs.env_changes == 'yes' && steps.parse_pr.outputs.added_envs != '' | |
run: | | |
# For each added environment variable, check if it exists in env.template and append if not present | |
echo "${{ steps.parse_pr.outputs.added_envs }}" | while IFS= read -r env; do | |
if ! grep -q "^$env=" env.template; then | |
echo "$env=http://mock-$env-url" >> env.template | |
else | |
echo "$env already exists in env.template, skipping..." | |
fi | |
done | |
- name: Remove variables from env.template | |
if: steps.parse_pr.outputs.env_changes == 'yes' && steps.parse_pr.outputs.removed_envs != '' | |
run: | | |
# For each removed environment variable, remove it from the env.template file | |
echo "${{ steps.parse_pr.outputs.removed_envs }}" | while IFS= read -r env; do | |
sed -i "/^$env=/d" env.template | |
done | |
- name: Commit changes | |
if: steps.parse_pr.outputs.env_changes == 'yes' | |
run: | | |
git config --local user.email "ragunanthan.j@ib-systems.org" | |
git config --local user.name "jragunanthan" | |
# Check if there are any changes to commit | |
if git diff --cached --quiet; then | |
echo "No changes to commit" | |
else | |
git add env.template | |
git commit -m "Update env.template based on PR changes" | |
# Pull the latest changes from the remote branch to avoid conflicts | |
git pull origin ${{ github.head_ref }} --rebase | |
# Push the changes to the PR branch | |
git push origin HEAD:${{ github.head_ref }} | |
fi |