Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
workflow "Build and Publish" {
on = "push"
resolves = "Docker Publish"
}
action "Shell Lint" {
uses = "actions/bin/shellcheck@master"
args = "entrypoint.sh"
}
action "Test" {
uses = "actions/bin/bats@master"
args = "test/*.bats"
}
action "Docker Lint" {
uses = "docker://replicated/dockerfilelint"
args = ["Dockerfile"]
}
action "Build" {
needs = ["Shell Lint", "Test", "Docker Lint"]
uses = "actions/docker/cli@master"
args = "build -t azure ."
}
action "Docker Tag" {
needs = ["Build"]
uses = "actions/docker/tag@master"
args = "azure github/azure --no-latest"
}
action "Publish Filter" {
needs = ["Build"]
uses = "actions/bin/filter@master"
args = "branch master"
}
action "Docker Login" {
needs = ["Publish Filter"]
uses = "actions/docker/login@master"
secrets = ["DOCKER_USERNAME", "DOCKER_PASSWORD"]
}
action "Docker Publish" {
needs = ["Docker Tag", "Docker Login"]
uses = "actions/docker/cli@master"
args = "push github/azure"
}
function Get-GitHubActionType {
$scanner.Scan("\w+")
}
function Get-GitHubActionTypeName {
$scanner.Skip($WHITESPACE)
$null = $scanner.Scan($QUOTE)
$scanner.ScanUntil("(?=$QUOTE)")
}
function Get-GitHubActionKVP {
param($block)
foreach ($line in $block.split("`r`n").where({$_.length -gt 0})) {
$innerScanner = New-PSStringScanner $line
do {
$key = $innerScanner.Scan("\w+")
if($innerScanner.Check('='+$WHITESPACE+$QUOTE)) {
$null = $innerScanner.Scan('=')
$innerScanner.Skip($WHITESPACE)
$null = $innerScanner.Scan($QUOTE)
$value = $innerScanner.ScanUntil("(?=$QUOTE)")
$null = $innerScanner.Scan($QUOTE)
} else {
$null = $innerScanner.Scan('=')
$innerScanner.Skip($WHITESPACE)
$null = $innerScanner.Scan($LEFTBRACKET)
$value = Invoke-Expression $innerScanner.ScanUntil("(?=$RIGHTBRACKET)")
$null = $innerScanner.Scan($RIGHTBRACKET)
}
if ($key) {
[PSCustomObject][Ordered]@{key = $key; value = $value}
}
} until ($innerScanner.EoS())
}
}
function Get-GitHubActionBlock {
$null = $scanner.Scan("\{")
Get-GitHubActionKVP $scanner.ScanUntil("(?=\})")
$null = $scanner.Scan("\}")
}
function Import-GitHubAction {
param(
[Parameter(Mandatory)]
$Path
)
$target = Get-Content -Path $Path -Raw
$QUOTE = '"'
$WHITESPACE = '\s+'
$LEFTBRACKET = '\['
$RIGHTBRACKET = '\]'
$scanner = New-PSStringScanner $target
do {
[PSCustomObject][Ordered]@{
Type = Get-GitHubActionType
Name = Get-GitHubActionTypeName
Block = Get-GitHubActionBlock
}
} until ($scanner.EoS())
}
@dfinke
Copy link
Author

dfinke commented Apr 1, 2019

PS C:\> Import-GitHubAction .\github.action

Type     Name              Block                                                                                                      
----     ----              -----                                                                                                      
workflow Build and Publish {@{key=on; value=push}, @{key=resolves; value=Docker Publish}}                                             
action   Shell Lint        {@{key=uses; value=actions/bin/shellcheck@master}, @{key=args; value=entrypoint.sh}}                       
action   Test              {@{key=uses; value=actions/bin/bats@master}, @{key=args; value=test/*.bats}}                               
action   Docker Lint       {@{key=uses; value=docker://replicated/dockerfilelint}, @{key=args; value=Dockerfile}}                     
action   Build             {@{key=needs; value=System.Object[]}, @{key=uses; value=actions/docker/cli@master}, @{key=args; value=bu...
action   Docker Tag        {@{key=needs; value=Build}, @{key=uses; value=actions/docker/tag@master}, @{key=args; value=azure github...
action   Publish Filter    {@{key=needs; value=Build}, @{key=uses; value=actions/bin/filter@master}, @{key=args; value=branch master}}
action   Docker Login      {@{key=needs; value=Publish Filter}, @{key=uses; value=actions/docker/login@master}, @{key=secrets; valu...
action   Docker Publish    {@{key=needs; value=System.Object[]}, @{key=uses; value=actions/docker/cli@master}, @{key=args; value=pu...

@dfinke
Copy link
Author

dfinke commented Apr 1, 2019

Unrolled

foreach($record in Import-GitHubAction .\github.action) {
    foreach ($item in $record.Block) {        
        [PSCustomObject][Ordered]@{
            Type=$record.Type
            Name=$record.Name
            Key=$item.key
            Value=$item.value
        }
    }
}
Type     Name              Key      Value                             
----     ----              ---      -----                             
workflow Build and Publish on       push                              
workflow Build and Publish resolves Docker Publish                    
action   Shell Lint        uses     actions/bin/shellcheck@master     
action   Shell Lint        args     entrypoint.sh                     
action   Test              uses     actions/bin/bats@master           
action   Test              args     test/*.bats                       
action   Docker Lint       uses     docker://replicated/dockerfilelint
action   Docker Lint       args     Dockerfile                        
action   Build             needs    {Shell Lint, Test, Docker Lint}   
action   Build             uses     actions/docker/cli@master         
action   Build             args     build -t azure .                  
action   Docker Tag        needs    Build                             
action   Docker Tag        uses     actions/docker/tag@master         
action   Docker Tag        args     azure github/azure --no-latest    
action   Publish Filter    needs    Build                             
action   Publish Filter    uses     actions/bin/filter@master         
action   Publish Filter    args     branch master                     
action   Docker Login      needs    Publish Filter                    
action   Docker Login      uses     actions/docker/login@master       
action   Docker Login      secrets  {DOCKER_USERNAME, DOCKER_PASSWORD}
action   Docker Publish    needs    {Docker Tag, Docker Login}        
action   Docker Publish    uses     actions/docker/cli@master         
action   Docker Publish    args     push github/azure                 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment