Skip to content

Create Release and Publish package #4

Create Release and Publish package

Create Release and Publish package #4

name: Create Release and Publish package
on:
workflow_dispatch:
inputs:
version_tag:
description: "Version Tag (format: major.minor.patch)"
required: true
commit_hash:
description: "Commit hash"
required: true
default: latest
jobs:
create-tag:
runs-on: ubuntu-22.04
steps:
- name: Check workflow run ref
run: |
if [ "$RUN_REF" != "refs/heads/main" ]; then
echo Workflow should only be run on main branch. Workflow aborted.
exit 1
fi
env:
RUN_REF: ${{ github.ref }}
- name: Check Version Tag format
run: |
if [[ ! $VERSION_TAG =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo Version Tag \"$VERSION_TAG\" does not match regular expression \"^[0-9]+\.[0-9]+\.[0-9]+$\". Workflow aborted.
exit 1
fi
env:
VERSION_TAG: ${{ github.event.inputs.version_tag }}
- uses: actions/checkout@v4
with:
fetch-depth: '0'
- name: Check is commit ancestor of main HEAD
if: github.event.inputs.commit_hash != 'latest'
run: |
set +e
git merge-base --is-ancestor "$COMMIT_HASH" HEAD 2> /dev/null
IS_ANCESTOR=$?
set -e
if [ $IS_ANCESTOR -eq 1 ]; then
echo Specified commit hash \"$COMMIT_HASH\" is not ancestor of main HEAD. Workflow aborted.
exit 1
fi
env:
COMMIT_HASH: ${{ github.event.inputs.commit_hash }}
- name: Check is Version Tag exist
run: |
if [ "$(git tag --list v$VERSION_TAG)" ]; then
echo Version Tag \"v$VERSION_TAG\" already exists. Workflow aborted.
exit 1
fi
env:
VERSION_TAG: ${{ github.event.inputs.version_tag }}
- name: Setup Git config
run: |
git config --local user.name "github-actions[bot]"
git config --local user.email "[email protected]"
- name: Tag commit
run: |
if [ "$COMMIT_HASH" = "latest" ]; then
git tag v$VERSION_TAG
else
git tag v$VERSION_TAG $COMMIT_HASH
fi
env:
VERSION_TAG: ${{ github.event.inputs.version_tag }}
COMMIT_HASH: ${{ github.event.inputs.commit_hash }}
- name: Push tag
run: git push --set-upstream origin main v$VERSION_TAG
env:
VERSION_TAG: ${{ github.event.inputs.version_tag }}
create-release-and-publish:
runs-on: ubuntu-22.04
needs: create-tag
steps:
- uses: actions/checkout@v4
with:
fetch-depth: '0'
- name: Switch to commit
if: github.event.inputs.commit_hash != 'latest'
run: git reset $COMMIT_HASH --hard
env:
COMMIT_HASH: ${{ github.event.inputs.commit_hash }}
- name: Check for open Pull requests
if: github.event.inputs.commit_hash == 'latest'
run: |
PULLS=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/$GITHUB_REPOSITORY/pulls)
if [ "$PULLS" != "[]" ]; then
echo Open Pull requests found. Workflow aborted.
exit 1
fi
env:
GH_TOKEN: ${{ github.token }}
- name: Set .csproj version
run: sed -i -E "s/(\s*)<Version>[0-9]+\.[0-9]+\.[0-9]+<\/Version>/\1<Version>$VERSION_TAG<\/Version>/g" ./Moira.ApiClient/Moira.ApiClient.csproj
env:
VERSION_TAG: ${{ github.event.inputs.version_tag }}
- uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Restore packages
run: dotnet restore
- name: Build solution
run: dotnet build Moira.ApiClient.sln --configuration Release --no-restore
- name: Create temp directory
run: mkdir ./.temp/
- name: Pack solution
run: dotnet pack --no-build --configuration Release --output ./.temp/
- name: Create GitHub release
run: gh release create v$VERSION_TAG --title v$VERSION_TAG --verify-tag
env:
GH_TOKEN: ${{ github.token }}
VERSION_TAG: ${{ github.event.inputs.version_tag }}
- name: Push package to NuGet.org
run: dotnet nuget push ./.temp/Moira.ApiClient.$VERSION_TAG.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json
env:
VERSION_TAG: ${{ github.event.inputs.version_tag }}
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}