|
| 1 | +--- |
| 2 | +category: Documentation |
| 3 | +categoryindex: 1 |
| 4 | +index: 9 |
| 5 | +--- |
| 6 | +# From zero to hero: deploying to GitHub Pages |
| 7 | + |
| 8 | +This guide is meant for a typical setup of open-source projects on GitHub. |
| 9 | +We start from a repository without any documentation and aim to end up with a published website on [GitHub Pages](https://pages.github.com/). |
| 10 | + |
| 11 | +## Install the local tool |
| 12 | + |
| 13 | +If you don't have a [dotnet tool manifest](https://learn.microsoft.com/en-us/dotnet/core/tools/local-tools-how-to-use#create-a-manifest-file), you can create one using `dotnet new tool-manifest`. |
| 14 | + |
| 15 | +Next, we can install [fsdocs-tool](https://www.nuget.org/packages/fsdocs-tool/) using `dotnet tool install --local fsdocs-tool`. |
| 16 | +It is recommended to install this tool as a local tool because it allows us to update to newer versions of the tool at our own pace. |
| 17 | + |
| 18 | +## Create the docs folder |
| 19 | + |
| 20 | +After we've installed the tool, we can run `dotnet fsdocs --help` and see the available commands. |
| 21 | +Both `build` and `watch` will generate the documentation for a solution and an input folder. |
| 22 | +The default folder for `--input` is the `./docs` folder, relative to the current working directory. |
| 23 | + |
| 24 | +Typically your project will be structured like: |
| 25 | + |
| 26 | +``` |
| 27 | +/repository-root |
| 28 | + YourSolution.sln |
| 29 | + ./docs |
| 30 | + index.md |
| 31 | + other-file.md |
| 32 | + ./src |
| 33 | + ./Project1/Project1.fsproj |
| 34 | + ./Project2/Project2.fsproj |
| 35 | +``` |
| 36 | + |
| 37 | +It is recommended to have a single solution at the root. In some editors, it is more convenient to open a solution at the root, to easily manipulate any file in root repository folder. |
| 38 | +When users clone your repository locally, they cannot be confused on how they need to open the project in their IDE. |
| 39 | + |
| 40 | +⚠️ Please avoid putting your solution in the `src` folder. When we open that solution, it can be more difficult to edit files in the `docs` folder, as we can sometimes only see the `src` folder. |
| 41 | + |
| 42 | +That being said, let's create the `docs` folder and a first Markdown file named `index.md`. |
| 43 | +When `fsdocs` runs, it will transform this `index.md` file to `index.html`, which will be served at the root. |
| 44 | + |
| 45 | +We can put `# Hello world` in the markdown file for now. |
| 46 | + |
| 47 | +Having this in place, should already serve the first page when we start the `watch` command: |
| 48 | + |
| 49 | +> dotnet fsdocs watch |
| 50 | +
|
| 51 | +Open [http://localhost:8901](http://localhost:8901) and you should see our first page! |
| 52 | + |
| 53 | +🪄 You might notice that there are some images missing. You can add these in the `docs` folder in the right location. |
| 54 | + |
| 55 | +## Generating API documentation |
| 56 | + |
| 57 | +By default, `fsdocs` will generate API documentation for the configured `--projects`. |
| 58 | +When this flag is not specified, `fsdocs` will look for solutions or projects in the working directory. |
| 59 | +It will filter these found projects, the requirements are: |
| 60 | + |
| 61 | +- Having `<OutputType>library</OutputType>` |
| 62 | +- Having a binary, so you need to build your project first before the documentation can be generated. |
| 63 | +- Not having `<IsTestProject>true</IsTestProject>` |
| 64 | +- Having `<GenerateDocumentationFile>true</GenerateDocumentationFile>` |
| 65 | + |
| 66 | +🪄 If you made some changes in order to adhere to the rules, you may want to remove the `.fsdocs/cache` file. |
| 67 | + |
| 68 | +## Adding the missing properties |
| 69 | + |
| 70 | +After our initial `watch` run, you may have noticed that some links aren't working yet. |
| 71 | +`License`, `Releases Notes` and `Source Repository` can be provided by setting MSBuild properties. |
| 72 | + |
| 73 | +You can either add these properties to a single `.fsproj` file, or more typically, add them to a [Directory.Build.props](https://learn.microsoft.com/visualstudio/msbuild/customize-by-directory) file. |
| 74 | +The simplest `Directory.Build.props` file: |
| 75 | + |
| 76 | +```xml |
| 77 | +<Project> |
| 78 | + <PropertyGroup> |
| 79 | + <RepositoryUrl>https://github.com/fsprojects/FSharp.AWS.DynamoDB</RepositoryUrl> |
| 80 | + <FsDocsLicenseLink>https://github.com/fsprojects/FSharp.AWS.DynamoDB/blob/master/License.md</FsDocsLicenseLink> |
| 81 | + <FsDocsReleaseNotesLink>https://github.com/fsprojects/FSharp.AWS.DynamoDB/blob/master/RELEASE_NOTES.md</FsDocsReleaseNotesLink> |
| 82 | + <PackageProjectUrl>https://fsprojects.github.io/FSharp.AWS.DynamoDB</PackageProjectUrl> |
| 83 | + </PropertyGroup> |
| 84 | +</Project> |
| 85 | +``` |
| 86 | + |
| 87 | +🪄 If you don't have any release notes yet, you could consider using [Ionide.KeepAChangelog](https://github.com/ionide/KeepAChangelog). |
| 88 | + |
| 89 | +Running `dotnet fsdocs watch` will now yield: |
| 90 | + |
| 91 | +``` |
| 92 | + root --> https://github.com/fsprojects/FSharp.AWS.DynamoDB/ |
| 93 | + ... |
| 94 | + fsdocs-license-link --> https://github.com/fsprojects/FSharp.AWS.DynamoDB/blob/master/License.md |
| 95 | + fsdocs-release-notes-link --> https://github.com/fsprojects/FSharp.AWS.DynamoDB/blob/master/RELEASE_NOTES.md |
| 96 | + ... |
| 97 | + fsdocs-repository-link --> https://github.com/fsprojects/FSharp.AWS.DynamoDB/ |
| 98 | +``` |
| 99 | + |
| 100 | +⚠️ Again, you might need to remove `.fsdocs/cache` in order for changes to be picked up! |
| 101 | + |
| 102 | +`<PackageProjectUrl>` is actually a very important property when you run `dotnet fsdocs build`. |
| 103 | +`build` will generate static files for the targeted production environment. In our case, this will be GitHub Pages. |
| 104 | + |
| 105 | +Pages will host your files from https://github.com/user/project on `https://user.github.io/project/` by default. |
| 106 | +You can change this by adding a custom domain so we need to be sure that all links and urls will be generated correctly during a build. |
| 107 | + |
| 108 | +Let's now run `dotnet fsdocs build`. |
| 109 | + |
| 110 | +`<PackageProjectUrl>` will replace the `{{root}}` substitution, which is used all over the place in the default template. |
| 111 | + |
| 112 | +⚠️ You want to ensure that the static files in the `output` folder (after running the build) have the correct links. |
| 113 | + |
| 114 | +## Ignore generated files |
| 115 | + |
| 116 | +Alright, at this point we've made a lot of progress. If you are using `git` you want to add the following to your [.gitignore](https://git-scm.com/docs/gitignore) file. |
| 117 | + |
| 118 | +```.ignorelang |
| 119 | +# FSharp.Formatting |
| 120 | +.fsdocs/ |
| 121 | +output/ |
| 122 | +tmp/ |
| 123 | +``` |
| 124 | + |
| 125 | +## Ship it! |
| 126 | + |
| 127 | +Once we are satisfied with our documentation, we want to publish it to GitHub Pages. |
| 128 | +We can use [GitHub Actions](https://github.com/features/actions) to deploy our website. |
| 129 | + |
| 130 | +Deploy to Pages from GitHub Actions must be enabled in the repository settings: |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +The typical flow is to publish your documentation after a release or after new commits were added to the default branch. |
| 135 | +Let's create a very basic Action that will deploy our website after pushing to main: |
| 136 | + |
| 137 | +Create a file `.github/workflows/docs.yml`: |
| 138 | + |
| 139 | +```yml |
| 140 | +name: Docs |
| 141 | + |
| 142 | +# Trigger this Action when new code is pushed to the main branch |
| 143 | +on: |
| 144 | + push: |
| 145 | + branches: |
| 146 | + - main |
| 147 | + |
| 148 | +# We need some permissions to publish to Github Pages |
| 149 | +permissions: |
| 150 | + contents: write |
| 151 | + pages: write |
| 152 | + id-token: write |
| 153 | + |
| 154 | +jobs: |
| 155 | + build: |
| 156 | + runs-on: ubuntu-latest |
| 157 | + steps: |
| 158 | + # Checkout the source code |
| 159 | + - uses: actions/checkout@v4 |
| 160 | + # Setup dotnet, please use a global.json to ensure the right SDK is used! |
| 161 | + - name: Setup .NET |
| 162 | + uses: actions/setup-dotnet@v3 |
| 163 | + # Restore the local tools |
| 164 | + - name: Restore tools |
| 165 | + run: dotnet tool restore |
| 166 | + # Build the code for the API documentation |
| 167 | + - name: Build code |
| 168 | + run: dotnet build -c Release YourSolution.sln |
| 169 | + # Generate the documentation files |
| 170 | + - name: Generate the documentation' |
| 171 | + run: dotnet fsdocs build --properties Configuration=Release |
| 172 | + # Upload the static files |
| 173 | + - name: Upload documentation |
| 174 | + uses: actions/upload-pages-artifact@v2 |
| 175 | + with: |
| 176 | + path: ./output |
| 177 | + |
| 178 | + # GitHub Actions recommends deploying in a separate job. |
| 179 | + deploy: |
| 180 | + runs-on: ubuntu-latest |
| 181 | + needs: build |
| 182 | + steps: |
| 183 | + - name: Deploy to GitHub Pages |
| 184 | + id: deployment |
| 185 | + uses: actions/deploy-pages@v2 |
| 186 | +``` |
| 187 | +
|
| 188 | +⚠️ Careful yaml is indentation sensitive! |
| 189 | +
|
| 190 | +## Next steps |
| 191 | +
|
| 192 | +Mission accomplished, right? If everything went well, you should have a published website at this point! |
| 193 | +Here are some next steps you could consider: |
| 194 | +
|
| 195 | +### Use fsx file in your documentation |
| 196 | +
|
| 197 | +Create documentation using [Literate Scripts](../literate.html). A typical flow here is that you load your locate project binary into a script and create examples using the latest code: |
| 198 | +
|
| 199 | +```fsharp |
| 200 | +#r "../src/Project1/bin/Debug/net6.0/Project1.dll" |
| 201 | + |
| 202 | +open Project1 |
| 203 | + |
| 204 | +// Actual consumption of your project! |
| 205 | +let result = SomeType.SomeMethod("foo") |
| 206 | +``` |
| 207 | + |
| 208 | +When using the `--strict` flag in `dotnet fsdocs build`, your documentation generation will fail if your script contains errors. |
| 209 | +This is useful to ensure your documentation is always in sync with your latest public API! |
| 210 | + |
| 211 | +### Automatically update to newer versions of fsdocs-tool |
| 212 | + |
| 213 | +Using [Dependabot](https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/about-dependabot-version-updates) you can easily receive new PR's with updates to your `dotnet` dependencies. |
| 214 | + |
| 215 | +Create a `github/dependabot.yml` file with: |
| 216 | + |
| 217 | +```yml |
| 218 | +version: 2 |
| 219 | +updates: |
| 220 | + # Update to newer version of GitHub Actions |
| 221 | + - package-ecosystem: "github-actions" |
| 222 | + directory: "/" |
| 223 | + schedule: |
| 224 | + interval: "weekly" |
| 225 | + |
| 226 | + # Update to newer NuGet dependencies |
| 227 | + - package-ecosystem: "nuget" |
| 228 | + directory: "/" |
| 229 | + schedule: |
| 230 | + interval: "daily" |
| 231 | +``` |
| 232 | +
|
| 233 | +This will automatically create a new PR when there is an update to the `fsdocs` tool. |
| 234 | + |
| 235 | +⚠️ P️lease be very careful, if you have followed along, we don't have any GitHub Actions right now that run against pull requests! |
| 236 | +It is recommended to have an Action that builds your documentation against any incoming changes. |
| 237 | +You typically want to lint code, run unit tests and perform other useful checks as well! |
| 238 | + |
| 239 | +Example Action, `.github/workflows/ci.yml`: |
| 240 | +```yml |
| 241 | +name: CI |
| 242 | +
|
| 243 | +on: [pull_request] |
| 244 | +
|
| 245 | +jobs: |
| 246 | + build: |
| 247 | + runs-on: ubuntu-latest |
| 248 | +
|
| 249 | + steps: |
| 250 | + - uses: actions/checkout@v3 |
| 251 | + - name: Setup .NET Core |
| 252 | + uses: actions/setup-dotnet@v3 |
| 253 | + - name: Restore tools |
| 254 | + run: dotnet tool restore |
| 255 | + - name: Build |
| 256 | + run: dotnet build YourSolution.sln |
| 257 | + - name: Documentation |
| 258 | + run: dotnet fsdocs build |
| 259 | +``` |
| 260 | + |
| 261 | +⚠️ Also never trust any update to `fsdocs` blindly, always check the [release notes](https://github.com/fsprojects/FSharp.Formatting/blob/main/RELEASE_NOTES.md) to see if there are any breaking changes. |
0 commit comments