xref: /plugin/gitbacked/.github/workflows/build_release.yml (revision 2762023dfb29a64197cb442f664aa321f9f5bc87)
1d32f7577SMarkus Hoffrogge# ------------------------------------------------------------------------------------------
2d32f7577SMarkus Hoffrogge# This is a workflow to release this project as a zipped installable artifact.
3d32f7577SMarkus Hoffrogge# Release version numbering and release notes generation is following standards defined by:
4d32f7577SMarkus Hoffrogge#
5d32f7577SMarkus Hoffrogge#   https://semver.org
6d32f7577SMarkus Hoffrogge#   https://keepachangelog.com
7d32f7577SMarkus Hoffrogge#   https://common-changelog.org
8d32f7577SMarkus Hoffrogge#
9d32f7577SMarkus Hoffrogge# Note: Since DokuWiki is using version numbering in format YYYY-MM-DD we use this numbering
10d32f7577SMarkus Hoffrogge#       format rather than a dotted numbering scheme.
11d32f7577SMarkus Hoffrogge#       The git tag names have to use a 'v' as prefix to the DokuWiki version number.
12d32f7577SMarkus Hoffrogge#
13d32f7577SMarkus Hoffrogge# ------------------------------------------------------------------------------------------
14d32f7577SMarkus Hoffroggename: Build a release
15d32f7577SMarkus Hoffrogge
16d32f7577SMarkus Hoffroggeon:
17d32f7577SMarkus Hoffrogge  # Triggers the workflow on push of a tag filtering the tag to meet
18d32f7577SMarkus Hoffrogge  # semantic version numbering according to https://semver.org
19d32f7577SMarkus Hoffrogge  # Here we use the DokuWiki conform version number pattern.
20d32f7577SMarkus Hoffrogge  push:
21d32f7577SMarkus Hoffrogge    tags:
22d32f7577SMarkus Hoffrogge      ['v[0-9]+-[0-9]+-[0-9]+']
23d32f7577SMarkus Hoffrogge
24d32f7577SMarkus Hoffrogge  # Allows you to run this workflow manually from the Actions tab
25d32f7577SMarkus Hoffrogge  workflow_dispatch:
26d32f7577SMarkus Hoffrogge
27d32f7577SMarkus Hoffroggejobs:
28d32f7577SMarkus Hoffrogge  # Ensure that we run on tag references only
29d32f7577SMarkus Hoffrogge  validate_github_reference:
30d32f7577SMarkus Hoffrogge    name: Validate the tag reference
31d32f7577SMarkus Hoffrogge    # The type of runner that the job will run on
32d32f7577SMarkus Hoffrogge    runs-on: ubuntu-latest
33d32f7577SMarkus Hoffrogge    # Validate tag
34d32f7577SMarkus Hoffrogge    if: ${{ !startsWith(github.ref, 'refs/tags/v') }}
35d32f7577SMarkus Hoffrogge    steps:
36d32f7577SMarkus Hoffrogge      - run: |
37d32f7577SMarkus Hoffrogge          echo "The selected git ref=${{ github.ref }} is NOT a valid release tag. Please select a valid release TAG as reference."
38d32f7577SMarkus Hoffrogge          exit 1
39d32f7577SMarkus Hoffrogge
40d32f7577SMarkus Hoffrogge  # Create a release
41d32f7577SMarkus Hoffrogge  release:
42d32f7577SMarkus Hoffrogge    name: Release
43d32f7577SMarkus Hoffrogge    # The type of runner that the job will run on
44d32f7577SMarkus Hoffrogge    runs-on: ubuntu-latest
45d32f7577SMarkus Hoffrogge    # Set job wide environment
46d32f7577SMarkus Hoffrogge    env:
47d32f7577SMarkus Hoffrogge      APP_NAME: dokuwiki-plugin-gitbacked
48d32f7577SMarkus Hoffrogge      APP_INFO_FILE: plugin.info.txt
49d32f7577SMarkus Hoffrogge      APP_INFO_FILE_VERSION_KEY: date
50d32f7577SMarkus Hoffrogge      BUILD_DIR: build
51*2762023dSMarkus Hoffrogge      ZIP_EXCLUSIONS: '*.git* .editorconfig /*.github/* /*.vscode/* /*build/* /*_test/* RELEASE_HEAD.md'
52d32f7577SMarkus Hoffrogge
53d32f7577SMarkus Hoffrogge    steps:
54d32f7577SMarkus Hoffrogge      # Log use case if triggered manually
55d32f7577SMarkus Hoffrogge      - name: Log use case if triggered manually
56d32f7577SMarkus Hoffrogge        if: ${{ github.event_name == 'workflow_dispatch' }}
57d32f7577SMarkus Hoffrogge        run: |
58d32f7577SMarkus Hoffrogge          echo "Workflow has been triggered manually"
59d32f7577SMarkus Hoffrogge
60d32f7577SMarkus Hoffrogge      # Log use case if triggered by push
61d32f7577SMarkus Hoffrogge      - name: Log use case if triggered by push
62d32f7577SMarkus Hoffrogge        if: ${{ github.event_name == 'push' }}
63d32f7577SMarkus Hoffrogge        run: |
64d32f7577SMarkus Hoffrogge          echo "Workflow has been triggered by push to ${{ github.ref }}"
65d32f7577SMarkus Hoffrogge
66d32f7577SMarkus Hoffrogge      # Check out this repo
67d32f7577SMarkus Hoffrogge      - name: Checkout
684f40c9b4SMarkus Hoffrogge        uses: actions/checkout@v3
69d32f7577SMarkus Hoffrogge
70d32f7577SMarkus Hoffrogge      # Set version tags as global environment properties
71d32f7577SMarkus Hoffrogge      - name: Prepare Version Tags
72d32f7577SMarkus Hoffrogge        run: |
73d32f7577SMarkus Hoffrogge          #echo "MAJOR_VERSION=$(echo ${GITHUB_REF/refs\/tags\//} | awk -F'-' '{print $1}')" >> $GITHUB_ENV
74d32f7577SMarkus Hoffrogge          #echo "MINOR_VERSION=$(echo ${GITHUB_REF/refs\/tags\//} | awk -F'-' '{print $1"-"$2}')" >> $GITHUB_ENV
75d32f7577SMarkus Hoffrogge          #echo "FULL_VERSION=$(echo ${GITHUB_REF/refs\/tags\//} | awk -F'-' '{print $1"-"$2"-"$3}')" >> $GITHUB_ENV
76d32f7577SMarkus Hoffrogge          echo "VERSION_TAG=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
77d32f7577SMarkus Hoffrogge          echo "RELEASE_VERSION=${GITHUB_REF/refs\/tags\/v/}" >> $GITHUB_ENV
78d32f7577SMarkus Hoffrogge          echo "APP_INFO_VERSION=$(sed -n -E 's/^${{ env.APP_INFO_FILE_VERSION_KEY }}[ \t]+([0-9-]+).*/\1/p' ${{ env.APP_INFO_FILE }})" >> $GITHUB_ENV
79d32f7577SMarkus Hoffrogge
80d32f7577SMarkus Hoffrogge      # Validate app info version and set release name
81d32f7577SMarkus Hoffrogge      - name: Validate app info version and set release name
82d32f7577SMarkus Hoffrogge        run: |
83d32f7577SMarkus Hoffrogge          if [ "${{ env.RELEASE_VERSION }}" != "${{ env.APP_INFO_VERSION }}" ]; then
84d32f7577SMarkus Hoffrogge            echo "Mismatch of release version=${{ env.RELEASE_VERSION }} and application info version=${{ env.APP_INFO_VERSION }}!" >&2
85d32f7577SMarkus Hoffrogge            echo "Please review the value for key=${{ env.APP_INFO_FILE_VERSION_KEY }} in file ${{ env.APP_INFO_FILE }}."
86d32f7577SMarkus Hoffrogge            exit 1
87d32f7577SMarkus Hoffrogge          fi
88d32f7577SMarkus Hoffrogge          echo "RELEASE_NAME=Release ${{ env.APP_INFO_VERSION }}" >> $GITHUB_ENV
89d32f7577SMarkus Hoffrogge
90d32f7577SMarkus Hoffrogge      - name: Validate CHANGELOG.md for this release version
91d32f7577SMarkus Hoffrogge        # explanation of sed command:
92d32f7577SMarkus Hoffrogge        # 1. select lines between SED_VERSION_BEGIN_PATTERN and SED_VERSION_END_PATTERN
93d32f7577SMarkus Hoffrogge        # 2. invert this selection
94d32f7577SMarkus Hoffrogge        # 3. delete it
95d32f7577SMarkus Hoffrogge        # => only selection is remaining in stream
96d32f7577SMarkus Hoffrogge        run: |
97d32f7577SMarkus Hoffrogge          SED_VERSION_BEGIN_PATTERN="/^## \\[${{ env.RELEASE_VERSION }}\\]/"
98d32f7577SMarkus Hoffrogge          SED_VERSION_END_PATTERN="/^## /"
99d32f7577SMarkus Hoffrogge          echo "Pattern used for sed: ${SED_VERSION_BEGIN_PATTERN},${SED_VERSION_END_PATTERN} ! d"
100d32f7577SMarkus Hoffrogge          #
101d32f7577SMarkus Hoffrogge          # Extract the release notes for this RELEASE_VERSION including the line of the previous version:
102d32f7577SMarkus Hoffrogge          #
103d32f7577SMarkus Hoffrogge          RELEASE_NOTES_WITH_PREV_VERSION=$(sed -e "${SED_VERSION_BEGIN_PATTERN},${SED_VERSION_END_PATTERN} ! d" CHANGELOG.md)
104d32f7577SMarkus Hoffrogge          echo ">>>>>> RELEASE_NOTES_WITH_PREV_VERSION - BEGIN >>>>>>"
105d32f7577SMarkus Hoffrogge          echo "${RELEASE_NOTES_WITH_PREV_VERSION}"
106d32f7577SMarkus Hoffrogge          echo "<<<<<< RELEASE_NOTES_WITH_PREV_VERSION - END <<<<<<"
107d32f7577SMarkus Hoffrogge          #
108d32f7577SMarkus Hoffrogge          # Format the release notes:
109d32f7577SMarkus Hoffrogge          #
110d32f7577SMarkus Hoffrogge          # 1. Remove last 2 lines: head -n 2
111d32f7577SMarkus Hoffrogge          # 2. Remove any empty line from the end: sed -e :a -e '/^\n*$/{$d;N;ba' -e '}'
112d32f7577SMarkus Hoffrogge          #    (s. http://sed.sourceforge.net/sed1line.txt for reference)
113d32f7577SMarkus Hoffrogge          #
114d32f7577SMarkus Hoffrogge          #RELEASE_VERSION_NOTES=$(echo "$RELEASE_NOTES_WITH_PREV_VERSION" | head -n -2 | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}')
115d32f7577SMarkus Hoffrogge          #echo "${RELEASE_VERSION_NOTES}" >> RELEASE.md
116d32f7577SMarkus Hoffrogge          #printf "\n" >> RELEASE.md
117d32f7577SMarkus Hoffrogge          #
118d32f7577SMarkus Hoffrogge          # Extract previous release version:
119d32f7577SMarkus Hoffrogge          #
120d32f7577SMarkus Hoffrogge          # 1. Cut the last line only: tail -1
121d32f7577SMarkus Hoffrogge          # 2. Get the version from the enclosing [] brackets: awk -F "[][]" '{ print $2 }'
122d32f7577SMarkus Hoffrogge          #
123d32f7577SMarkus Hoffrogge          PREV_RELEASE_VERSION=$(echo "$RELEASE_NOTES_WITH_PREV_VERSION" | tail -1 | awk -F "[][]" '{ print $2 }')
124d32f7577SMarkus Hoffrogge          if [ -z "$PREV_RELEASE_VERSION" ]; then
125d32f7577SMarkus Hoffrogge            EXPECTED_COMPARE_URL="${{ github.server_url }}/${{ github.repository }}/releases/tag/v${{ env.RELEASE_VERSION }}"
126d32f7577SMarkus Hoffrogge          else
127d32f7577SMarkus Hoffrogge            EXPECTED_COMPARE_URL="${{ github.server_url }}/${{ github.repository }}/compare/v${PREV_RELEASE_VERSION}..v${{ env.RELEASE_VERSION }}"
128d32f7577SMarkus Hoffrogge          fi
129d32f7577SMarkus Hoffrogge          # Validate CHANGELOG.md content
130d32f7577SMarkus Hoffrogge          IS_OK="true"
131d32f7577SMarkus Hoffrogge          if ! grep -q "^## \\[${{ env.RELEASE_VERSION }}\\]" CHANGELOG.md; then
132d32f7577SMarkus Hoffrogge            IS_OK="false"
133d32f7577SMarkus Hoffrogge            echo "ERROR: CHANGELOG.md does not contain an entry for this release version of format: ## [${{ env.RELEASE_VERSION }}]"
134d32f7577SMarkus Hoffrogge          fi
135d32f7577SMarkus Hoffrogge          if ! grep -q "^\\[${{ env.RELEASE_VERSION }}\\]: ${EXPECTED_COMPARE_URL}" CHANGELOG.md; then
136d32f7577SMarkus Hoffrogge            IS_OK="false"
137d32f7577SMarkus Hoffrogge            echo "ERROR: CHANGELOG.md does not contain a line with a compare link of format: [${{ env.RELEASE_VERSION }}]: ${EXPECTED_COMPARE_URL}"
138d32f7577SMarkus Hoffrogge          fi
139d32f7577SMarkus Hoffrogge          if [ "$IS_OK" != "true" ]; then
140d32f7577SMarkus Hoffrogge            echo "Please review CHANGELOG.md and update it for the content expected."
141d32f7577SMarkus Hoffrogge            exit 1
142d32f7577SMarkus Hoffrogge          fi
143d32f7577SMarkus Hoffrogge
144d32f7577SMarkus Hoffrogge     # Prepare release notes and build directory
145d32f7577SMarkus Hoffrogge      - name: Prepare release notes and build directory
146d32f7577SMarkus Hoffrogge        run: |
147d32f7577SMarkus Hoffrogge          mkdir ${{ env.BUILD_DIR }}
148d32f7577SMarkus Hoffrogge          #cp ./README.md ${{ env.BUILD_DIR }}/README.md
149d32f7577SMarkus Hoffrogge          touch ${{ env.BUILD_DIR }}/README.md
150d32f7577SMarkus Hoffrogge          cp ./CHANGELOG.md ${{ env.BUILD_DIR }}/CHANGELOG.md
151d32f7577SMarkus Hoffrogge          cp ./.github/workflows/resources/RELEASE_HEAD.md ${{ env.BUILD_DIR }}/RELEASE_HEAD.md
152d32f7577SMarkus Hoffrogge
153d32f7577SMarkus Hoffrogge      # Format the filename of this release
154d32f7577SMarkus Hoffrogge      - name: Format release filename
155d32f7577SMarkus Hoffrogge        id: format_release_filename
156d32f7577SMarkus Hoffrogge        run: |
1574f40c9b4SMarkus Hoffrogge          echo "FILE_NAME=${{ env.APP_NAME }}-${{ env.APP_INFO_VERSION }}.zip" >> $GITHUB_OUTPUT
158d32f7577SMarkus Hoffrogge
159d32f7577SMarkus Hoffrogge      # Create archive file
160d32f7577SMarkus Hoffrogge      - name: Build release archive
161d32f7577SMarkus Hoffrogge        uses: GHCICD/zip-release@master
162d32f7577SMarkus Hoffrogge        with:
163d32f7577SMarkus Hoffrogge          type: 'zip'
164d32f7577SMarkus Hoffrogge          filename: ${{ env.BUILD_DIR }}/${{ steps.format_release_filename.outputs.FILE_NAME }}
165d32f7577SMarkus Hoffrogge          exclusions: ${{ env.ZIP_EXCLUSIONS }}
166d32f7577SMarkus Hoffrogge
167d32f7577SMarkus Hoffrogge      # Create release notes by release-notes-from-changelog
168d32f7577SMarkus Hoffrogge      - name: Create release notes by GHCICD/release-notes-from-changelog@v1
169d32f7577SMarkus Hoffrogge        uses: GHCICD/release-notes-from-changelog@v1
170d32f7577SMarkus Hoffrogge        with:
171d32f7577SMarkus Hoffrogge          version: ${{ env.RELEASE_VERSION }}
172d32f7577SMarkus Hoffrogge          working-directory: ${{ env.BUILD_DIR }}
173d32f7577SMarkus Hoffrogge
174d32f7577SMarkus Hoffrogge      - name: Log RELEASE.md
175d32f7577SMarkus Hoffrogge        run: |
176d32f7577SMarkus Hoffrogge          echo ">>>>> build/RELEASE.md:"
177d32f7577SMarkus Hoffrogge          cat ${{ env.BUILD_DIR }}/RELEASE.md
178d32f7577SMarkus Hoffrogge          echo "<<<<<"
179d32f7577SMarkus Hoffrogge#          echo ">>>>> build/CHANGELOG.md:"
180d32f7577SMarkus Hoffrogge#          cat ${{ env.BUILD_DIR }}/CHANGELOG.md
181d32f7577SMarkus Hoffrogge#          echo "<<<<<"
182d32f7577SMarkus Hoffrogge
183d32f7577SMarkus Hoffrogge      # Create release with info from CHANGELOG.md
1844f40c9b4SMarkus Hoffrogge      - name: Create GitHub release by ncipollo/release-action@v1
185d32f7577SMarkus Hoffrogge        id: create_release
1864f40c9b4SMarkus Hoffrogge        uses: ncipollo/release-action@v1
187d32f7577SMarkus Hoffrogge        with:
1884f40c9b4SMarkus Hoffrogge          token: ${{ secrets.GITHUB_TOKEN }}
1894f40c9b4SMarkus Hoffrogge          name: ${{ env.RELEASE_NAME }}
1904f40c9b4SMarkus Hoffrogge          tag: ${{ env.VERSION_TAG }}
1914f40c9b4SMarkus Hoffrogge          bodyFile: ${{ env.BUILD_DIR }}/RELEASE.md
1924f40c9b4SMarkus Hoffrogge          artifacts: ${{ env.BUILD_DIR }}/${{ steps.format_release_filename.outputs.FILE_NAME }}
1934f40c9b4SMarkus Hoffrogge          artifactContentType: application/zip
194d32f7577SMarkus Hoffrogge#
195d32f7577SMarkus Hoffrogge# EOF
196d32f7577SMarkus Hoffrogge#
197