Preventing tests from running if no relenant changes pushed #28
This file contains hidden or 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: Versioning & Release | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
pre-checks: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 2 # Ensure we have at least the last commit to compare | |
- name: Check Modified Files | |
id: check_changes | |
run: | # Ensure history exists for comparison | |
git fetch origin main --depth=2 | |
CHANGED_FILES=$(git diff --name-only HEAD^ HEAD) | |
# Check if cli_monitor.py or tests/ directory has been changed | |
if echo "$CHANGED_FILES" | grep -qE '^(cli_monitor.py|tests/)'; then | |
echo "RELEASE_NEEDED=true" >> $GITHUB_ENV | |
else | |
echo "RELEASE_NEEDED=false" >> $GITHUB_ENV | |
fi | |
- name: Skip Release if No Relevant Changes | |
if: env.RELEASE_NEEDED == 'false' | |
run: | | |
echo "No relevant changes detected. Skipping version bump." | |
exit 0 | |
check-tests: | |
runs-on: ubuntu-latest | |
needs: pre-checks # Ensures pre-checks are completed first | |
steps: | |
- name: Download All Test Results | |
uses: actions/download-artifact@v4 | |
with: | |
path: test-results # Store all downloaded test result files here | |
- name: Aggregate Test Results | |
id: aggregate_tests | |
run: | | |
FAILED_TESTS=0 | |
if [ -d "test-results" ]; then | |
for file in test-results/*/test_result_*; do | |
TEST_STATUS=$(cat "$file") | |
if [[ "$TEST_STATUS" == "failure" ]]; then | |
FAILED_TESTS=$((FAILED_TESTS + 1)) | |
fi | |
done | |
fi | |
echo "TOTAL_FAILED_TESTS=$FAILED_TESTS" >> $GITHUB_ENV | |
echo "Failed tests: $FAILED_TESTS" | |
- name: Read Commit Message | |
id: commit_msg | |
run: echo "COMMIT_MSG=$(git log -1 --pretty=%B)" >> $GITHUB_ENV | |
- name: Skip Release if Any Test Failed (Unless Forced) | |
if: env.TOTAL_FAILED_TESTS != '0' && !contains(env.COMMIT_MSG, 'FORCE-RELEASE') | |
run: | | |
echo "Tests failed ($TOTAL_FAILED_TESTS jobs failed). Skipping release." | |
exit 1 | |
versioning: | |
needs: check-tests | |
runs-on: ubuntu-latest | |
if: success() # Only runs if all tests passed | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Read Current Version | |
id: get_version | |
run: | | |
VERSION=$(grep -m 1 '^__version__ =' cli_monitor.py | awk -F'=' '{print $2}' | sed 's/[^0-9.]//g') | |
echo "CURRENT_VERSION=$VERSION" >> $GITHUB_ENV | |
- name: Determine Version Increment | |
id: version_bump | |
run: | | |
CURRENT_VERSION=${{ env.CURRENT_VERSION }} | |
MAJOR=$(echo $CURRENT_VERSION | cut -d. -f1) | |
MINOR=$(echo $CURRENT_VERSION | cut -d. -f2) | |
PATCH=$(echo $CURRENT_VERSION | cut -d. -f3) | |
COMMIT_MSG=$(git log -1 --pretty=%B) | |
if [[ "$COMMIT_MSG" == *"MAJOR"* ]]; then | |
MAJOR=$((MAJOR + 1)) | |
MINOR=0 | |
PATCH=0 | |
elif [[ "$COMMIT_MSG" == *"MINOR"* ]]; then | |
MINOR=$((MINOR + 1)) | |
PATCH=0 | |
elif [[ "$COMMIT_MSG" == *"PATCH"* ]]; then | |
PATCH=$((PATCH + 1)) | |
fi | |
NEW_VERSION="$MAJOR.$MINOR.$PATCH" | |
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV | |
echo "Version updated to $NEW_VERSION" | |
- name: Update Version in `cli_monitor.py` | |
run: sed -i "s/^__version__ = .*/__version__ = \"${{ env.NEW_VERSION }}\"/" cli_monitor.py | |
- name: Commit & Push Version Update | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
git config --global user.name "github-actions[bot]" | |
git config --global user.email "github-actions@users.noreply.github.com" | |
# Ensure GitHub uses GITHUB_TOKEN for authentication | |
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/Dimos082/cli-monitor.git | |
git add cli_monitor.py | |
git commit -m "chore: bump version to ${{ env.NEW_VERSION }}" || echo "No changes to commit" | |
git push origin main | |
- name: Create GitHub Release | |
uses: softprops/action-gh-release@v2 | |
with: | |
tag_name: v${{ env.NEW_VERSION }} | |
name: Release v${{ env.NEW_VERSION }} | |
body: "Automated release for version ${{ env.NEW_VERSION }}" | |
draft: false | |
prerelease: false | |
files: cli_monitor.py |