Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify CI script to check diffs #276

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions .github/workflows/pr_check_copies.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#!/bin/bash

# Script to check if the changed files to promote are a direct copy of earlier environment

# Function to compare the files, file1 is our original file passed to the script followed by two possible further files
compare_files() {
file1=$1
file2=$2

# Check the number of arguments passed to the function for comparison
# We then check whether the files exist in the previous environment, if not the PR needs review
if [ -f $file2 ]; then

# Check if the second file exists
if [ -f "$file2" ]; then
if cmp -s "$file1" "$file2"; then
echo $((0))
return 0 # Files are identical
else
echo $((1))
echo "Difference found between $file1 and $file2"
return 1 # Files are different
fi
else
echo $((1))
echo "File $file2 does not exist"
return 1 # Second file doesn't exist
fi
}

# Check the file path to determine if it's from staging or prod folder
# If it's staging we pass it along with a url to the file in dev folder
# If it's from prod we pass both the staging and dev file also for comparison
path=$1
curr_env=$2
comp_env=$3

compare_files "$1" "${path/$curr_env/"$comp_env"}"
# Compare files and exit with its return code
compare_files "$1" "${path/$curr_env/"$comp_env"}"
exit $? # Exit with the return code from compare_files
84 changes: 18 additions & 66 deletions .github/workflows/pr_copied_files.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
name: CI Copied Files Check

on:
pull_request:
branches:
- main
branches: [main]

jobs:
changed_files:
check_files:
runs-on: ubuntu-latest
name: Test changed-files
outputs:
staging_result: ${{ steps.staging-check.outputs.result }}
prod_result: ${{ steps.prod-check.outputs.result }}
any_changed: ${{ steps.changed-files.outputs.any_changed }}
steps:
- uses: actions/checkout@v4
- name: Get changed files in specific repo folders
id: changed-files

- id: changed-files
uses: tj-actions/changed-files@v45
with:
files_yaml: |
Expand All @@ -25,69 +18,28 @@ jobs:
prod:
- charts/prod/**

- name: Run step if any file(s) in staging env change
id: staging-check
- name: Check staging files
if: steps.changed-files.outputs.staging_any_changed == 'true'
env:
STAGING_ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.staging_all_changed_files }}
run: |
echo "One or more files in the staging folder has changed."
echo "Files that have changed: $STAGING_ALL_CHANGED_FILES"
echo "result=true" >> "$GITHUB_OUTPUT"
echo "Changed files in staging:"
for file in ${{ steps.changed-files.outputs.staging_all_changed_files }}; do
res=$( ${{ github.workspace }}/.github/workflows/pr_check_copies.sh "$file" "staging" "dev" )
echo $res
if [ "$res" -eq 1 ]; then
echo "result=false" >> "$GITHUB_OUTPUT"
break;
echo "Checking $file..."
if ! ${{ github.workspace }}/.github/workflows/pr_check_copies.sh "$file" "staging" "dev"; then
echo "❌ $file differs from dev version"
exit 1
fi
done
echo "✅ All staging files match dev"


- name: Run step if any file(s) in prod env change
id: prod-check
- name: Check prod files
if: steps.changed-files.outputs.prod_any_changed == 'true'
env:
PROD_ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.prod_all_changed_files }}
run: |
echo "One or more files in the prod folder has changed."
echo "Files that have changed: $PROD_ALL_CHANGED_FILES"
echo "result=true" >> "$GITHUB_OUTPUT"
echo "Changed files in prod:"
for file in ${{ steps.changed-files.outputs.prod_all_changed_files }}; do
res=$( ${{ github.workspace }}/.github/workflows/pr_check_copies.sh "$file" "prod" "staging" )
echo $res
if [ "$res" -eq 1 ]; then
echo "result=false" >> "$GITHUB_OUTPUT"
break;
echo "Checking $file..."
if ! ${{ github.workspace }}/.github/workflows/pr_check_copies.sh "$file" "prod" "staging"; then
echo "❌ $file differs from staging version"
exit 1
fi
done

comment_pr:
needs: changed_files
if: ${{ contains(needs.changed_files.outputs.any_changed, 'true') }}
runs-on: ubuntu-latest
steps:
- name: comment_staging_pr
uses: actions/github-script@v7
if: contains(needs.changed_files.outputs.staging_result, 'true')
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '👋 All changes for Staging are an exact match to Dev'
})

- name: comment_prod_pr
uses: actions/github-script@v7
if: contains(needs.changed_files.outputs.prod_result, 'true')
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '👋 All changes for Prod are an exact match to Staging'
})

echo "✅ All prod files match staging"