diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index ce28395..d83f1f2 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -12,6 +12,7 @@ permissions: actions: read checks: write deployments: write + packages: write jobs: build-and-test: @@ -28,16 +29,20 @@ jobs: - name: Install Dependencies run: npm install - - - name: Check if package.json has changed - id: check + + - name: Check if tag is present + id: tag_check run: | - if [ $(git diff --name-only HEAD^1 HEAD | grep 'package.json' | wc -l) -eq 0 ]; then - echo "No version change detected in package.json." + VERSION=$(jq -r '.version' package.json) + echo "version=${VERSION}" >> $GITHUB_ENV + if git rev-parse "${VERSION}" >/dev/null 2>&1; then + echo "Tag v${VERSION} already exists." + echo "exists=true" >> $GITHUB_ENV else - echo "Version change detected." + echo "Tag v${VERSION} does not exist." + echo "exists=false" >> $GITHUB_ENV fi - + - name: Create npm package run: npm pack @@ -47,39 +52,54 @@ jobs: - name: Checkout release branch uses: actions/checkout@v4 - ref: release - path: './release' + with: + ref: release + path: ./release + fetch-depth: 0 - name: Extract package contents run: | tar -xzf *.tgz --strip-components=1 -C ./release - - name: Set up Git use - run: | - git config --global user.email "github-actions[bot]" - git config user.email "github-actions[bot]@users.noreply.github.com" - - name: Push package contents to a new branch run: | - VERSION=$(jq -r '.version' package.json) + git config --global user.email "github-actions[bot]@users.noreply.github.com" + git config --global user.name "github-actions[bot]" cd ./release git add . - git commit -m "Release ${VERSION}" + git commit -m "Release v${{ env.version }}" + echo "Pushing with message: Release v${{ env.version }}" + git push origin release - name: Generate release notes run: | - PREV_TAG=$(git describe --tags --abbrev=0 HEAD^1) + cd ./release + PREV_TAG=$(git describe --tags --abbrev=0) + echo "${PREV_TAG}" GIT_LOG=$(git log --pretty=format:"%h - %s (%an)" "${PREV_TAG}..HEAD") echo "RELEASE_NOTES<> $GITHUB_ENV echo "${GIT_LOG}" >> $GITHUB_ENV echo "EOF" >> $GITHUB_ENV - + - name: Printing release notes + if: always() run: | - echo ${{ env.RELEASE_NOTES }} - echo ${{ env.GIT_LOG }} - echo ${{ env.RELEASE_NOTES }} - echo v${{ steps.check.outputs.version }} + echo " ${{ env.RELEASE_NOTES }} " + echo v${{ env.version }} + echo ${{ env.exists }} + + - name: Create Release + if: env.exists == 'false' + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ env.version }} + release_name: Release v${{ env.version }} + body: ${{ env.RELEASE_NOTES }} + draft: false + prerelease: false + commitish: release \ No newline at end of file