Add CI/CD pipeline #11
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: LeanCode.AppReview Build & Publish | |
on: | |
push: | |
branches: [ main ] | |
tags-ignore: [ "*-v*" ] | |
paths: | |
- "backend/**" | |
pull_request: | |
branches: [ main ] | |
paths: | |
- "backend/**" | |
workflow_dispatch: | |
env: | |
DOTNET_VERSION: 8.0.100 | |
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1 | |
DOTNET_CLI_TELEMETRY_OPTOUT: 1 | |
jobs: | |
event_file: | |
name: "Event File" | |
runs-on: ubuntu-latest | |
steps: | |
- name: Upload | |
uses: actions/upload-artifact@v3 | |
with: | |
name: Event File | |
path: ${{ github.event_path }} | |
version: | |
name: Version | |
runs-on: ubuntu-latest | |
outputs: | |
version: ${{ steps.version.outputs.version }} | |
publish_artifacts: ${{ steps.version.outputs.publish_artifacts }} | |
publish_nuget: ${{ steps.version.outputs.publish_nuget }} | |
artifacts_name: ${{ steps.version.outputs.artifacts_name }} | |
steps: | |
- name: Version | |
id: version | |
run: | | |
BRANCH=${GITHUB_REF#refs/*/} | |
if [[ $BRANCH == "main" ]] | |
then | |
BUILD_NUMBER=$GITHUB_RUN_NUMBER | |
VERSION="1.0.${BUILD_NUMBER}" | |
PUBLISH_ARTIFACTS=1 | |
ARTIFACTS_NAME="LeanCode.AppRating.$VERSION.zip" | |
else | |
VERSION="0.0.0" | |
PUBLISH_ARTIFACTS=0 | |
ARTIFACTS_NAME="<none>" | |
fi | |
echo Building on "$BRANCH" | |
echo Building version: "$VERSION" | |
echo "Artifacts will be saved as $ARTIFACTS_NAME" | |
if [[ $GITHUB_EVENT_NAME == "workflow_dispatch" ]] | |
then | |
echo "Packages will be published to NuGet" | |
PUBLISH_NUGET=1 | |
else | |
PUBLISH_NUGET=0 | |
fi | |
if [[ $PUBLISH_ARTIFACTS == 0 && $PUBLISH_NUGET == 1 ]] | |
then | |
echo "Only vX.Y branches can be published to NuGet, failing" | |
exit 1 | |
fi | |
echo "version=${VERSION}" >> $GITHUB_OUTPUT | |
echo "publish_artifacts=${PUBLISH_ARTIFACTS}" >> $GITHUB_OUTPUT | |
echo "publish_nuget=${PUBLISH_NUGET}" >> $GITHUB_OUTPUT | |
echo "artifacts_name=${ARTIFACTS_NAME}" >> $GITHUB_OUTPUT | |
checkout: | |
name: Checkout | |
runs-on: ubuntu-latest | |
outputs: | |
version: ${{ steps.version.outputs.version }} | |
publish_artifacts: ${{ steps.version.outputs.publish_artifacts }} | |
publish_nuget: ${{ steps.version.outputs.publish_nuget }} | |
artifacts_name: ${{ steps.version.outputs.artifacts_name }} | |
steps: | |
- uses: actions/checkout@v4 | |
build: | |
name: Build | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
working-directory: backend | |
needs: [version, checkout] | |
permissions: | |
checks: write | |
pull-requests: write | |
env: | |
VERSION: ${{ needs.prepare.outputs.version }} | |
steps: | |
- name: Setup .NET | |
uses: actions/setup-dotnet@v3 | |
with: | |
dotnet-version: ${{ env.DOTNET_VERSION }} | |
- name: Restore | |
run: dotnet restore | |
- name: Check formatting | |
run: dotnet csharpier . --check | |
- name: Build | |
run: dotnet build --configuration Release --no-restore | |
env: | |
GIT_COMMIT: ${{ github.sha }} | |
- name: Pack | |
if: ${{ needs.prepare.outputs.publish_artifacts == '1' }} | |
env: | |
ZIP: ${{ needs.prepare.outputs.artifacts_name }} | |
run: | | |
dotnet pack --no-build -c Release -o "$PWD/packed" | |
zip -j "$ZIP" "$PWD"/packed/*.nupkg | |
- name: Publish artifacts | |
if: ${{ needs.prepare.outputs.publish_artifacts == '1' }} | |
uses: actions/upload-artifact@v3 | |
with: | |
name: ${{ needs.prepare.outputs.artifacts_name }} | |
path: ${{ needs.prepare.outputs.artifacts_name }} | |
publish: | |
runs-on: ubuntu-latest | |
name: Publish to feeds | |
needs: [version, checkout, build] | |
if: ${{ needs.prepare.outputs.publish_artifacts == '1' }} | |
steps: | |
- name: Fetch build | |
id: download | |
uses: actions/download-artifact@v2 | |
with: | |
name: ${{ needs.prepare.outputs.artifacts_name }} | |
- name: Unzip | |
run: | | |
unzip "$ZIP" | |
env: | |
ZIP: ${{ needs.prepare.outputs.artifacts_name }} | |
- name: Push to Feedz | |
run: | | |
find -name '*.nupkg' -exec dotnet nuget push -k "$FEEDZ_API_KEY" -s 'https://f.feedz.io/leancode/public/nuget/index.json' -n '{}' ';' | |
env: | |
FEEDZ_API_KEY: ${{ secrets.FEEDZ_API_KEY }} | |
- name: Create release | |
if: ${{ needs.prepare.outputs.publish_nuget == '1' }} | |
uses: actions/create-release@v1 | |
with: | |
tag_name: ${{ format('v{0}', needs.prepare.outputs.version) }} | |
release_name: ${{ format('Release v{0}', needs.prepare.outputs.version) }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Push to NuGet | |
if: ${{ needs.prepare.outputs.publish_nuget == '1' }} | |
run: | | |
find -name '*.nupkg' -exec dotnet nuget push -k "$NUGET_API_KEY" -s 'https://api.nuget.org/v3/index.json' -n '{}' ';' | |
env: | |
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }} |