|
1 | 1 | #!/bin/bash
|
2 | 2 |
|
| 3 | +######################################################################################## |
| 4 | +# common "constants" |
| 5 | +PACKAGE_INDEX_PATH=/tmp/micropython-lib-deploy |
| 6 | + |
3 | 7 | ########################################################################################
|
4 | 8 | # code formatting
|
5 | 9 |
|
@@ -38,5 +42,104 @@ function ci_build_packages_check_manifest {
|
38 | 42 | }
|
39 | 43 |
|
40 | 44 | function ci_build_packages_compile_index {
|
41 |
| - python3 tools/build.py --micropython /tmp/micropython --output /tmp/micropython-lib-deploy |
| 45 | + python3 tools/build.py --micropython /tmp/micropython --output $PACKAGE_INDEX_PATH |
| 46 | +} |
| 47 | + |
| 48 | +function ci_push_package_index { |
| 49 | + set -euo pipefail |
| 50 | + |
| 51 | + # Note: This feature is opt-in, so this function is only run by GitHub |
| 52 | + # Actions if the MICROPY_PUBLISH_MIP_INDEX repository variable is set to a |
| 53 | + # "truthy" value in the "Secrets and variables" -> "Actions" |
| 54 | + # -> "Variables" setting of the GitHub repo. |
| 55 | + |
| 56 | + PAGES_PATH=/tmp/gh-pages |
| 57 | + |
| 58 | + if git fetch --depth=1 origin gh-pages; then |
| 59 | + git worktree add ${PAGES_PATH} gh-pages |
| 60 | + cd ${PAGES_PATH} |
| 61 | + NEW_BRANCH=0 |
| 62 | + else |
| 63 | + echo "Creating gh-pages branch for $GITHUB_REPOSITORY..." |
| 64 | + git worktree add --force ${PAGES_PATH} HEAD |
| 65 | + cd ${PAGES_PATH} |
| 66 | + git switch --orphan gh-pages |
| 67 | + NEW_BRANCH=1 |
| 68 | + fi |
| 69 | + |
| 70 | + DEST_PATH=${PAGES_PATH}/mip/${GITHUB_REF_NAME} |
| 71 | + if [ -d ${DEST_PATH} ]; then |
| 72 | + git rm -r ${DEST_PATH} |
| 73 | + fi |
| 74 | + mkdir -p ${DEST_PATH} |
| 75 | + cd ${DEST_PATH} |
| 76 | + |
| 77 | + cp -r ${PACKAGE_INDEX_PATH}/* . |
| 78 | + |
| 79 | + git add . |
| 80 | + git_bot_commit "Add CI built packages from commit ${GITHUB_SHA} of ${GITHUB_REF_NAME}" |
| 81 | + |
| 82 | + if [ "$NEW_BRANCH" -eq 0 ]; then |
| 83 | + # A small race condition exists here if another CI job pushes to |
| 84 | + # gh-pages at the same time, but this narrows the race to the time |
| 85 | + # between these two commands. |
| 86 | + git pull --rebase origin gh-pages |
| 87 | + fi |
| 88 | + git push origin gh-pages |
| 89 | + |
| 90 | + INDEX_URL="https://${GITHUB_REPOSITORY_OWNER}.github.io/$(echo ${GITHUB_REPOSITORY} | cut -d'/' -f2-)/mip/${GITHUB_REF_NAME}" |
| 91 | + |
| 92 | + echo "" |
| 93 | + echo "--------------------------------------------------" |
| 94 | + echo "Uploaded package files to GitHub Pages." |
| 95 | + echo "" |
| 96 | + echo "Unless GitHub Pages is disabled on this repo, these files can be installed remotely with:" |
| 97 | + echo "" |
| 98 | + echo "mpremote mip install --index ${INDEX_URL} PACKAGE_NAME" |
| 99 | + echo "" |
| 100 | + echo "or on the device as:" |
| 101 | + echo "" |
| 102 | + echo "import mip" |
| 103 | + echo "mip.install(PACKAGE_NAME, index=\"${INDEX_URL}\")" |
| 104 | +} |
| 105 | + |
| 106 | +function ci_cleanup_package_index() |
| 107 | +{ |
| 108 | + if ! git fetch --depth=1 origin gh-pages; then |
| 109 | + exit 0 |
| 110 | + fi |
| 111 | + |
| 112 | + # Argument $1 is github.event.ref, passed in from workflow file. |
| 113 | + # |
| 114 | + # this value seems to be a REF_NAME, without heads/ or tags/ prefix. (Can't |
| 115 | + # use GITHUB_REF_NAME, this evaluates to the default branch.) |
| 116 | + DELETED_REF="$1" |
| 117 | + |
| 118 | + if [ -z "$DELETED_REF" ]; then |
| 119 | + echo "Bad DELETE_REF $DELETED_REF" |
| 120 | + exit 1 # Internal error with ref format, better than removing all mip/ directory in a commit |
| 121 | + fi |
| 122 | + |
| 123 | + # We need Actions to check out default branch and run tools/ci.sh, but then |
| 124 | + # we switch branches |
| 125 | + git switch gh-pages |
| 126 | + |
| 127 | + echo "Removing any published packages for ${DELETED_REF}..." |
| 128 | + if [ -d mip/${DELETED_REF} ]; then |
| 129 | + git rm -r mip/${DELETED_REF} |
| 130 | + git_bot_commit "Remove CI built packages from deleted ${DELETED_REF}" |
| 131 | + git pull --rebase origin gh-pages |
| 132 | + git push origin gh-pages |
| 133 | + else |
| 134 | + echo "Nothing to remove." |
| 135 | + fi |
| 136 | +} |
| 137 | + |
| 138 | +# Make a git commit with bot authorship |
| 139 | +# Argument $1 is the commit message |
| 140 | +function git_bot_commit { |
| 141 | + # Ref https://github.com/actions/checkout/discussions/479 |
| 142 | + git config user.name 'github-actions[bot]' |
| 143 | + git config user.email 'github-actions[bot]@users.noreply.github.com' |
| 144 | + git commit -m "$1" |
42 | 145 | }
|
0 commit comments