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