|
| 1 | +name: Build and Release (Manual Run) v1.3 |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: # This event allows manual triggering |
| 5 | + |
| 6 | +jobs: |
| 7 | + build-and-release: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + |
| 10 | + steps: |
| 11 | + - name: Checkout repository |
| 12 | + uses: actions/checkout@v3 |
| 13 | + with: |
| 14 | + fetch-depth: 0 |
| 15 | + |
| 16 | + |
| 17 | + - name: Set up JDK 8 |
| 18 | + uses: actions/setup-java@v3 |
| 19 | + with: |
| 20 | + java-version: 8 |
| 21 | + distribution: 'temurin' |
| 22 | + |
| 23 | + |
| 24 | + - name: Set Extensions Dir |
| 25 | + id: set_ext_dir |
| 26 | + run: | |
| 27 | + echo "Setting Extensions Dir..." |
| 28 | + mkdir ${HOME}/release |
| 29 | + mkdir /tmp/to |
| 30 | + echo "NEW_RELIC_EXTENSIONS_DIR=${HOME}/release" >> $GITHUB_ENV |
| 31 | +
|
| 32 | + - name: Build with Gradle and verifyInstrumentation |
| 33 | + run: | |
| 34 | + . ./newrelic-dependencies.sh |
| 35 | + ./gradlew clean build install verifyInstrumentation |
| 36 | +
|
| 37 | + - name: Identify Release Type |
| 38 | + id: define_release_type |
| 39 | + run: | |
| 40 | + echo "Generating changelog to check type of release..." |
| 41 | + old_tag=$(git describe --abbrev=0 --tags 2>/dev/null) || true |
| 42 | + if [[ -n "$old_tag" ]]; then |
| 43 | + changelog=$(git log --pretty=format:"- %s (%h)" $old_tag..HEAD) |
| 44 | + fi |
| 45 | + if echo "$changelog" | grep -iqE '\bBREAKING CHANGE\b'; then |
| 46 | + echo "RELEASE_TYPE=major" >> $GITHUB_ENV |
| 47 | + elif echo "$changelog" | grep -iqE '\bfeat\b'; then |
| 48 | + echo "RELEASE_TYPE=minor" >> $GITHUB_ENV |
| 49 | + else |
| 50 | + echo "RELEASE_TYPE=patch" >> $GITHUB_ENV |
| 51 | + fi |
| 52 | + |
| 53 | + - name: Set release version |
| 54 | + id: set_release_version |
| 55 | + run: | |
| 56 | + major_version=1 |
| 57 | + minor_version=0 |
| 58 | + patch_revision=1 |
| 59 | +
|
| 60 | + # Retrieve the latest release tag |
| 61 | + latest_tag=$(git describe --abbrev=0 --tags 2>/dev/null) || true |
| 62 | + echo "LATEST_TAG=${latest_tag}" >> $GITHUB_ENV |
| 63 | +
|
| 64 | + if [[ -n "$latest_tag" && $latest_tag == v* ]]; then |
| 65 | + # Extract the major and minor versions from the latest tag |
| 66 | + current_major_version=$(echo $latest_tag | cut -d'.' -f1 | sed 's/v//') |
| 67 | + current_minor_version=$(echo $latest_tag | cut -d'.' -f2) |
| 68 | + current_patch_revision=$(echo $latest_tag | cut -d'.' -f3) |
| 69 | + |
| 70 | + if [ "${{ env.RELEASE_TYPE }}" = "major" ]; then |
| 71 | + major_version=$((current_major_version +1 )) |
| 72 | + elif [ "${{ env.RELEASE_TYPE }}" = "minor" ]; then |
| 73 | + minor_version=$((current_minor_version + 1)) |
| 74 | + major_version=$((current_major_version)) |
| 75 | + else |
| 76 | + patch_revision=$((current_patch_revision + 1)) |
| 77 | + minor_version=$((current_minor_version)) |
| 78 | + major_version=$((current_major_version)) |
| 79 | + fi |
| 80 | + |
| 81 | + fi |
| 82 | + |
| 83 | + # Set the release version environment variable |
| 84 | + release_version="v${major_version}.${minor_version}.${patch_revision}" |
| 85 | + echo "RELEASE_VERSION=${release_version}" >> $GITHUB_ENV |
| 86 | + |
| 87 | + - name: Set Tag |
| 88 | + id: set_tag |
| 89 | + run: echo "::set-output name=tag::${{ env.RELEASE_VERSION }}" |
| 90 | + |
| 91 | + - name: Set release name |
| 92 | + id: set_release_name |
| 93 | + run: | |
| 94 | + repo_name="${{ github.repository }}" |
| 95 | + sanitized_repo_name=$(echo "$repo_name" | awk -F 'newrelic-java-' '{print $2}') |
| 96 | + echo "RELEASE_NAME=${sanitized_repo_name}-instrumentation-" >> $GITHUB_ENV |
| 97 | + previous_tag=$(git describe --abbrev=0 --tags HEAD^ 2>/dev/null || git rev-list --max-parents=0 HEAD) |
| 98 | +
|
| 99 | + - name: Create Archive |
| 100 | + run: | |
| 101 | + echo "CURRENT=${PWD}" >> $GITHUB_ENV |
| 102 | + cd ${HOME}/release |
| 103 | + zip -r /tmp/to/${{ env.RELEASE_NAME}}${{ steps.set_tag.outputs.tag }}.zip *.jar |
| 104 | + cd ${{env.CURRENT}} |
| 105 | +
|
| 106 | + |
| 107 | + |
| 108 | + - name: Create Release |
| 109 | + id: create_release |
| 110 | + uses: actions/github-script@v6 |
| 111 | + with: |
| 112 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 113 | + script: | |
| 114 | + try { |
| 115 | + var changelog = ``; |
| 116 | + var tag = '' + `${{ steps.set_tag.outputs.tag }}`; |
| 117 | + const archivePath = '/tmp/to/${{ env.RELEASE_NAME}}${{ steps.set_tag.outputs.tag }}.zip'; |
| 118 | + var response = await github.rest.repos.createRelease({ |
| 119 | + draft:false, |
| 120 | + generate_release_notes:true, |
| 121 | + name:tag, |
| 122 | + owner:context.repo.owner, |
| 123 | + prerelease:false, |
| 124 | + repo:context.repo.repo, |
| 125 | + tag_name:tag, |
| 126 | + body:changelog |
| 127 | + }); |
| 128 | +
|
| 129 | + core.exportVariable('RELEASE_ID', response.data.id); |
| 130 | + core.exportVariable('RELEASE_URL', response.data.html_url); |
| 131 | + core.exportVariable('RELEASE_UPLOAD_URL', response.data.upload_url); |
| 132 | + } catch (error) { |
| 133 | + core.setFailed(error.message); |
| 134 | + } |
| 135 | + - name: Upload Release Artifacts |
| 136 | + uses: actions/upload-release-asset@v1 |
| 137 | + env: |
| 138 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 139 | + with: |
| 140 | + asset_path: /tmp/to/${{ env.RELEASE_NAME}}${{ steps.set_tag.outputs.tag }}.zip |
| 141 | + asset_name: ${{ env.RELEASE_NAME}}${{ steps.set_tag.outputs.tag }}.zip |
| 142 | + upload_url: ${{ env.RELEASE_UPLOAD_URL }} |
| 143 | + asset_content_type: application/zip |
| 144 | + |
| 145 | + - name: "Generate release changelog" |
| 146 | + id: github_changelog |
| 147 | + uses: Helmisek/conventional-changelog-generator@v1.0.6-release |
| 148 | + with: |
| 149 | + repo-token: ${{ secrets.GITHUB_TOKEN }} |
| 150 | + commit-types: "fix:Bug Fixes,feat:Features,doc:Documentation,build:Build Upgrades,BREAKING CHANGE:Enhancements" |
| 151 | + template-path: ".github/workflows/CHANGELOG.tpl.md" |
| 152 | + |
| 153 | + |
| 154 | + - name: update CHANGELOG.md |
| 155 | + run: | |
| 156 | + # Content to add at the top |
| 157 | + # Get the current date in YYYY-MM-DD format |
| 158 | + release_date=$(date +"%Y-%m-%d") |
| 159 | + version="## Version: [${{env.RELEASE_VERSION}}](${{ env.RELEASE_URL }}) | Created: $release_date" |
| 160 | + content="$version${{steps.github_changelog.outputs.changelog}}" |
| 161 | +
|
| 162 | + # Existing file |
| 163 | + file="CHANGELOG.md" |
| 164 | +
|
| 165 | + # Create a temporary file with the content at the top and existing content below |
| 166 | + |
| 167 | + echo "$content" > temp_file.txt |
| 168 | + cat "$file" >> temp_file.txt |
| 169 | +
|
| 170 | + # Overwrite the original file with the updated content |
| 171 | + mv temp_file.txt "$file" |
| 172 | + |
| 173 | + # Commit the updated CHANGELOG.md file |
| 174 | + git add CHANGELOG.md |
| 175 | + git config --local user.email "action@github.com" |
| 176 | + git config --local user.name "GitHub Action" |
| 177 | + git commit -m "Update Changelog for Release [skip ci]" |
| 178 | +
|
| 179 | + # Push the changes to the remote repository |
| 180 | + git push --quiet --set-upstream origin HEAD |
| 181 | + |
| 182 | + - name: Get Compare URL |
| 183 | + run: | |
| 184 | + compare=$(echo ${{ env.RELEASE_URL }} | sed 's/releases\/tag.*/compare/') |
| 185 | + compareurl=$(echo "\nFull Changelog: ($compare/${{ env.LATEST_TAG }}...${{ steps.set_tag.outputs.tag }})") |
| 186 | + echo "COMPAREURL=${compareurl}" >> $GITHUB_ENV |
| 187 | + |
| 188 | + - name: Update Release |
| 189 | + id: update_release |
| 190 | + uses: actions/github-script@v6 |
| 191 | + with: |
| 192 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 193 | + script: | |
| 194 | + try { |
| 195 | + |
| 196 | + |
| 197 | + var changelog = `${{steps.github_changelog.outputs.changelog}}` + `${{env.COMPAREURL}}` ; |
| 198 | + var release_id = `${{env.RELEASE_ID}}`; |
| 199 | + var tag = '' + `${{ steps.set_tag.outputs.tag }}`; |
| 200 | + const archivePath = '/tmp/to/${{ env.RELEASE_NAME}}${{ steps.set_tag.outputs.tag }}.zip'; |
| 201 | + var _response = await github.rest.repos.updateRelease({ |
| 202 | + draft:false, |
| 203 | + generate_release_notes:true, |
| 204 | + owner:context.repo.owner, |
| 205 | + repo: context.repo.repo, |
| 206 | + prerelease:false, |
| 207 | + release_id:release_id, |
| 208 | + body:changelog |
| 209 | + }); |
| 210 | +
|
| 211 | + core.exportVariable('RELEASE_ID', _response.data.id); |
| 212 | + core.exportVariable('RELEASE_URL', _response.data.html_url); |
| 213 | + core.exportVariable('RELEASE_UPLOAD_URL', _response.data.upload_url); |
| 214 | + } catch (error) { |
| 215 | + core.setFailed(error.message); |
| 216 | + } |
| 217 | +
|
0 commit comments