From 4e558f994959441521bd2ef69c1012af86772683 Mon Sep 17 00:00:00 2001 From: Kwankyu Lee Date: Mon, 18 Dec 2023 15:32:59 +0900 Subject: [PATCH] Add binder.yml --- .github/workflows/binder-dockerfile | 78 +++++++++++++++++++++++++++++ .github/workflows/binder.yml | 46 +++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 .github/workflows/binder-dockerfile create mode 100644 .github/workflows/binder.yml diff --git a/.github/workflows/binder-dockerfile b/.github/workflows/binder-dockerfile new file mode 100644 index 00000000000..d82b5bd5bcd --- /dev/null +++ b/.github/workflows/binder-dockerfile @@ -0,0 +1,78 @@ +# Dockerfile for Binder +# Reference: https://mybinder.readthedocs.io/en/latest/tutorials/dockerfile.html + +# Pull the Sage docker image +FROM ghcr.io/sagemath/sage/sage-ubuntu-focal-standard-with-targets:dev + +USER root + +# Remove warnings +ENV DEBIAN_FRONTEND noninteractive +ENV DEBCONF_NOWARNINGS="yes" + +# Install system packages +RUN apt-get update +RUN apt-get install -y apt-utils +RUN apt-get install -y git +RUN apt-get install -y sudo +RUN apt-get install -y python3-pip +RUN apt-get install -y texlive +RUN apt-get install -y texlive-luatex +RUN apt-get install -y preview-latex-style +RUN apt-get install -y fonts-freefont-otf +RUN apt-get install -y xindy + +# Install jupyterlab +RUN python3 -m pip install --no-warn-script-location jupyterlab + +# Disable the pupup of Jupyter news +RUN jupyter labextension disable "@jupyterlab/apputils-extension:announcements" + +# Create the user alice with uid 1000 and password alice +ARG NB_USER=alice +ARG NB_UID=1000 +ENV NB_USER alice +ENV NB_UID 1000 +ENV HOME /home/${NB_USER} +RUN adduser --disabled-password --gecos "Default user" --uid ${NB_UID} ${NB_USER} +RUN echo 'alice:alice' | chpasswd && adduser alice sudo + +# Prepare for building Sage +ENV PR_REPO https://github.com/REPO_NAME +ENV PR_BRANCH BRANCH_NAME +ENV BINDER_BRANCH sagemath-environment + +# Build Sage incrementally +WORKDIR /sage +RUN export PATH="/sage/build/bin:$PATH" +RUN git config --global user.email ${NB_USER}@wonderland +RUN git config --global user.name ${NB_USER} +RUN git config --global --add safe.directory $(pwd) +RUN pwd +COPY .gitignore /sage/.gitignore +RUN if [ ! -d .git ]; then git init && git add -A; fi +RUN git fetch ${PR_REPO} ${PR_BRANCH} +RUN git checkout -f -b ${BINDER_BRANCH} FETCH_HEAD +RUN ./bootstrap && ./configure --enable-build-as-root && make -j4 build + +# Make sure the contents of the notebooks directory are in ${HOME} +COPY notebooks/* ${HOME}/ +RUN chown -R ${NB_UID} ${HOME} + +# Install jupyterlab to Sage +RUN /sage/sage -pip install --no-warn-script-location jupyterlab + +# Install Sage packages +# RUN /sage/sage -i + +# Switch to the user +USER ${NB_USER} + +# This is where kernels are installed +RUN mkdir -p $(jupyter --data-dir)/kernels + +# Install sagemath kernel +RUN ln -s /sage/venv/share/jupyter/kernels/sagemath $(jupyter --data-dir)/kernels + +# Start in the home directory of the user +WORKDIR /home/${NB_USER} diff --git a/.github/workflows/binder.yml b/.github/workflows/binder.yml new file mode 100644 index 00000000000..e0f2585197c --- /dev/null +++ b/.github/workflows/binder.yml @@ -0,0 +1,46 @@ +name: Create Binder branch + +# How to use this workflow +# +# (1) Go to your forked Sage repo +# (2) In the Actions tab, find "Create Binder branch" workflow +# (3) Run the workflow with your PR branch named say "contribution" +# (4) Wait for the workflow run to finish +# (5) In the Code tab, select the new "contribution-binder" branch +# (6) Find the Binder badge in README.md +# (7) Copy the Binder badge from README.md (in edit mode) +# (8) Paste the Binder badge into the description of your PR in the sagemath/sage repo +# (9) Click the Binder badge to start creating the Binder environment +# (10) Leave it open and come back after an hour. +# (11) The Binder badge is now ready for reviewers of your PR + +on: workflow_dispatch + +jobs: + create-branch: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Create binder branch + run: | + set -ex + REPO_NAME=${{ github.repository }} + BRANCH_NAME=${GITHUB_REF#refs/heads/} + BINDER_REPO_NAME=${REPO_NAME} + BINDER_BRANCH_NAME=${BRANCH_NAME}-binder + cp .github/workflows/binder-dockerfile Dockerfile + sed -i 's/REPO_NAME/'$(echo "${REPO_NAME}" | sed 's/\//\\\//g')'/' Dockerfile + sed -i 's/BRANCH_NAME/'$(echo "${BRANCH_NAME}" | sed 's/\//\\\//g')'/' Dockerfile + sed -i 's/sagemath\/sage-binder-env\/master/'$(echo "${BINDER_REPO_NAME}/${BINDER_BRANCH_NAME}" | sed 's/\//\\\//g')'/' README.md + if [ ! -d .git ]; then git init; fi + if [ ! -d notebooks ]; then mkdir notebooks && touch notebooks/my_favorite_notebooks.md; fi + git config --global user.email alice@wonderland + git config --global user.name Alice + git config --global --add safe.directory $(pwd) + git checkout -b $BINDER_BRANCH_NAME + git add -A + git commit -a -m "Binder environment for '${BRANCH_NAME}' branch" + git push -f origin $BINDER_BRANCH_NAME