Manual build and release #31
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: Manual build and release | |
on: | |
workflow_dispatch: | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
outputs: | |
version: ${{ steps.read_version.outputs.version }} | |
is_prerelease: ${{ steps.check_prerelease.outputs.is_prerelease }} | |
zip_name: ${{ steps.zip_project.outputs.zip_name }} | |
release_needed: ${{ steps.compare_versions.outputs.release_needed }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '20' | |
- name: Install dependencies and Build | |
run: | | |
npm install | |
npm run build | |
- name: Read version and project name from package.json | |
id: read_version | |
run: | | |
VERSION=$(jq -r '.version' < package.json) | |
PROJECT_NAME=$(jq -r '.name' < package.json) | |
echo "Version is $VERSION" | |
echo "Project name is $PROJECT_NAME" | |
echo "version=$VERSION" >> $GITHUB_OUTPUT | |
echo "project_name=$PROJECT_NAME" >> $GITHUB_OUTPUT | |
- name: Check if version is a pre-release | |
id: check_prerelease | |
run: | | |
VERSION="${{ steps.read_version.outputs.version }}" | |
if [[ "$VERSION" == *"beta"* || "$VERSION" == *"alpha"* || "$VERSION" == *"rc"* ]]; then | |
echo "This is a pre-release" | |
echo "is_prerelease=true" >> $GITHUB_OUTPUT | |
else | |
echo "This is not a pre-release" | |
echo "is_prerelease=false" >> $GITHUB_OUTPUT | |
fi | |
- name: Fetch latest tag | |
id: fetch_latest_tag | |
run: | | |
if git rev-list --tags --max-count=1 >/dev/null 2>&1; then | |
LATEST_TAG=$(git describe --tags "$(git rev-list --tags --max-count=1)") | |
else | |
LATEST_TAG="none" | |
fi | |
echo "Latest tag: $LATEST_TAG" | |
echo "LATEST_TAG=$LATEST_TAG" >> $GITHUB_ENV | |
- name: Compare versions and decide if a new tag is needed | |
id: compare_versions | |
run: | | |
VERSION="${{ steps.read_version.outputs.version }}" | |
LATEST_TAG="$LATEST_TAG" # Retrieved from GITHUB_ENV | |
if [ "$LATEST_TAG" = "none" ]; then | |
echo "No previous tag found. Proceeding with new tag and release." | |
echo "release_needed=true" >> $GITHUB_OUTPUT | |
elif [ "$(printf '%s\n' "$VERSION" "$LATEST_TAG" | sort -V | head -n1)" != "$VERSION" ]; then | |
echo "New version detected. Proceeding with new tag and release." | |
echo "release_needed=true" >> $GITHUB_OUTPUT | |
else | |
echo "No new version. Skipping release." | |
echo "release_needed=false" >> $GITHUB_OUTPUT | |
fi | |
- name: Zip the contents of the dist directory with project name and version | |
id: zip_project | |
run: | | |
ZIP_NAME="${{ steps.read_version.outputs.project_name }}-${{ steps.read_version.outputs.version }}.zip" | |
cd dist | |
zip -r "../$ZIP_NAME" . | |
cd .. | |
echo "zip_name=$ZIP_NAME" >> $GITHUB_OUTPUT | |
- name: Upload the zip file as an artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: dist-zip | |
path: ${{ steps.zip_project.outputs.zip_name }} | |
release: | |
runs-on: ubuntu-latest | |
needs: build | |
if: needs.build.outputs.release_needed == 'true' | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Download dist.zip artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: dist-zip | |
path: ./ | |
- name: Create new Git tag | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
git tag "${{ needs.build.outputs.version }}" | |
git push origin "${{ needs.build.outputs.version }}" | |
- name: Create GitHub Release and upload compressed file | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
echo "Uploading ${{ needs.build.outputs.zip_name }} to GitHub release..." | |
PRERELEASE_FLAG="" | |
if [ "${{ needs.build.outputs.is_prerelease }}" == "true" ]; then | |
PRERELEASE_FLAG="--prerelease" | |
fi | |
gh release create "${{ needs.build.outputs.version }}" "${{ needs.build.outputs.zip_name }}" \ | |
--title "${{ needs.build.outputs.version }}" \ | |
--notes "Automated release for version ${{ needs.build.outputs.version }}" \ | |
$PRERELEASE_FLAG |