Skip to content

Commit

Permalink
Merge pull request #61 from derskythe/dotnet
Browse files Browse the repository at this point in the history
Back Sync
  • Loading branch information
derskythe authored Apr 9, 2023
2 parents 054cbae + 204f2b0 commit fef92b8
Show file tree
Hide file tree
Showing 35 changed files with 2,050 additions and 151 deletions.
124 changes: 124 additions & 0 deletions .github/actions/build-docker/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
name: "Action build image"
description: "Number of steps to build and deploy image"
author: "DerSkythe"
branding:
icon: 'layers'
color: 'blue'

inputs:
full_tag:
description: 'full_tag'
required: true
repository_owner:
description: 'repository_owner'
required: true
release_image_name:
description: 'release_image_name'
required: true
base_build_name:
description: 'base_build_name'
required: true
base_build_image:
description: 'base_build_image'
required: true
revision:
description: 'revision'
required: true
tag_prefix:
description: 'tag_prefix'
required: true
dockerfile:
description: 'dockerfile'
required: true
matrix_distro:
description: 'matrix_distro'
required: true
matrix_codename:
description: 'matrix_codename'
required: true
matrix_version:
description: 'matrix_version'
required: true
matrix_docker_codename:
description: 'matrix_docker_codename'
required: true
chown_user:
description: 'chown_user'
required: true
docker_user:
description: 'Secret'
required: true
docker_token:
description: 'Secret'
required: true
gh_token:
description: 'Secret'
required: true
platforms:
description: 'platforms'
required: true
image_list:
description: 'image_list'
required: true
runs:
using: "composite"
steps:
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ inputs.docker_user }}
password: ${{ inputs.docker_token }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ inputs.docker_user }}
password: ${{ inputs.gh_token }}
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
labels: |
org.opencontainers.image.name=${{ inputs.release_image_name }}
org.opencontainers.image.version=${{ inputs.full_tag }}
org.opencontainers.image.base.name=${{ inputs.base_build_image }}
org.opencontainers.image.revision=${{ inputs.revision }}
org.opencontainers.image.licenses=MIT
images: ${{ inputs.image_list }}
flavor: |
latest=false
prefix=
suffix=
tags: |
type=raw,value=${{ inputs.full_tag }},enable=true
type=raw,value=${{ inputs.tag_prefix }},enable=true
- name: Build and push
if: ${{ success() }}
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfiles/${{ inputs.dockerfile }}
provenance: true
sbom: true
pull: true
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
BUILD_IMAGE=${{ inputs.base_build_image }}
CHOWN_USER=${{ inputs.chown_user }}
LSB_RELEASE_CODENAME=${{ inputs.matrix_docker_codename }}
DOCKER_CODENAME=${{ inputs.matrix_docker_codename }}
DISTRO=${{ inputs.matrix_distro }}
DISTRO_VERSION=${{ inputs.matrix_version }}
VERSION=${{ env.DOCKER_METADATA_OUTPUT_VERSION }}
REVISION=${{ inputs.revision }}
platforms: ${{ inputs.platforms }}
cache-from: type=gha
cache-to: type=gha,mode=max
99 changes: 99 additions & 0 deletions .github/actions/process-urls/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
---
name: "Process URL list of dependencies"
description: "Process URL list of dependencies and make env files if necessary"
author: "DerSkythe"
branding:
icon: 'flag'
color: 'yellow'

inputs:
config_file:
description: 'Path to JSON'
required: true
output_prefix:
description: 'Directory to save env files'
required: true
platforms:
description: 'List of required platforms'
required: true
gh_version:
description: 'Version of GitHub Action Runner'
required: true
gh_token:
description: 'Secret token of GitHub account'
required: true
outputs:
version:
description: "Latest version of GitHub Action Runner"
value: ${{ steps.prepare-for-output.outputs.version }}
runs:
using: composite
steps:
- name: Copy Repo Files
uses: actions/checkout@v3
with:
ref: ${{ github.ref }}

- name: Check deps for update
id: run-check-for-updates
shell: pwsh
env:
GITHUB_TOKEN: ${{ inputs.gh_token }}
CONFIG_FILE: ${{ inputs.config_file }}
OUTPUT_PREFIX: ${{ inputs.output_prefix }}
PLATFORMS: ${{ inputs.platforms }}
run: |
./.github/helpers/process-urls.ps1 -JsonFile ${{ env.CONFIG_FILE }} -OutputPrefix ${{ env.OUTPUT_PREFIX }} -Platforms ${{ env.PLATFORMS }} | %{ Write-Host $_ Write-Output($_) >> $Env:GITHUB_OUTPUT }
- name: Commit if updated
id: commit-if-updated
if: ${{ success() && steps.run-check-for-updates.outputs.files_changed == 1 }}
env:
GITHUB_TOKEN: ${{ inputs.gh_token }}
NEW_VERSION: ${{ steps.run-check-for-updates.outputs.gh_runner_new_version }}
LOG_VERSION: ${{ steps.run-check-for-updates.outputs.log_version }}
FILES_CHANGED: ${{ steps.run-check-for-updates.outputs.files_changed }}
shell: pwsh
run: |
$CurrentBranch = (git branch --no-color --no-column --show-current)
git pull
$Listed = (git branch -ar --no-color --no-column --list '*update/deps-*')
Write-Host $Listed
$LastBranchNumber = $Listed | % {($_ -replace '[^\d]', '') -as [int]} | Sort-Object -Descending | Select-Object -Index 0
Write-Host $LastBranchNumber
$LastBranchNumber = $LastBranchNumber -gt 0 ? $LastBranchNumber + 1 : 1
Write-Host $LastBranchNumber
git config user.name github-actions
git config user.email [email protected]
$BranchName = "update/deps-$($LastBranchNumber)"
git checkout -b $BranchName
git add .
$Body = "list of updates:`n"
$Body = $Body + ('{0}' -f ($env:LOG_VERSION -replace ';', "`n")) -replace "'", ''
Write-Output($Body) | Out-File pr.txt
git commit --file=pr.txt
git push --set-upstream origin $BranchName
if ($?) {
$BranchName = ('{0}' -f $BranchName)
$CurrentBranch = ('{0}' -f $CurrentBranch)
gh pr create --head "$BranchName" --base "$CurrentBranch" --label 'ci/cd' --title "Update deps" -F pr.txt --repo ${{ github.repository }}
}
Remove-Item -Force pr.txt
# $DefaultBranch=(gh repo view --json defaultBranchRef --jq .defaultBranchRef.name)
# Write-Host ('CurrentBranch: {0}, DefaultBranch: {1}' -f $CurrentBranch, $DefaultBranch)
# gh api --method POST -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" '/repos/${{ github.github.repository }}/pulls' `
# -f title='Update deps' -f body='Please pull these awesome changes in!' `
# -f head="${{ github.repository_owner }}:$BranchName" -f head_repo="${{ github.repository_owner }}" `
# -f base="$CurrentBranch"

- name: Dependencies was updated
id: prepare-for-output
if: ${{ always() && steps.run-check-for-updates.outputs.files_changed == 1 }}
env:
GITHUB_TOKEN: ${{ inputs.gh_token }}
NEW_VERSION: ${{ steps.run-check-for-updates.outputs.gh_runner_new_version }}
CURRENT_VERSION: ${{ inputs.gh_version }}
shell: pwsh
run: |
$NewVersion = (([string]::IsNullOrWhitespace($env:NEW_VERSION)) ? $env:CURRENT_VERSION : $env:NEW_VERSION)
Write-Output "version=$NewVersion" >> $env:GITHUB_OUTPUT
173 changes: 173 additions & 0 deletions .github/helpers/github-latest-release.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
param(
# ex.:https://github.com/cli/cli/releases or https://github.com/cli/cli/releases/latest
[Parameter (Mandatory = $true)]
[Alias ("Uri")]
[string]$Url,

# tar.gz, zip
[Parameter (Mandatory = $true)]
[Alias ("Extension")]
[string]$FileType,

# ex.: amd64,arm64
[Parameter ()]
[Alias ("Arch")]
[string[]]$Platforms,

[Parameter ()]
[Alias ("OutputFormat")]
[PSDefaultValue (Value = $true)]
[switch]$ForActions,

# if we set platforms like amd64,arm64 we need to set here x64,arm64
[Parameter ()]
[Alias ("Alternative")]
[string[]]$AnotherName,

# linux,windows
[Parameter ()]
[PSDefaultValue (Value = 'linux')]
[Alias ("TargetOs")]
[string]$Os,

[Parameter ()]
[Alias ("IsDebug")]
[PSDefaultValue (Value)]
[switch]$DebugMessages
)
# test data:
# ./get-latest-release.ps1 https://github.com/actions/runner/releases 'tar.gz' -Platforms amd64,arm64 -Alternative x64,arm64
# ./get-latest-release.ps1 https://github.com/cli/cli/releases 'tar.gz' -Platforms amd64,arm64 -ForActions -IsDebug
########################################
Set-StrictMode -Version 3.0 #
$ErrorActionPreference = "Stop" #
########################################

# Execution in GitHub Actions gives
# The variable '$LASTEXITCODE' cannot be retrieved because it has not been set
$LASTEXITCODE = 0
$ConstApiMime = "Accept: application/vnd.github+json"
$ConstApiVersion = "X-GitHub-Api-Version: 2022-11-28"

if ( [string]::IsNullOrWhiteSpace($Os)) {
$Os = 'linux'
}
else {
$Os = $Os.ToLowerInvariant()
}

if ([string]::IsNullOrWhiteSpace($Url)) {
Write-Error "Empty URL"
exit 1
}

function DebugMessage {
param(
[string[]]$Url
)
if (! $DebugMessages) {
return
}

Write-Host ($Url -join ', ') -ForegroundColor Blue
}

function ErrorMessage {
param(
[string[]]$Url
)

Write-Host ($Url -join ', ') -ForegroundColor Red
}

$ApiUrl = $Url -replace '^https\:\/\/github\.com\/([^\/]+)\/([^\/]+).*', '/repos/$1/$2'

DebugMessage $ApiUrl

$Release = (gh api -H $ConstApiMime -H $ConstApiVersion "$( $ApiUrl )/releases/latest" | ConvertFrom-Json)
if (($LASTEXITCODE -ne 0) -or ($false -eq $?) -or ($null -eq $Release) -or ([string]::IsNullOrWhiteSpace($Release.tag_name))) {
Write-Error "Invalid URL: $( $Url )"
ErrorMessage "Invalid URL: $( $Url )"
exit 1
}

$LatestReleaseVersion = $Release.tag_name -replace '^\D+(\d+\.\d+\.\d+).*', '$1'
DebugMessage $LatestReleaseVersion
$Like = "*$( $Os )*$( $FileType )"

$DownloadUrls = ($Release.assets | Where-Object browser_download_url -like $Like).browser_download_url
$Output = @{
LATEST_RELEASE_VERSION = $LatestReleaseVersion
}

if (($null -eq $AnotherName) -or ($AnotherName.Count -eq 0)) {
foreach ($Platform in $Platforms) {
$Found = $false

foreach ($Url in $DownloadUrls) {
if ($Url -match $Platform) {
$Found = $true
$Output.Add($Platform, $Url)
DebugMessage $Platform, $Url
break
}
}

if (! $Found) {
$Output.$Platform = ''
DebugMessage $Platform, 'NONE!'
}
}
}
else {
[bool]$NotAlpine = $Url.Contains('PowerShell')
for ($Index = 0; $Index -lt $Platforms.Count; $Index++) {
$AltPlatform = $AnotherName[$Index]
DebugMessage 'AnotherNames', $AltPlatform
$Platform = $Platforms[$Index]

$Found = $false
foreach ($Url in $DownloadUrls) {
# Alpine was added for pwsh
if ($NotAlpine) {
if (($Url.Contains('fxdependent')) -or ($Url.Contains('alpine'))) {
continue
}
if (($Url -match $AltPlatform)) {
$Found = $true
$Output.Add($Platform, $Url)
DebugMessage $Platform, $Url
break
}
}
else {
if (($Url -match $AltPlatform)) {
$Found = $true
$Output.Add($Platform, $Url)
DebugMessage $Platform, $Url
break
}
}
}

if (! $Found) {
$Output.$Platform = ''
DebugMessage $Platform
, 'NONE!'
}
}
}


if ($ForActions) {
$Plain = New-Object -TypeName "System.Text.StringBuilder"
$Output.GetEnumerator() | ForEach-Object {
[void]$Plain.Append($_.Key.ToUpperInvariant())
[void]$Plain.Append('=')
[void]$Plain.AppendLine($_.Value)
}
Write-Output $Plain.ToString()
}
else {
$Output
}
Loading

0 comments on commit fef92b8

Please sign in to comment.