|
| 1 | +# Deploy to GitHub Pages from Azure DevOps |
| 2 | + |
| 3 | + |
| 4 | +### 1. **Create a GitHub Personal Access Token (PAT)** |
| 5 | +You will need a GitHub PAT with repo permissions to allow Azure DevOps to push to the GitHub Pages branch. |
| 6 | + |
| 7 | +- Go to your GitHub account > **Settings** > **Developer settings** > **Personal Access Tokens**. |
| 8 | +- Click on **Generate new token** and select the required repo permissions. |
| 9 | +- Save the token somewhere secure as it will be used in the Azure DevOps pipeline. |
| 10 | + |
| 11 | +### 2. **Create Azure DevOps Pipeline** |
| 12 | + |
| 13 | +#### a. **Setup DocFX** |
| 14 | +Ensure that you have a `docfx.json` configuration file in your repository. This will define how DocFX generates your documentation. |
| 15 | + |
| 16 | +- Install DocFX locally on your development machine and create the configuration: |
| 17 | + |
| 18 | + ```bash |
| 19 | + docfx init -q |
| 20 | + ``` |
| 21 | + |
| 22 | +This will generate the `docfx.json` file, which you can configure to specify input and output directories, build settings, etc. |
| 23 | + |
| 24 | +#### b. **Azure Pipelines YAML File** |
| 25 | + |
| 26 | +Create an Azure DevOps pipeline YAML file (e.g., `azure-pipelines.yml`) in the root of your project to automate the build and deployment. |
| 27 | + |
| 28 | +Here is a sample pipeline YAML configuration for DocFX: |
| 29 | + |
| 30 | +```yaml |
| 31 | +trigger: |
| 32 | + branches: |
| 33 | + include: |
| 34 | + - main # Trigger on main branch |
| 35 | + |
| 36 | +pool: |
| 37 | + vmImage: 'ubuntu-latest' # Use the latest Ubuntu VM |
| 38 | + |
| 39 | +variables: |
| 40 | + - group: GitHubPAT |
| 41 | + |
| 42 | +steps: |
| 43 | + # Step 1: Install DocFX |
| 44 | + - task: UseDotNet@2 |
| 45 | + inputs: |
| 46 | + packageType: 'sdk' |
| 47 | + version: '8.x' # Ensure you have .NET SDK installed |
| 48 | + installationPath: $(Agent.ToolsDirectory)/dotnet |
| 49 | + |
| 50 | + - script: | |
| 51 | + dotnet tool install -g docfx |
| 52 | + displayName: 'Install DocFX' |
| 53 | +
|
| 54 | + # Step 2: Build the Documentation using DocFX |
| 55 | + - script: | |
| 56 | + docfx build |
| 57 | + displayName: 'Build Documentation' |
| 58 | +
|
| 59 | + # Step 3: Deploy to GitHub Pages |
| 60 | + - task: Bash@3 |
| 61 | + displayName: 'Deploy to GitHub Pages' |
| 62 | + inputs: |
| 63 | + targetType: 'inline' |
| 64 | + script: | |
| 65 | + git config --global user.email "your-email@example.com" |
| 66 | + git config --global user.name "Your Name" |
| 67 | + git clone --branch gh-pages https://$GITHUB_PAT@github.com/$GITHUB_REPO.git out |
| 68 | + rm -rf out/* |
| 69 | + cp -r _site/* out/ |
| 70 | + cd out |
| 71 | + git add --all |
| 72 | + git commit -m "Update documentation" |
| 73 | + git push origin gh-pages |
| 74 | + env: |
| 75 | + GITHUB_PAT: $(GITHUB_PAT) # The GitHub Personal Access Token |
| 76 | + GITHUB_REPO: your-github-username/your-repo-name # Update with your repo details |
| 77 | +``` |
| 78 | +
|
| 79 | +#### c. **Setup Azure Pipeline Variables** |
| 80 | +
|
| 81 | +- Go to **Azure DevOps** > **Pipelines** > **Library**. |
| 82 | +- Create a new pipeline variable group called GitHubPAT. |
| 83 | +- Add a new variable named GITHUB_PAT and set the value to the GitHub Personal Access Token you created earlier. Mark this variable as secret. |
| 84 | +
|
| 85 | +### 3. **Configure GitHub Pages** |
| 86 | +
|
| 87 | +- Go to your GitHub repository. |
| 88 | +- Under **Settings** > **Pages**, set the source branch to gh-pages. |
| 89 | +
|
| 90 | +### 4. **Run the Pipeline** |
| 91 | +
|
| 92 | +- Commit the azure-pipelines.yml file to your repository and trigger the pipeline. |
| 93 | +- Azure DevOps will now: |
| 94 | + 1. Install DocFX. |
| 95 | + 2. Build the documentation. |
| 96 | + 3. Deploy the documentation to the gh-pages branch in GitHub. |
| 97 | +
|
| 98 | +### 5. **Access the Documentation** |
| 99 | +
|
| 100 | +After the pipeline finishes, your documentation will be available in the github-pages deployments. |
0 commit comments