Skip to content

Commit

Permalink
Improve workflows (#81)
Browse files Browse the repository at this point in the history
* Improve workflows

* Fix concurrency

* Make documentation preview notification trigger only once

* Update job permissions and standardize PR context step

Updated job permissions in `documentation-preview.yml` to set `pull-requests` to `write` and `actions` to `read`. Standardized the step name for retrieving PR context by removing single quotes. Added an `id` field (`pr-context`) to the `Get PR context` step. Defined environment variables for the `Get PR context` step, including a comment on best practices for referencing environment variables.
  • Loading branch information
KubaZ2 authored Nov 28, 2024
1 parent bdb0a72 commit 322b6fc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 49 deletions.
20 changes: 11 additions & 9 deletions .github/workflows/build-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,14 @@ jobs:
- name: Test
run: dotnet test -c Release --no-build --verbosity normal

- name: Pack
- name: Pack Packages
run: |
dotnet pack NetCord -c Release --no-build
dotnet pack NetCord.Services -c Release --no-build
dotnet pack Hosting/NetCord.Hosting -c Release --no-build
dotnet pack Hosting/NetCord.Hosting.Services -c Release --no-build
dotnet pack Hosting/NetCord.Hosting.AspNetCore -c Release --no-build
- name: Publish
run: |
dotnet nuget push NetCord/bin/Release/*.nupkg -k ${{ secrets.NUGET_API_KEY }} -n -s https://api.nuget.org/v3/index.json --skip-duplicate
dotnet nuget push NetCord.Services/bin/Release/*.nupkg -k ${{ secrets.NUGET_API_KEY }} -n -s https://api.nuget.org/v3/index.json --skip-duplicate
dotnet nuget push Hosting/NetCord.Hosting/bin/Release/*.nupkg -k ${{ secrets.NUGET_API_KEY }} -n -s https://api.nuget.org/v3/index.json --skip-duplicate
dotnet nuget push Hosting/NetCord.Hosting.Services/bin/Release/*.nupkg -k ${{ secrets.NUGET_API_KEY }} -n -s https://api.nuget.org/v3/index.json --skip-duplicate
dotnet nuget push Hosting/NetCord.Hosting.AspNetCore/bin/Release/*.nupkg -k ${{ secrets.NUGET_API_KEY }} -n -s https://api.nuget.org/v3/index.json --skip-duplicate
- name: Setup Node
uses: actions/setup-node@v4

Expand All @@ -53,6 +45,16 @@ jobs:
npm install
npm run build
- name: Publish Packages
env:
KEY: ${{ secrets.NUGET_API_KEY }}
run: |
dotnet nuget push NetCord/bin/Release/*.nupkg -k $KEY -n -s https://api.nuget.org/v3/index.json --skip-duplicate
dotnet nuget push NetCord.Services/bin/Release/*.nupkg -k $KEY -n -s https://api.nuget.org/v3/index.json --skip-duplicate
dotnet nuget push Hosting/NetCord.Hosting/bin/Release/*.nupkg -k $KEY -n -s https://api.nuget.org/v3/index.json --skip-duplicate
dotnet nuget push Hosting/NetCord.Hosting.Services/bin/Release/*.nupkg -k $KEY -n -s https://api.nuget.org/v3/index.json --skip-duplicate
dotnet nuget push Hosting/NetCord.Hosting.AspNetCore/bin/Release/*.nupkg -k $KEY -n -s https://api.nuget.org/v3/index.json --skip-duplicate
- name: Deploy Documentation
uses: appleboy/[email protected]
with:
Expand Down
13 changes: 7 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
name: Build

on:
push:
branches-ignore:
- stable
- alpha
pull_request:
types: [opened, synchronize]
branches:
- stable
- alpha

concurrency:
group: ${{ github.ref_name }}

jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -32,14 +30,17 @@ jobs:
- name: Test
run: dotnet test -c Release --no-build --verbosity normal

- name: Pack
- name: Pack Packages
run: |
dotnet pack NetCord -c Release --no-build
dotnet pack NetCord.Services -c Release --no-build
dotnet pack Hosting/NetCord.Hosting -c Release --no-build
dotnet pack Hosting/NetCord.Hosting.Services -c Release --no-build
dotnet pack Hosting/NetCord.Hosting.AspNetCore -c Release --no-build
- name: Setup Node
uses: actions/setup-node@v4

- name: Build Documentation
working-directory: Documentation
run: |
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/cleanup-documentation-preview.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
name: Cleanup Documentation Preview

on:
pull_request:
pull_request_target:
types: [closed]
paths:
- 'Documentation/**'

concurrency:
group: ${{ github.ref_name }}
cancel-in-progress: true

jobs:
documentation-preview:
Expand Down
82 changes: 51 additions & 31 deletions .github/workflows/documentation-preview.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
name: Documentation Preview

on:
pull_request:
types: [opened, synchronize]
paths:
- 'Documentation/**'
workflow_run:
workflows: [Build]
types: [completed]

concurrency:
group: ${{ github.event.workflow_run.head_branch }}

jobs:
documentation-preview:
runs-on: ubuntu-latest

permissions:
pull-requests: write
actions: read

steps:
- uses: actions/checkout@v4
- name: Get PR context
id: pr-context
env:
# Token required for GH CLI:
GH_TOKEN: ${{ github.token }}
# Best practice for scripts is to reference via ENV at runtime. Avoid using the expression syntax in the script content directly:
PR_TARGET_REPO: ${{ github.repository }}
# If the PR is from a fork, prefix it with `<owner-login>:`, otherwise only the PR branch name is relevant:
PR_BRANCH: |-
${{
(github.event.workflow_run.head_repository.owner.login != github.event.workflow_run.repository.owner.login)
&& format('{0}:{1}', github.event.workflow_run.head_repository.owner.login, github.event.workflow_run.head_branch)
|| github.event.workflow_run.head_branch
}}
# Query the PR number by repo + branch, then assign to step output:
run: |
gh pr view --repo "${PR_TARGET_REPO}" "${PR_BRANCH}" \
--json 'number' --jq '"number=\(.number)"' \
>> "${GITHUB_OUTPUT}"
- name: Setup .NET
uses: actions/setup-dotnet@v4
- name: Download Documentation
uses: actions/download-artifact@v4
with:
dotnet-version: 8.0.x

- name: Restore dependencies
run: dotnet restore

- name: Build
run: dotnet build -c Release --no-restore --warnaserror

- name: Test
run: dotnet test -c Release --no-build --verbosity normal

- name: Build Documentation
working-directory: Documentation
run: |
dotnet tool install docfx -g --version 2.75.3
npm install
npm run build
name: Documentation Artifacts
github-token: ${{ github.token }}
run-id: ${{ github.event.workflow_run.id }}

- name: Deploy Documentation Preview
uses: appleboy/[email protected]
Expand All @@ -45,18 +52,31 @@ jobs:
port: ${{ secrets.SSH_PORT }}
key: ${{ secrets.SSH_KEY }}
rm: true
source: Documentation/_site
strip_components: 2
target: ~/NetCord/preview/html/${{ github.event.pull_request.number }}
source: .
target: ~/NetCord/preview/html/${{ steps.pr-context.outputs.number }}

- name: Notify Documentation Preview
uses: actions/github-script@v7
env:
PR_NUMBER: ${{ steps.pr-context.outputs.number }}
with:
script: |
const number = context.issue.number;
github.rest.issues.createComment({
const number = process.env.PR_NUMBER;
const { data: comments } = await github.rest.issues.listComments({
issue_number: number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `The documentation preview is available at https://preview.netcord.dev/${number}.`,
})
});
const body = `The documentation preview is available at https://preview.netcord.dev/${number}.`;
const duplicateComment = comments.find(comment => comment.user.id === 41898282 && comment.body === body);
if (!duplicateComment) {
await github.rest.issues.createComment({
issue_number: number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body,
});
}

0 comments on commit 322b6fc

Please sign in to comment.