示例#1
0
    public void Can_generate_workflow()
    {
        var workflow = new WorkflowBuilder("my-workflow");

        workflow.OnPullRequest()
        .Branches("main", "dev")
        .BranchesIgnore("feature/")
        .Paths("libs/foo**", "build/**")
        .PathsIgnore("docs/")
        .Tags("foo-**")
        .TagsIgnore("release/**");

        workflow.OnPush()
        .Branches("main")
        .Paths("**")
        .Tags("'foo-**'");

        workflow.OnSchedule("'0 5,17 * * *'");

        workflow.OnEvent("release", "published", "created", "edited");

        workflow.OnWorkflowCall()
        .Inputs("username", "'A username passed from the caller workflow'", "'john-doe'", false, WorkflowCallType.String)
        .Outputs("workflow_output1", "'The first job output'", "${{ jobs.my_job.outputs.job_output1 }}")
        .Secrets("access-token", "'A token passed from the caller workflow'", false);

        var job = workflow.AddJob("build")
                  .RunsOn("ubuntu-latest")
                  .WithEnvironment(new Dictionary <string, string>
        {
            { "GITHUB_TOKEN", "${{secrets.GITHUB_TOKEN}}" }
        })
                  .Permissions(packages: Permission.Write)
                  .Concurrency("${{ github.head_ref }}", true);

        job.Checkout();

        job.LogIntoGitHubContainerRegistry();

        job.PrintEnvironment();

        job.AddStep("Push")
        .If("github.event_name == 'push'")
        .Run("./build.ps1 push")
        .ShellPowerShell();

        var actual = workflow.Generate();

        var expected = @"# This was generated by a tool (Logicality GitHub Actions Workflow Builder).
# Edits will be overwritten.

name: my-workflow

on:
  pull_request:
    branches:
    - main
    - dev
    branches-ignore:
    - feature/
    paths:
    - libs/foo**
    - build/**
    paths-ignore:
    - docs/
    tags:
    - foo-**
    tags-ignore:
    - release/**
  push:
    branches:
    - main
    paths:
    - **
    tags:
    - 'foo-**'
  schedule:
    - cron: '0 5,17 * * *'
  release:
    types: [published, created, edited]
  workflow_call:
    inputs:
      username:
        description: 'A username passed from the caller workflow'
        default: 'john-doe'
        required: False
        type: string
    outputs:
      workflow_output1:
        description: 'The first job output'
        value: ${{ jobs.my_job.outputs.job_output1 }}
    secrets:
      access-token:
        description: 'A token passed from the caller workflow'
        required: false

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
    permissions:
      packages: write
    concurrency:
      group: ${{ github.head_ref }}
      cancel-in-progress: true

    steps:

    - name: Checkout
      uses: actions/checkout@v2
      with:
        fetch-depth: 0

    - name: Log into GitHub Container Registry
      run: echo ""${{secrets.GITHUB_TOKEN}}"" | docker login ghcr.io -u ${{github.actor}} --password-stdin

    - name: Print Environment
      run: printenv
      shell: bash

    - name: Push
      if: github.event_name == 'push'
      run: ./build.ps1 push
      shell: pwsh
";

        actual.ShouldBe(expected);
    }