diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml new file mode 100644 index 000000000..1ca8ffd7a --- /dev/null +++ b/.github/workflows/ci-cd.yml @@ -0,0 +1,162 @@ +name: OpenAPI.NET CI/CD Pipeline + +on: [push, pull_request, workflow_dispatch] + +jobs: + ci: + name: Continuous Integration + runs-on: ubuntu-latest + outputs: + latest_version: ${{ steps.tag_generator.outputs.new_version }} + env: + ARTIFACTS_FOLDER: ${{ github.workspace }}/Artifacts + GITHUB_RUN_NUMBER: ${{ github.run_number }} + steps: + - name: Conditionals handler + id: conditionals_handler + shell: pwsh + run: | + $defaultBranch = 'vnext' + $githubRef = "${{ github.ref }}" + $isDefaultBranch = 'false' + if ( $githubRef -like "*$defaultBranch*" ) { + $isDefaultBranch = 'true' + } + Write-Output "::set-output name=is_default_branch::$(echo $isDefaultBranch)" + + - name: Data gatherer + id: data_gatherer + shell: pwsh + run: | + # Get default branch + $repo = 'microsoft/OpenAPI.NET' + $defaultBranch = Invoke-RestMethod -Method GET -Uri https://api.github.com/repos/$repo | Select-Object -ExpandProperty default_branch + Write-Output "::set-output name=default_branch::$(echo $defaultBranch)" + + - name: Checkout repository + id: checkout_repo + uses: actions/checkout@v2 + with: + token: ${{ secrets.GITHUB_TOKEN }} + fetch-depth: 0 + + - if: steps.step_conditionals_handler.outputs.is_default_branch == 'true' + name: Bump GH tag + id: tag_generator + uses: mathieudutour/github-tag-action@v5.4 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + default_bump: false + release_branches: ${{ steps.data_gatherer.outputs.default_branch }} + + - name: Build projects + id: build_projects + shell: pwsh + run: | + $newVersion = "${{ steps.tag_generator.outputs.new_version }}" + $projectsArray = @( + '.\src\Microsoft.OpenApi\Microsoft.OpenApi.csproj', + '.\src\Microsoft.OpenApi.Readers\Microsoft.OpenApi.Readers.csproj', + '.\src\Microsoft.OpenApi.Tool\Microsoft.OpenApi.Tool.csproj' + ) + + if ($newVersion) { + $projectsArray | ForEach-Object { + dotnet build $PSItem ` + -c Release ` + -o $env:ARTIFACTS_FOLDER ` + /p:Version=$newVersion + } + } + else { + $projectsArray | ForEach-Object { + dotnet build $PSItem ` + -c Release ` + -o $env:ARTIFACTS_FOLDER + } + } + + # Move NuGet packages to separate folder for pipeline convenience + New-Item Artifacts/NuGet -ItemType Directory + Get-ChildItem Artifacts/*.nupkg | Move-Item -Destination "Artifacts/NuGet" + + - name: Run unit tests + id: run_unit_tests + shell: pwsh + run: | + $testProjectsArray = @( + '.\test\Microsoft.OpenApi.Tests\Microsoft.OpenApi.Tests.csproj', + '.\test\Microsoft.OpenApi.Readers.Tests\Microsoft.OpenApi.Readers.Tests.csproj', + '.\test\Microsoft.OpenApi.SmokeTests\Microsoft.OpenApi.SmokeTests.csproj' + ) + + $testProjectsArray | ForEach-Object { + dotnet test $PSItem ` + -c Release + } + + - name: Upload NuGet packages as artifacts + id: ul_packages_artifact + uses: actions/upload-artifact@v1 + with: + name: NuGet packages + path: Artifacts/NuGet/ + + cd: + if: github.ref == 'refs/heads/vnext' && needs.ci.outputs.latest_version != '' + name: Continuous Deployment + needs: ci + runs-on: ubuntu-latest + steps: + - name: Download and extract NuGet packages + id: dl_packages_artifact + uses: actions/download-artifact@v2 + with: + name: NuGet packages + path: NuGet/ + + - name: Push NuGet packages to NuGet.org + id: push_nuget_packages + continue-on-error: true + shell: pwsh + run: | + Get-ChildItem NuGet/*.nupkg | ForEach-Object { + nuget push $PSItem ` + -ApiKey $env:NUGET_API_KEY ` + -Source https://api.nuget.org/v3/index.json + } + env: + NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }} + + - name: Create and publish release + id: create_release + uses: actions/create-release@v1 + with: + tag_name: v${{ needs.ci.outputs.latest_version }} + release_name: OpenApi v${{ needs.ci.outputs.latest_version }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Upload OpenAPI package as release asset + id: upload_openapi_pckg + uses: actions/upload-release-asset@v1 + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: NuGet/Microsoft.OpenApi.${{ needs.ci.outputs.latest_version }}.nupkg + asset_name: Microsoft.OpenApi.${{ needs.ci.outputs.latest_version }}.nupkg + asset_content_type: application/zip + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Upload OpenAPI.Readers package as release asset + id: upload_openapi_readers_pckg + uses: actions/upload-release-asset@v1 + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: NuGet/Microsoft.OpenApi.Readers.${{ needs.ci.outputs.latest_version }}.nupkg + asset_name: Microsoft.OpenApi.Readers.${{ needs.ci.outputs.latest_version }}.nupkg + asset_content_type: application/zip + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + +# Built with ❤ by [Pipeline Foundation](https://pipeline.foundation) \ No newline at end of file