Skip to content

refactor actions, included programs from .ghaignore #330

refactor actions, included programs from .ghaignore

refactor actions, included programs from .ghaignore #330

Workflow file for this run

name: Anchor
on:
push:
branches: [main]
pull_request:
branches: [main]
env:
SOLANA_VERSION: 1.18.17
ANCHOR_VERSION: 0.30.1
jobs:
changes:
runs-on: ubuntu-latest
outputs:
project_batches: ${{ steps.get-projects.outputs.project_batches }}
steps:
- uses: actions/checkout@v4
- name: Get projects to process
id: get-projects
shell: bash
run: |
set -e
get_changed_projects() {
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} || echo "Git diff failed"
elif [[ "${{ github.event_name }}" == "push" ]]; then
git diff --name-only ${{ github.event.before }} ${{ github.event.after }} || echo "Git diff failed"
else
echo "Not a pull request or push event"
fi
}
create_ignore_regex() {
grep -v '^#' .github/.ghaignore 2>/dev/null | sed 's/^/^/' | sed 's/$/\//' | tr '\n' '|' | sed 's/|$//' || echo ""
}
filter_projects() {
local ignore_regex="$1"
if [[ -n "$ignore_regex" ]]; then
grep -vE "$ignore_regex" || true
else
cat
fi
}
batch_projects() {
local batch_size=5
jq -R -s 'split("\n")[:-1] | _nwise('"$batch_size"') | map(select(length > 0))'
}
ignore_regex=$(create_ignore_regex)
changed_files=$(get_changed_projects)
if [[ $changed_files == *"Git diff failed"* || $changed_files == *"Not a pull request or push event"* ]]; then
echo "::warning::Failed to get changed files. Processing all projects."
projects=$(find . -type d -name "anchor")
else
projects=$(echo "$changed_files" | grep '/anchor/' | sed 's/\/anchor\/.*$/\/anchor/' | sort -u)
fi
filtered_projects=$(echo "$projects" | filter_projects "$ignore_regex")
if [[ -z "$filtered_projects" ]]; then
echo "::notice::No projects to process after filtering."
echo "project_batches=[]" >> $GITHUB_OUTPUT
else
batches=$(echo "$filtered_projects" | batch_projects)
echo "project_batches=$batches" >> $GITHUB_OUTPUT
echo "::notice::Projects to process: $filtered_projects"
fi
build-and-test:
needs: changes
runs-on: ubuntu-latest
strategy:
matrix:
projects: ${{ fromJson(needs.changes.outputs.project_batches) }}
fail-fast: false
steps:
- uses: actions/checkout@v4
- name: Setup Anchor and Solana
uses: heyAyushh/[email protected]
with:
anchor-version: ${{ env.ANCHOR_VERSION }}
solana-cli-version: ${{ env.SOLANA_VERSION }}
- uses: pnpm/action-setup@v2
with:
version: 8
- name: Build and Test Projects
run: |
process_project() {
local project=$1
echo "Processing $project"
cd "$project"
# Build the project
if ! anchor build; then
echo "::error file=$project/Cargo.toml::Failed to build $project"
echo "- :x: $project (Build Failed)" >> $GITHUB_STEP_SUMMARY
return 1
fi
# Run tests
if ! pnpm install --frozen-lockfile || ! anchor test; then
echo "::error file=$project/Cargo.toml::Failed to test $project"
echo "- :x: $project (Test Failed)" >> $GITHUB_STEP_SUMMARY
return 1
fi
echo "- :white_check_mark: $project" >> $GITHUB_STEP_SUMMARY
cd - > /dev/null
}
echo "## Project Results" >> $GITHUB_STEP_SUMMARY
for project in ${{ toJson(matrix.projects) }}; do
process_project "$project"
done
summary:
needs: build-and-test
runs-on: ubuntu-latest
if: always()
steps:
- name: Summary
run: |
echo "## Anchor Workflow Summary" >> $GITHUB_STEP_SUMMARY
echo "Solana Version: ${{ env.SOLANA_VERSION }}" >> $GITHUB_STEP_SUMMARY
echo "Anchor Version: ${{ env.ANCHOR_VERSION }}" >> $GITHUB_STEP_SUMMARY
if [[ "${{ needs.build-and-test.result }}" == "success" ]]; then
echo ":white_check_mark: All projects completed successfully" >> $GITHUB_STEP_SUMMARY
else
echo ":x: Some projects encountered errors" >> $GITHUB_STEP_SUMMARY
fi
echo "Check individual job logs for details on errors and failures." >> $GITHUB_STEP_SUMMARY