项目作者: samsmithnz

项目描述 :
Convert Azure Pipelines YAML to GitHub Actions YAML
高级语言: C#
项目地址: git://github.com/samsmithnz/AzurePipelinesToGitHubActionsConverter.git
创建时间: 2019-10-08T01:28:07Z
项目社区:https://github.com/samsmithnz/AzurePipelinesToGitHubActionsConverter

开源协议:MIT License

下载


Convert Azure Pipelines YAML to GitHub Actions YAML

A project to create a conversion tool to make migrations between Azure Pipelines YAML and GitHub Actions YAML easier. As GitHub Actions becomes more popular, it’s clear that a migration tool will be useful to move workloads to GitHub.

Current build
Coverage Status
Latest NuGet package
Current Release

How to use

We built a website that uses this module at: https://pipelinestoactions.azurewebsites.net/.
You can also use the NuGet package

Example:

The Azure Pipelines YAML to build a dotnet application on ubuntu:

  1. trigger:
  2. - main
  3. variables:
  4. buildConfiguration: Release
  5. jobs:
  6. - job: Build
  7. displayName: Build job
  8. pool:
  9. vmImage: ubuntu-latest
  10. steps:
  11. - script: dotnet build WebApplication1/WebApplication1.Service/WebApplication1.Service.csproj --configuration $(buildConfiguration)
  12. displayName: dotnet build webapp

In GitHub Actions:

  1. on:
  2. push:
  3. branches:
  4. - main
  5. env:
  6. buildConfiguration: Release
  7. jobs:
  8. Build:
  9. name: Build job
  10. runs-on: ubuntu-latest
  11. steps:
  12. - uses: actions/checkout@v1
  13. - name: dotnet build webapp
  14. run: dotnet build WebApplication1/WebApplication1.Service/WebApplication1.Service.csproj --configuration ${{ env.buildConfiguration }}";

Current builds running

The table below shows current, functioning YML files that run on a regular schedule to regression test the YAML produced
| Language | Azure Pipelines | GitHub Actions |
| — | — | — |
| .NET Core WebAPI | .NET Core WebAPI | .NET Core WebAPI |
| .NET Framework Desktop | .NET Framework Desktop | .NET Framework Desktop |
| Ant | Ant | Ant |
| Maven | Maven | Maven |
| NodeJS | NodeJS | Node.js |
| Python | Python | Python |
| Ruby | Ruby | Ruby |

How this works

Currently this only supports one-way migrations from Azure Pipelines to GitHub Actions. There are functions to deserialize Azure Pipelines, and serialize and deserialize GitHub Actions. While this is translating many steps, there are nearly infinite combinations, therefore most of the focus has been supporting the basic .NET pipelines. Even if steps can’t convert, the pipeline should. Check the issues for incomplete items, or the TODO’s in the source code.

Yaml can be challenging. The yaml wikipedia page does a very good job of laying out the rules, but when we are talking about converting yaml to C#, there are a few things to know:

  1. Use a good editor - Visual Studio Code has a decent YAML extension (https://marketplace.visualstudio.com/items?itemName=docsmsft.docs-yaml), or if using Visual Studio, enable spaces with CTRL+R,CTRL+W. The GitHub and Azure DevOps in-browser editors are decent too.
  2. String arrays (string[]) are useful for lists (e.g -job). Note both of the following pieces of code for triggers are effectively the same (although the YamlDotNet serializer does not currently support the single line ‘sequence flow’ format)
    ```YAML
    trigger: [main,develop]

trigger:

  • main
  • develop
    ```
  1. The dictonary object (dictonary) is useful for dynamic key value pairs, for example, variables
    1. variables:
    2. MY_VAR: 'my value'
    3. ANOTHER_VAR: 'another value'
    The C# definition for a dictonary looks like:
    1. Dictionary<string, string> variables = new Dictionary<string, string>
    2. {
    3. { "MY_VAR", "my value" },
    4. { "ANOTHER_VAR", "another value" }
    5. };
  2. Just about everything else can be a string or object. Here is an example of a simple job:
    ```YAML
  • job: Build
    displayName: ‘Build job’
    pool:
    vmImage: ubuntu-latest
    1. ```C#
    2. public string job { get; set; }
    3. public string displayName { get; set; }
    4. public Pool pool { get; set; }
  1. Yaml is wack. The white spaces can destroy you, as the errors returned are often not helpful at all. Take lots of breaks. In the end YAML is worth it - I promise!

Current limitations

There are a number of Azure Pipeline features that don’t currently match up well with a GitHub feature, and hence, these migrate with a change in functionality (e.g. parameters become variables and stages become jobs), or not at all (e.g. deployment strategies/environments). As/if these features are added to Actions, we will build in the conversions

Stages

Stages are converted to jobs. For example, a job “JobA” in a stage “Stage1”, becomes a job named “Stage1_JobA”

Azure Pipelines YAML
  1. stages:
  2. - stage: Build
  3. displayName: 'Build Stage'
  4. jobs:
  5. - job: Build
  6. displayName: 'Build job'
  7. pool:
  8. vmImage: windows-latest
  9. steps:
  10. - task: PowerShell@2
  11. inputs:
  12. targetType: 'inline'
  13. script: |
  14. Write-Host "Hello world!"
  15. - job: Build2
  16. displayName: 'Build job 2'
  17. pool:
  18. vmImage: windows-latest
  19. steps:
  20. - task: PowerShell@2
  21. inputs:
  22. targetType: 'inline'
  23. script: |
  24. Write-Host "Hello world 2!"
GitHub Actions YAML
  1. jobs:
  2. Build_Stage_Build:
  3. name: Build job
  4. runs-on: windows-latest
  5. steps:
  6. - uses: actions/checkout@v1
  7. - run:
  8. Write-Host "Hello world!"
  9. shell: powershell
  10. Build_Stage_Build2:
  11. name: Build job 2
  12. runs-on: windows-latest
  13. steps:
  14. - uses: actions/checkout@v1
  15. - run:
  16. Write-Host "Hello world 2!"
  17. shell: powershell

Parameters

Parameters become variables

Azure Pipelines YAML
  1. parameters:
  2. buildConfiguration: 'Release'
  3. buildPlatform: 'Any CPU'
  4. jobs:
  5. - job: Build
  6. displayName: 'Build job'
  7. pool:
  8. vmImage: windows-latest
  9. steps:
  10. - task: PowerShell@2
  11. displayName: 'Test'
  12. inputs:
  13. targetType: inline
  14. script: |
  15. Write-Host "Hello world ${{parameters.buildConfiguration}} ${{parameters.buildPlatform}}"
GitHub Actions YAML
  1. env:
  2. buildConfiguration: Release
  3. buildPlatform: Any CPU
  4. jobs:
  5. Build:
  6. name: Build job
  7. runs-on: windows-latest
  8. steps:
  9. - uses: actions/checkout@v1
  10. - name: Test
  11. run: Write-Host "Hello world ${{ env.buildConfiguration }} ${{ env.buildPlatform }}"
  12. shell: powershell

RunOnce deployment strategy and deployment jobs

The strategy and deployment job is consolidated to a job, with the strategy removed, as there is no GitHub equivalent

Azure Pipelines YAML
  1. jobs:
  2. - deployment: DeployInfrastructure
  3. displayName: Deploy job
  4. environment: Dev
  5. pool:
  6. vmImage: windows-latest
  7. strategy:
  8. runOnce:
  9. deploy:
  10. steps:
  11. - task: PowerShell@2
  12. displayName: 'Test'
  13. inputs:
  14. targetType: inline
  15. script: |
  16. Write-Host ""Hello world""";
GitHub Actions YAML
  1. jobs:
  2. DeployInfrastructure:
  3. name: Deploy job
  4. runs-on: windows-latest
  5. steps:
  6. - name: Test
  7. run: Write-Host ""Hello world""
  8. shell: powershell

Templates

There are no templates in GitHub actions. At this time we are converting the template into an (almost) empty job.

  1. jobs:
  2. - template: azure-pipelines-build-template.yml
  3. parameters:
  4. buildConfiguration: 'Release'
  5. buildPlatform: 'Any CPU'
  6. vmImage: windows-latest
  1. jobs:
  2. job_1_template:
  3. #: 'Note: Azure DevOps template does not have an equivalent in GitHub Actions yet'
  4. steps:
  5. - uses: actions/checkout@v1

Conditions

Conditions are processing with about 98% accuracy. There are some system variables that still need conversions, but we’ve tried to handle the most popular combinations.

System Variables

This is our current table of how we are translating Azure DevOps system variables, to @latest/actions/reference/environment-variables#default-environment-variables">GitHub Environment variables.
| Azure Pipelines | GitHub Actions |
| — | — |
| Build.ArtifactStagingDirectory | github.workspace |
| Build.BuildId | github.run_id |
| Build.BuildNumber | github.run_number |
| Build.SourceBranch | github.ref |
| Build.Repository.Name | github.repository |
| Build.SourcesDirectory | github.workspace |
| Build.StagingDirectory | github.workspace |
| System.DefaultWorkingDirectory | github.workspace |
| Agent.OS | runner.os |

Architecture

The core functionality is contained in a .NET Standard 2.0 class, “AzurePipelinesToGitHubActionsConverter.Core”.

  • In the conversion object, is a public call, “ConvertAzurePipelineToGitHubAction”, to convert Azure DevOps yaml to GitHub Actions yaml:
  • In the same conversion object is a call to convert individual tasks/steps: “ConvertAzurePinelineTaskToGitHubActionTask”
  • The GitHubActionsSerialization object has calls to serialize and deserialize GitHub actions

Testing:

  • There is a .NET CORE 3.1 MSTest project for tests, “AzurePipelinesToGitHubActionsConverter.Tests”

Current projects consuming this:

References

Made with help from https://github.com/aaubry/YamlDotNet and https://en.wikipedia.org/wiki/YAML.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please use a consistent naming convention for your feature branches to identify the work done in the branch. Some suggestions for naming:

  • features/feature-name
  • bugfix/description
  • hotfix/description