|
| 1 | +name: CI Copier |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +# Automatically stop old builds on the same branch/PR |
| 9 | +concurrency: |
| 10 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 11 | + cancel-in-progress: true |
| 12 | + |
| 13 | +jobs: |
| 14 | + pre-commit-checks: |
| 15 | + name: Pre-commit Checks |
| 16 | + timeout-minutes: 30 |
| 17 | + runs-on: ubuntu-latest |
| 18 | + steps: |
| 19 | + - name: Checkout branch |
| 20 | + uses: actions/checkout@v4 |
| 21 | + - name: Set up pixi |
| 22 | + uses: prefix-dev/setup-pixi@ba3bb36eb2066252b2363392b7739741bb777659 |
| 23 | + with: |
| 24 | + environments: default lint |
| 25 | + - name: pre-commit |
| 26 | + run: pixi run pre-commit-run --color=always --show-diff-on-failure |
| 27 | + |
| 28 | + pytest: |
| 29 | + name: pytest |
| 30 | + timeout-minutes: 30 |
| 31 | + runs-on: ubuntu-latest |
| 32 | + steps: |
| 33 | + - name: Checkout branch |
| 34 | + uses: actions/checkout@v4 |
| 35 | + - name: Set up pixi |
| 36 | + uses: prefix-dev/setup-pixi@ba3bb36eb2066252b2363392b7739741bb777659 |
| 37 | + - name: Cache pre-commit envs |
| 38 | + uses: actions/cache@v4 |
| 39 | + with: |
| 40 | + path: | |
| 41 | + ~/.cache/pre-commit |
| 42 | + key: project-generation-pre-commit-${{ hashFiles('template/.pre-commit-config.yaml.jinja') }} |
| 43 | + - name: Test |
| 44 | + run: pixi run test |
| 45 | + |
| 46 | + test-generated-package-ci: |
| 47 | + name: Test CI of generated package (minimal-python = ${{ matrix.minimal-python-version }}) |
| 48 | + timeout-minutes: 30 |
| 49 | + runs-on: ubuntu-latest |
| 50 | + strategy: |
| 51 | + matrix: |
| 52 | + minimal-python-version: [py38, py310] |
| 53 | + |
| 54 | + steps: |
| 55 | + - name: Checkout branch |
| 56 | + uses: actions/checkout@v4 |
| 57 | + with: |
| 58 | + ssh-key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 59 | + - name: Set up pixi |
| 60 | + uses: prefix-dev/setup-pixi@ba3bb36eb2066252b2363392b7739741bb777659 |
| 61 | + - name: Test generated package CI |
| 62 | + run: | |
| 63 | + # Name of the generated package. |
| 64 | + # Authentication for pushing to $REPO. |
| 65 | + AUTH='authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' |
| 66 | + eval $(ssh-agent) |
| 67 | + ssh-add - <<< "${{ secrets.SSH_PRIVATE_KEY }}" |
| 68 | + # Set up local Git so that copier can run "git commit". |
| 69 | + git config --global user.email "landocalrissian@example.com" |
| 70 | + git config --global user.name "Lando Calrissian" |
| 71 | + # Generate package with default settings + Windows CI. |
| 72 | + copier copy \ |
| 73 | + --data project_name="Package" \ |
| 74 | + --data project_short_description="Example Package" \ |
| 75 | + --data github_user="LandoCalrissian" \ |
| 76 | + --data project_slug="package" \ |
| 77 | + --data minimal_python_version="${{ matrix.minimal-python-version }}" \ |
| 78 | + --defaults \ |
| 79 | + --trust \ |
| 80 | + . out |
| 81 | + cd out |
| 82 | + # Push the generated package's HEAD commit to a `ci/*` branch |
| 83 | + cid=$(git rev-parse HEAD) |
| 84 | + git push -f "${GITHUB_SERVER_URL/https:\/\//git@}:$GITHUB_REPOSITORY" $cid:refs/heads/ci/$GITHUB_SHA |
| 85 | + # Use the GitHub API to wait for the generated package's CI to complete (success or failure). |
| 86 | + # We look for a GitHub Actions run for the HEAD commit ID. |
| 87 | + WORKFLOW_URL="$GITHUB_API_URL/repos/${GITHUB_REPOSITORY}/actions/runs?branch=ci/${GITHUB_SHA}&head_sha=${cid}" |
| 88 | + echo "Waiting for inner CI to start" |
| 89 | + while (( $(curl -Ls --header "$AUTH" "$WORKFLOW_URL" | jq -r ".workflow_runs | length") < 1 )); do |
| 90 | + sleep 10 |
| 91 | + done |
| 92 | + echo "Waiting for inner CI to complete" |
| 93 | + while curl -Ls --header "$AUTH" "$WORKFLOW_URL" | jq -r ".workflow_runs | .[] | .status" | grep --invert-match completed > /dev/null; do |
| 94 | + sleep 10 |
| 95 | + done |
| 96 | + # Fail unless CI was successful. |
| 97 | + if curl -Ls --header "$AUTH" "$WORKFLOW_URL" | jq -r ".workflow_runs | .[] | .conclusion" | grep --invert-match success > /dev/null; then |
| 98 | + echo "CI pipeline failed" |
| 99 | + exit 1 |
| 100 | + fi |
| 101 | + - name: Clean up CI branch |
| 102 | + if: always() |
| 103 | + run: | |
| 104 | + AUTH='authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' |
| 105 | + eval $(ssh-agent) |
| 106 | + ssh-add - <<< "${{ secrets.SSH_PRIVATE_KEY }}" |
| 107 | +
|
| 108 | + git push -d "${GITHUB_SERVER_URL/https:\/\//git@}:$GITHUB_REPOSITORY" refs/heads/ci/$GITHUB_SHA |
| 109 | +
|
| 110 | + for line in $(curl -Ls --header "$AUTH" "$GITHUB_API_URL/repos/${GITHUB_REPOSITORY}/actions/runs?branch=ci/${GITHUB_SHA}&head_sha=${cid}" | jq -r ".workflow_runs | .[] | select(.status != \"completed\") | .id") |
| 111 | + do |
| 112 | + curl -Ls --header "$AUTH" --request POST "$GITHUB_API_URL/repos/${GITHUB_REPOSITORY}/actions/runs/$line/cancel" > /dev/null |
| 113 | + done |
0 commit comments