Skip to content

Publish Lykke Infrastructure NuGet Package #1

Publish Lykke Infrastructure NuGet Package

Publish Lykke Infrastructure NuGet Package #1

name: Publish Lykke Infrastructure NuGet Package
# This workflow is agreed on and requirements are documented
# in https://bit.ly/4529Q9Q.
on:
workflow_dispatch:
inputs:
version:
description: 'Version number (e.g. v1.0.0)'
required: true
default: 'v1.0.0'
create_release:
description: 'Create a GitHub release'
required: true
default: 'true'
update_changelog:
description: 'Update CHANGELOG.md'
required: true
default: 'true'
release_name:
description: 'Release name. If empty, will be extracted from CHANGELOG.md or default value [Release ${VERSION}] will be used'
required: false
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
ref: ${{ github.ref }}
# Checking out deployment tools repository from Bitbucket
# todo: these tools are supposed to be used for SNOW project but can be used for LYKKE as well,
# should be adopted and moved to separate repository
- name: Checkout deployment tools
run: |
git clone -v https://${{ secrets.BITBUCKET_USER }}:${{ secrets.BITBUCKET_PASSWORD }}@bitbucket.org/lykke-snow/lykke.snow.deployment.git lykke.snow.deployment
# Parses the version number, removes prefix and stores it in TAG_NUMBERS for later use
- name: Parse version number
run: |
TAG_VERSION=${{ github.event.inputs.version }}
TAG_NUMBERS=${TAG_VERSION#v} # Remove preceding "v" if present
echo "TAG_NUMBERS=$TAG_NUMBERS" >> $GITHUB_ENV
# Injects the new version into CHANGELOG.md by replacing placeholder
- name: Inject new version into changelog file
if: ${{ github.event.inputs.update_changelog == 'true' }}
run: |
FILES=$(find . -type f -name 'CHANGELOG.md')
if [[ -z "$FILES" ]]; then
echo "CHANGELOG.md not found. Failing the workflow."
exit 1
fi
COUNT=$(echo "$FILES" | wc -l)
if [[ $COUNT -ne 1 ]]; then
echo "Found $COUNT instances of CHANGELOG.md. There must be exactly one. Failing the workflow."
exit 1
fi
source ./lykke.snow.deployment/tools/core/changelog.sh
FILE=$(echo "$FILES" | head -n 1)
TBD_REPLACED=$(replace_tbd_placeholder "$FILE" "${{ env.TAG_NUMBERS }}")
if [[ $TBD_REPLACED -eq 0 ]]; then
echo "CHANGELOG.md does not contain a placeholder for the new version. Failing the workflow."
exit 1
fi
# Updates all .csproj files with the new version
- name: Update .csproj files with new version
run: |
FILES=$(find . -type f -name '*.csproj')
if [[ -z "$FILES" ]]; then
echo "No .csproj files found. Failing the workflow."
exit 1
fi
source ./lykke.snow.deployment/tools/core/csproj.sh
for FILE in $FILES; do
VERSION_UPDATED=$(update_csproj_version "$FILE" "${{ env.TAG_NUMBERS }}")
if [[ $VERSION_UPDATED -eq 0 ]]; then
echo "Failed to update version in $FILE. Failing the workflow."
exit 1
fi
done
# Tags the current commit with the new version
- name: Tag version
run: |
git config --global user.name 'GitHub Actions'
git config --global user.email 'actions@github.com'
git add -A
git reset -- lykke.snow.deployment
git commit -m "Version ${{ env.TAG_NUMBERS }}"
git tag -fa ${{ github.event.inputs.version }} -m "${{ github.event.inputs.version }}"
git push origin --tags
git push origin ${{ github.ref }}
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x' # Using .NET 6
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal
- name: Pack
run: dotnet pack --no-build --configuration Release -o out
- name: Push
run: dotnet nuget push out/*.nupkg --api-key ${{ secrets.NUGET_TOKEN }} --source https://api.nuget.org/v3/index.json --skip-duplicate
# Extracts release name and release notes from CHANGELOG.md
- name: Extract release name and release notes
if: ${{ github.event.inputs.create_release == 'true' }}
run: |
CHANGELOG_FILE=$(find . -type f -name 'CHANGELOG.md')
if [[ -z "$CHANGELOG_FILE" ]]; then
echo "CHANGELOG.md not found. Failing the workflow."
exit 1
fi
RELEASE_NAME="${{ github.event.inputs.release_name }}"
if [[ -z "$RELEASE_NAME" ]]; then
echo "Release name not provided. Extracting from CHANGELOG.md"
# Extract the latest title
LATEST_TITLE=$(grep -E '^## [0-9.]+' "$CHANGELOG_FILE" | head -n 1)
# Check if the latest title matches the expected pattern
if [[ ! "$LATEST_TITLE" =~ ^##\ [0-9.]+(\ -\ (.*))?\ \([0-9-]*\)$ ]]; then
echo "Failed to extract release name from CHANGELOG.md."
else
# Extract the release name from the latest title, if present
RELEASE_NAME=${BASH_REMATCH[2]}
fi
fi
if [[ -z "$RELEASE_NAME" ]]; then
RELEASE_NAME="Release ${{ env.TAG_NUMBERS }}"
echo "Defaulting release name to $RELEASE_NAME"
fi
RELEASE_NOTES=$(awk '/^## / { if (found) exit; if (!found) found=1; next } found && NF > 0 { print }' $CHANGELOG_FILE)
if [[ -z "$RELEASE_NOTES" ]]; then
echo "Failed to extract release notes from CHANGELOG.md. Failing the workflow."
exit 1
fi
echo "RELEASE_NAME=$RELEASE_NAME" >> $GITHUB_ENV
echo "$RELEASE_NOTES" > release_notes.txt
echo "RELEASE_NOTES_FILE=release_notes.txt" >> $GITHUB_ENV
# Creates a GitHub release with the release name and release notes from CHANGELOG.md
- name: Create GitHub Release
if: ${{ github.event.inputs.create_release == 'true' }}
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.event.inputs.version }}
release_name: ${{ env.RELEASE_NAME }}
body_path: ${{ env.RELEASE_NOTES_FILE }}
draft: false
prerelease: false
- name: Clean up temporary files
run: rm -f ${{ env.RELEASE_NOTES_FILE }}