PowerShell 7.x - قسمت دوازدهم - آشنایی با GitHub Actions و بررسی یک مثال
نویسنده: سیروان عفیفی
تاریخ: ۱۴۰۲/۰۱/۲۴ ۲۲:۲۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
name: Build Application Code
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Install Libraries
uses: pip install -r requirements.txt -t .
test:
runs-on: ubuntu-latest
needs: build
steps:
... ├── .github │ ├── scripts │ └── workflows ├── README.md ├── assets └── deps
name: Update Recent Blog Posts
on:
schedule:
- cron: "0 0 * * 0" # Run once a week at 00:00 (midnight) on Sunday
workflow_dispatch:
jobs:
update_posts:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v3
- name: Run the script for fetching latest blog posts
shell: pwsh
run: |
. ./.github/scripts/Get-Posts.ps1
- name: Commit and Push the changes
uses: mikeal/publish-to-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} Function Get-Posts {
Param (
[Parameter(Mandatory = $false)]
[string]$rssUrl
)
$posts = @()
$feed = [xml](Invoke-WebRequest -Uri $rssUrl).Content
$feed.rss.channel.item | Select-Object -First 3 | ForEach-Object {
$post = [PSCustomObject]@{
Title = $_.title."#cdata-section" ?? $_.title
Link = $_.link
Description = $_.description."#cdata-section" ?? $_.description
PubDate = $_.pubDate
}
$posts += $post
}
$posts
}
Function Get-DntipsPosts {
$assemblyPath = "$(Get-Location)/deps/CodeHollow.FeedReader.dll"
[Reflection.Assembly]::LoadFile($assemblyPath)
$feed = [CodeHollow.FeedReader.FeedReader]::ReadAsync("https://www.dntips.ir/feed/author/%d8%b3%db%8c%d8%b1%d9%88%d8%a7%d9%86%20%d8%b9%d9%81%db%8c%d9%81%db%8c").Result
$posts = @()
$feed.Items | Select-Object -First 3 | ForEach-Object {
$post = [PSCustomObject]@{
Title = $_.Title
Link = $_.Link
Description = $_.Description
PubDate = $_.PublishingDate
}
$posts += $post
}
$posts
}
Function Set-Posts {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[PSCustomObject[]]$posts,
[Parameter(Mandatory = $false)]
[string]$marker = "## Recent Blog Posts - English"
)
Begin {
$readMePath = "./README.md"
$readmeContents = Get-Content -Path $readMePath -Raw
$markdownTable = "| Link | Published At |`n"
$markdownTable += "| --- | --- |`n"
}
Process {
if ($null -eq $_.Title) {
return
}
$date = Get-Date -Date $_.PubDate
$link = "[$($_.Title)]($($_.Link))"
$markdownTable += "| $($link) | $($date.ToString("dd/MM/yy")) |`n"
}
End {
$updatedContent = $readmeContents -replace "$marker\n([\s\S]*?)(?=#| $)", "$marker`n$($markdownTable)`n"
$updatedContent | Set-Content -Path $readMePath
}
}
Function Set-Blogs {
$recentBlogPostsStr = "## Recent blog posts -"
Get-Posts("https://dev.to/feed/sirwanafifi") | Set-Posts -marker "$recentBlogPostsStr dev.to"
Get-Posts("https://sirwan.infohttps://www.dntips.ir/rss.xml") | Set-Posts -marker "$recentBlogPostsStr sirwan.info"
Get-DntipsPosts | Set-Posts -marker "$recentBlogPostsStr dntips.ir"
}
Set-Blogs name: Update Step Component
on:
schedule:
- cron: "0 18 * * *"
workflow_dispatch:
jobs:
update_steps:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v3
- name: Run the script for fetching my latest steps
shell: pwsh
env:
STEPS_URI: ${{ secrets.STEPS_URI }}
run: |
. ./.github/scripts/Get-Steps.ps1
- name: Commit and Push the changes
uses: mikeal/publish-to-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} Function Set-Steps {
Param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[PSObject]$json
)
Write-Host ($json | ConvertTo-Json)
$SvgPath = "$(Get-Location)/assets/step.svg"
$SvgContent = Get-Content -Path $SvgPath -Raw
$TextTags = @"
<tspan id="step-count" font-weight="bold">$([System.String]::Format("{0:n0}", [int]$json.steps))</tspan>
"@
$DatetimeTags = "<text id=""datetime"" x=""800"" y=""72"" font-size=""39"" fill="#99989E"">$($json.date)</text>"
$SvgContent = $SvgContent -Replace '<tspan id="step-count" font-weight="bold">.*?</tspan>', $TextTags
$SvgContent = $SvgContent -Replace '<text id="datetime" x="800" y="72" font-size="39" fill="#99989E">.*?</text>', $DatetimeTags
$SvgContent | Set-Content -Path $SvgPath
}
Function Get-LatestSteps {
Try {
$Uri = $env:STEPS_URI
Write-Host "Uri: $Uri"
$JsonResult = (Invoke-WebRequest -Uri $Uri).Content | ConvertFrom-Json
Write-Host "Steps: $($JsonResult.steps)"
Return $JsonResult
}
Catch {
Return @{
steps = 0
date = Get-Date -Format "yyyy-MM-dd"
}
}
}
Write-Host "Getting latest steps..."
Get-LatestSteps | Set-Steps
Write-Host "Done!"
name: "AutoUpdate"
on:
push:
branches: [ "main" ]
schedule:
- cron: '*/15 * * * *'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Autoupdate something!
run: |
run: ... something ...!
git config --global user.email "name@gmail.com"
git config --global user.name "My Name"
git add -A
git commit -m "Automatic update"
git push - name: Push changes to repo
run: |
git config http.sslVerify false
git config user.name "${{ github.actor }}"
git config user.email "${{ github.actor }}@users.noreply.github.com"
git remote add publisher "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git"
git show-ref
git branch --verbose
git lfs install
git checkout main
git add -A
git commit -m "Automated publish"
git pull --rebase publisher main
git push publisher main