|  | 
|  | 1 | +name: Build and deploy docs | 
|  | 2 | + | 
|  | 3 | +on: | 
|  | 4 | +  # Trigger the workflow on push | 
|  | 5 | +  push: | 
|  | 6 | +    # Selected branches | 
|  | 7 | +    branches: [develop, master, docs] | 
|  | 8 | +  # Allows you to run this workflow manually from the Actions tab | 
|  | 9 | +  workflow_dispatch: | 
|  | 10 | + | 
|  | 11 | +# Allow only one concurrent workflow, skipping runs queued between the run | 
|  | 12 | +# in-progress and latest queued. And cancel in-progress runs. | 
|  | 13 | +concurrency: | 
|  | 14 | +  group: | 
|  | 15 | +    ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | 
|  | 16 | +  cancel-in-progress: true | 
|  | 17 | + | 
|  | 18 | +env: | 
|  | 19 | +  # Set the environment variables to be used in all jobs defined in this workflow | 
|  | 20 | +  # Set the CI_BRANCH environment variable to be the branch name | 
|  | 21 | +  # Set the NOTEBOOKS_DIR environment variable to be the directory containing the Jupyter notebooks | 
|  | 22 | +  CI_BRANCH: ${{ github.head_ref || github.ref_name }} | 
|  | 23 | +  NOTEBOOKS_DIR: tutorials | 
|  | 24 | + | 
|  | 25 | +jobs: | 
|  | 26 | +  # Job 1: Build the static files for the documentation site | 
|  | 27 | +  build-docs: | 
|  | 28 | +    strategy: | 
|  | 29 | +      matrix: | 
|  | 30 | +        os: [macos-14] # Use macOS to switch to dark mode for Plotly charts | 
|  | 31 | +        python-version: ['3.13'] | 
|  | 32 | + | 
|  | 33 | +    runs-on: ${{ matrix.os }} | 
|  | 34 | + | 
|  | 35 | +    steps: | 
|  | 36 | +      # Without this step, GITHUB_REPOSITORY is not accessible from mkdocs.yml | 
|  | 37 | +      - name: Get GitHub repository | 
|  | 38 | +        run: echo "GITHUB_REPOSITORY=$GITHUB_REPOSITORY" >> $GITHUB_ENV | 
|  | 39 | + | 
|  | 40 | +      # Save the latest release version of easyscience/diffraction-lib to RELEASE_VERSION | 
|  | 41 | +      # RELEASE_VERSION is used in the mkdocs.yml file to set release_version. | 
|  | 42 | +      # The release_version is then needed to display the latest release version in the index.md file | 
|  | 43 | +      - name: Get the latest release version of easydiffraction library | 
|  | 44 | +        run: | | 
|  | 45 | +          git clone --depth 1 https://github.com/easyscience/${{ github.event.repository.name }} . | 
|  | 46 | +          git fetch --tags | 
|  | 47 | +          echo "RELEASE_VERSION=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV | 
|  | 48 | +
 | 
|  | 49 | +      # Activate dark mode to create documentation with Plotly charts in dark mode | 
|  | 50 | +      # Need a better solution to automatically switch the chart colour theme based on the mkdocs material switcher | 
|  | 51 | +      # Something similar to mkdocs_plotly_plugin https://haoda-li.github.io/mkdocs-plotly-plugin/, | 
|  | 52 | +      # but for generating documentation from notepads | 
|  | 53 | +      #- name: Activate dark mode | 
|  | 54 | +      #  run: | | 
|  | 55 | +      #    brew install dark-mode | 
|  | 56 | +      #    dark-mode status | 
|  | 57 | +      #    dark-mode on | 
|  | 58 | +      #    dark-mode status | 
|  | 59 | + | 
|  | 60 | +      - name: Check-out repository | 
|  | 61 | +        uses: actions/checkout@v4 | 
|  | 62 | + | 
|  | 63 | +      - name: Set up Python ${{ matrix.python-version }} | 
|  | 64 | +        uses: actions/setup-python@v5 | 
|  | 65 | +        with: | 
|  | 66 | +          python-version: ${{ matrix.python-version }} | 
|  | 67 | + | 
|  | 68 | +      - name: Upgrade package installer for Python | 
|  | 69 | +        shell: bash | 
|  | 70 | +        run: python -m pip install --upgrade pip | 
|  | 71 | + | 
|  | 72 | +      # Install EasyDiffraction Library to run Jupyter notebooks | 
|  | 73 | +      # Install with the 'docs' and 'visualization' extras | 
|  | 74 | +      - name: Install EasyDiffraction Library and its dependencies | 
|  | 75 | +        run: python -m pip install .'[dev,docs,visualization]' | 
|  | 76 | + | 
|  | 77 | +      # Clone assets extra from: | 
|  | 78 | +      # - easyscience/assets-docs | 
|  | 79 | +      # - easyscience/assets-branding | 
|  | 80 | +      - name: Clone easyscience/assets-docs and easyscience/assets-branding | 
|  | 81 | +        run: | | 
|  | 82 | +          cd .. | 
|  | 83 | +          git clone https://github.com/easyscience/assets-docs.git | 
|  | 84 | +          git clone https://github.com/easyscience/assets-branding.git | 
|  | 85 | +
 | 
|  | 86 | +      # Add the extra files from the easyscience/assets-docs repository | 
|  | 87 | +      - name: Add files from easyscience/assets-docs files | 
|  | 88 | +        run: | | 
|  | 89 | +          cp -R ../assets-docs/docs/assets/ docs/assets/ | 
|  | 90 | +          cp -R ../assets-docs/includes/ includes/ | 
|  | 91 | +          cp -R ../assets-docs/overrides/ overrides/ | 
|  | 92 | +
 | 
|  | 93 | +      # Add the extra files from the easyscience/assets-branding repository | 
|  | 94 | +      - name: Add files from easyscience/assets-branding files | 
|  | 95 | +        run: | | 
|  | 96 | +          mkdir -p docs/assets/images/ | 
|  | 97 | +          cp ../assets-branding/easydiffraction/hero/dark.png docs/assets/images/hero_dark.png | 
|  | 98 | +          cp ../assets-branding/easydiffraction/hero/light.png docs/assets/images/hero_light.png | 
|  | 99 | +          cp ../assets-branding/easydiffraction/logos/dark.svg docs/assets/images/logo_dark.svg | 
|  | 100 | +          cp ../assets-branding/easydiffraction/logos/light.svg docs/assets/images/logo_light.svg | 
|  | 101 | +          cp ../assets-branding/easydiffraction/icons/color.png docs/assets/images/favicon.png | 
|  | 102 | +          mkdir -p overrides/.icons/ | 
|  | 103 | +          cp ../assets-branding/easydiffraction/icons/bw.svg overrides/.icons/easydiffraction.svg | 
|  | 104 | +          cp ../assets-branding/easyscience-org/icons/eso-icon_bw.svg overrides/.icons/easyscience.svg | 
|  | 105 | +
 | 
|  | 106 | +      # Convert python scripts in the notebooks directory to Jupyter notebooks | 
|  | 107 | +      # Strip output from the notebooks and simpify cell ids | 
|  | 108 | +      # The notebooks are used to generate the documentation | 
|  | 109 | +      - name: | 
|  | 110 | +          Convert ${{ env.NOTEBOOKS_DIR }}/*.py to docs/${{env.NOTEBOOKS_DIR | 
|  | 111 | +          }}/*.ipynb | 
|  | 112 | +        run: | | 
|  | 113 | +          cp -R ${{ env.NOTEBOOKS_DIR }}/data docs/${{ env.NOTEBOOKS_DIR }}/ | 
|  | 114 | +          jupytext ${{ env.NOTEBOOKS_DIR }}/*.py --from py:percent --to ipynb | 
|  | 115 | +          nbstripout ${{ env.NOTEBOOKS_DIR }}/*.ipynb | 
|  | 116 | +          mv ${{ env.NOTEBOOKS_DIR }}/*.ipynb docs/${{ env.NOTEBOOKS_DIR }}/ | 
|  | 117 | +
 | 
|  | 118 | +      # The following step is needed to avoid the following message during the build: | 
|  | 119 | +      # "Matplotlib is building the font cache; this may take a moment" | 
|  | 120 | +      - name: Pre-build site step | 
|  | 121 | +        run: | | 
|  | 122 | +          export PYTHONPATH="$(pwd)/src${PYTHONPATH:+:$PYTHONPATH}" | 
|  | 123 | +          python -c "import easydiffraction" | 
|  | 124 | +
 | 
|  | 125 | +      # Create the mkdocs.yml configuration file | 
|  | 126 | +      # The file is created by merging two files: | 
|  | 127 | +      # - assets-docs/mkdocs.yml - the common configuration (theme, plugins, etc.) | 
|  | 128 | +      # - docs/mkdocs.yml - the project-specific configuration (project name, TOC, etc.) | 
|  | 129 | +      - name: Create mkdocs.yml file | 
|  | 130 | +        run: python tools/create_mkdocs-yml.py | 
|  | 131 | + | 
|  | 132 | +      # Build the static files | 
|  | 133 | +      # Input: docs/ directory containing the Markdown files | 
|  | 134 | +      # Output: site/ directory containing the generated HTML files | 
|  | 135 | +      - name: Build site with MkDocs | 
|  | 136 | +        run: | | 
|  | 137 | +          export JUPYTER_PLATFORM_DIRS=1 | 
|  | 138 | +          export PYTHONWARNINGS="ignore::RuntimeWarning" | 
|  | 139 | +          export PYTHONPATH="$(pwd)/src${PYTHONPATH:+:$PYTHONPATH}" | 
|  | 140 | +          mkdocs build | 
|  | 141 | +
 | 
|  | 142 | +      # Set up the Pages action to configure the static files to be deployed | 
|  | 143 | +      # NOTE: The repository must have GitHub Pages enabled and configured to build using GitHub Actions | 
|  | 144 | +      # This can be done via https://github.com/easyscience/diffraction-lib/settings/pages | 
|  | 145 | +      # Select: Build and deploy - Source - GitHub Actions | 
|  | 146 | +      - name: Setup GitHub Pages | 
|  | 147 | +        uses: actions/configure-pages@v5 | 
|  | 148 | + | 
|  | 149 | +      # Upload the static files from the site/ directory to be used in the next job | 
|  | 150 | +      # This artifact is named github-pages and is a single gzip archive containing a single tar file | 
|  | 151 | +      # The artifact is then used in the next job by actions/deploy-pages to deploy the static files to GitHub Pages | 
|  | 152 | +      # Unfortunately, the artifact is not available for download, so extra steps below are needed to do similar things | 
|  | 153 | +      - name: | 
|  | 154 | +          Upload built site as artifact for easyscience.github.io/${{ | 
|  | 155 | +          github.event.repository.name }} (all branches) | 
|  | 156 | +        uses: actions/upload-pages-artifact@v3 | 
|  | 157 | +        with: | 
|  | 158 | +          path: site/ | 
|  | 159 | + | 
|  | 160 | +      # Upload the static files from the site/ directory to be used in the next job | 
|  | 161 | +      # This extra step is needed to allow the download of the artifact in the next job | 
|  | 162 | +      # for pushing its content to the branch named 'gh_pages' | 
|  | 163 | +      - name: Upload built site as artifact for gh_pages (master branch) | 
|  | 164 | +        if: ${{ env.CI_BRANCH == 'master' }} | 
|  | 165 | +        uses: actions/upload-artifact@v4 | 
|  | 166 | +        with: | 
|  | 167 | +          name: artifact # name of the artifact (without the extension zip) | 
|  | 168 | +          path: site/ | 
|  | 169 | +          if-no-files-found: 'error' | 
|  | 170 | +          compression-level: 0 | 
|  | 171 | + | 
|  | 172 | +  # Job 2: Deploy the static files | 
|  | 173 | +  deploy-docs: | 
|  | 174 | +    needs: build-docs # previous job 'build-docs' need to be finished first | 
|  | 175 | + | 
|  | 176 | +    # Grant GITHUB_TOKEN the permissions required to make a Pages deployment | 
|  | 177 | +    permissions: | 
|  | 178 | +      contents: read | 
|  | 179 | +      pages: write # to deploy to Pages | 
|  | 180 | +      id-token: write # to verify the deployment, originates from an appropriate source | 
|  | 181 | + | 
|  | 182 | +    # Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. | 
|  | 183 | +    # However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. | 
|  | 184 | +    concurrency: | 
|  | 185 | +      group: 'pages' | 
|  | 186 | +      cancel-in-progress: false | 
|  | 187 | + | 
|  | 188 | +    # Deploy to the github-pages environment | 
|  | 189 | +    environment: | 
|  | 190 | +      name: github-pages # Artifact name | 
|  | 191 | +      url: ${{ steps.deployment.outputs.page_url }} | 
|  | 192 | + | 
|  | 193 | +    runs-on: ubuntu-latest | 
|  | 194 | + | 
|  | 195 | +    steps: | 
|  | 196 | +      # Deploy the static files created in the previous job to GitHub Pages | 
|  | 197 | +      # To allow the deployment of the static files to GitHub Pages, no | 
|  | 198 | +      # restrictions on the branch name need to be set for desired branches on | 
|  | 199 | +      # https://github.com/easyscience/diffraction-lib/settings/environments | 
|  | 200 | +      # Currently, only develop and master branches are allowed to deploy to GitHub Pages | 
|  | 201 | +      # Deployed pages are available at https://easyscience.github.io/diffraction-lib | 
|  | 202 | +      - name: | 
|  | 203 | +          Deploy to easyscience.github.io/${{ github.event.repository.name }} | 
|  | 204 | +          (all branches) | 
|  | 205 | +        uses: actions/deploy-pages@v4 | 
|  | 206 | + | 
|  | 207 | +      # Download built site as artifact from a previous job for gh_pages (master branch) | 
|  | 208 | +      - name: Download built site from previous job (master branch) | 
|  | 209 | +        if: ${{ env.CI_BRANCH == 'master' }} | 
|  | 210 | +        uses: actions/download-artifact@v4 | 
|  | 211 | +        with: # name or path are taken from the upload step of the previous job | 
|  | 212 | +          name: artifact | 
|  | 213 | +          path: site/ # directory to extract downloaded zipped artifacts | 
|  | 214 | + | 
|  | 215 | +      # Push the site files created in the previous job to the gh_pages branch | 
|  | 216 | +      # To be able to push to the gh_pages branch, the personal GitHub API access | 
|  | 217 | +      # token GH_API_PERSONAL_ACCSESS_TOKEN must be set for this repository via | 
|  | 218 | +      # https://github.com/easyscience/diffraction-lib/settings/secrets/actions | 
|  | 219 | +      # This branch is used to deploy the site to the custom domain. | 
|  | 220 | +      # Deploying is done with a webhook: | 
|  | 221 | +      # https://github.com/easyscience/diffraction-lib/settings/hooks | 
|  | 222 | +      # This is done for the gh_pages branch when the site is tested with a step above | 
|  | 223 | +      - name: | 
|  | 224 | +          Deploy to gh_pages branch to trigger deployment to custom domain | 
|  | 225 | +          (master branch) | 
|  | 226 | +        if: ${{ env.CI_BRANCH == 'master' }} | 
|  | 227 | +        uses: s0/git-publish-subdir-action@develop | 
|  | 228 | +        env: | 
|  | 229 | +          GITHUB_TOKEN: ${{ secrets.GH_API_PERSONAL_ACCSESS_TOKEN }} | 
|  | 230 | +          REPO: self | 
|  | 231 | +          BRANCH: gh_pages | 
|  | 232 | +          FOLDER: site | 
0 commit comments