|
| 1 | +name: Update Version On Release.md |
| 2 | + |
| 3 | +on: |
| 4 | + # Triggers the workflow on release push tag |
| 5 | + push: |
| 6 | + tags: |
| 7 | + - 'r*.*.*' |
| 8 | + branches: |
| 9 | + - '!master' |
| 10 | + |
| 11 | +permissions: |
| 12 | + actions : write |
| 13 | + checks : write |
| 14 | + contents : write |
| 15 | + deployments : write |
| 16 | + issues : write |
| 17 | + packages : write |
| 18 | + pull-requests : write |
| 19 | + repository-projects : write |
| 20 | + security-events : write |
| 21 | + statuses : write |
| 22 | + |
| 23 | +jobs: |
| 24 | + update_and_push: |
| 25 | + runs-on: [ self-hosted, linux ] |
| 26 | + |
| 27 | + steps: |
| 28 | + - uses: actions/checkout@v2 |
| 29 | + with: |
| 30 | + ref: ${{ github.head_ref }} |
| 31 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 32 | + |
| 33 | + - name: Set Git Config |
| 34 | + run: | |
| 35 | + git config --global user.name 'DT Platform Datavis CI' |
| 36 | + git config --global user.email 'dt-platform-datavis-ci-noreply@unity3d.com' |
| 37 | + git remote set-url origin https://github:${{ secrets.GITHUB_TOKEN }}@github.cds.internal.unity3d.com/${{ github.repository }} |
| 38 | + |
| 39 | + - name: Get current date |
| 40 | + id: date |
| 41 | + run: | |
| 42 | + echo "::set-output name=date::$(date +'%Y-%m-%d')" |
| 43 | + echo DATE = $(date +'%Y-%m-%d') |
| 44 | + |
| 45 | + - name: Tag Match |
| 46 | + id: tag-match |
| 47 | + shell: bash |
| 48 | + run: | |
| 49 | + if [[ ${{ github.event.ref }} =~ ^refs\/tags\/([rR](elease)?[\.-]?([0-9]+\.[0-9]+\.[0-9]+(-.*\.[0-9])))$ ]]; then |
| 50 | + echo ::set-output name=tag::${BASH_REMATCH[1]} |
| 51 | + echo ::set-output name=version::${BASH_REMATCH[3]} |
| 52 | + echo TAG = ${BASH_REMATCH[1]} |
| 53 | + echo VERSION = ${BASH_REMATCH[3]} |
| 54 | + echo ::set-output name=rc::rc-${BASH_REMATCH[3]} |
| 55 | + git tag -l | xargs git tag -d |
| 56 | + git fetch --tags |
| 57 | + echo ::set-output name=origin::$(git tag -l rc-${BASH_REMATCH[3]}) |
| 58 | + fi |
| 59 | + |
| 60 | + - name: Validate Tag |
| 61 | + if: ${{ (steps.tag-match.outputs.tag != '') && (steps.tag-match.outputs.origin != '') }} |
| 62 | + run: | |
| 63 | + echo A tag named ${{ steps.tag-match.outputs.rc }} already exist. Increment the version to fix this. |
| 64 | + git push --delete origin ${{ steps.tag-match.outputs.tag }} |
| 65 | + exit 1 |
| 66 | +
|
| 67 | + - name: Branch Name Is Develop |
| 68 | + id: get-branch-dev |
| 69 | + if: ${{ steps.tag-match.outputs.tag != '' }} |
| 70 | + run: | |
| 71 | + git fetch |
| 72 | + echo ::set-output name=branch::$( git branch --contains ${{ steps.tag-match.outputs.tag }} -r | grep --only-matching --max-count 1 --extended-regex '^.*origin/develop.*$') |
| 73 | +
|
| 74 | + - name: Branch Name Is Other |
| 75 | + id: get-branch-other |
| 76 | + if: ${{ (steps.tag-match.outputs.tag != '') && (steps.get-branch-dev.outputs.branch == '') }} |
| 77 | + run: | |
| 78 | + echo ::set-output name=branch::$( git branch --contains ${{ steps.tag-match.outputs.tag }} -r | grep --only-matching --max-count 1 --extended-regex 'origin\/[^ H]+' | sed -E 's/.*origin\/([^ ]+).*/\1/') |
| 79 | +
|
| 80 | + - name: Get Branch Name |
| 81 | + id: get-branch |
| 82 | + if: ${{ steps.tag-match.outputs.tag != '' }} |
| 83 | + run: | |
| 84 | + if [[ '${{ steps.get-branch-dev.outputs.branch }}' == '' ]]; then |
| 85 | + echo ::set-output name=branch::${{ steps.get-branch-other.outputs.branch }} |
| 86 | + else |
| 87 | + echo ::set-output name=branch::develop |
| 88 | + fi |
| 89 | + echo ::set-output name=changelog::changelog-${{ steps.tag-match.outputs.version }} |
| 90 | + echo ::set-output name=staging::staging-${{ steps.tag-match.outputs.version }} |
| 91 | +
|
| 92 | + - name: Changelog branch |
| 93 | + if: ${{ steps.get-branch.outputs.branch == 'develop' }} |
| 94 | + run: | |
| 95 | + git checkout -b ${{ steps.get-branch.outputs.changelog }} |
| 96 | + |
| 97 | + - name: Checkout the current branch |
| 98 | + if: ${{ (steps.get-branch.outputs.branch != 'develop') && (steps.tag-match.outputs.tag != '') }} |
| 99 | + run: | |
| 100 | + git checkout ${{ steps.get-branch.outputs.branch }} |
| 101 | + git pull |
| 102 | + |
| 103 | + - name: Update CHANGELOG.md |
| 104 | + if: ${{ steps.tag-match.outputs.tag != '' }} |
| 105 | + run: sed -i "s/\\[\\\$ReleasedVersion\\\$\\]/[\$ReleasedVersion\$]\r\n### Added\r\n### Modified\r\n### Fix\r\n\r\n## [${{ steps.tag-match.outputs.version }}] - ${{ steps.date.outputs.date }}/" CHANGELOG.md |
| 106 | + |
| 107 | + - name: Commit the changes |
| 108 | + if: ${{ steps.tag-match.outputs.tag != '' }} |
| 109 | + run: | |
| 110 | + git add CHANGELOG.md |
| 111 | + git commit -m "[skip ci] Updated CHANGELOG.md for ${{ steps.tag-match.outputs.version }}" |
| 112 | + |
| 113 | + - name: Push Changelog Branch |
| 114 | + if: ${{ steps.get-branch.outputs.branch == 'develop' }} |
| 115 | + run: git push -f --set-upstream origin ${{ steps.get-branch.outputs.changelog }} |
| 116 | + |
| 117 | + - name: Create Changelog Pull Request |
| 118 | + id: pr-changelog |
| 119 | + if: ${{ steps.get-branch.outputs.branch == 'develop' }} |
| 120 | + run: | |
| 121 | + echo ::set-output name=number::$(curl -i \ |
| 122 | + --request POST \ |
| 123 | + --header 'authorization: bearer ${{ secrets.GITHUB_TOKEN }}' \ |
| 124 | + --header 'content-type: application/json' \ |
| 125 | + https://github.cds.internal.unity3d.com/api/v3/repos/${{ github.repository }}/pulls \ |
| 126 | + --data '{"title":"CHANGELOG ${{ steps.tag-match.outputs.version }}", "body":"This PR is auto-generated by workflow/update-version-on-release", "head":"${{ steps.get-branch.outputs.changelog }}", "base":"${{ steps.get-branch.outputs.branch }}"}' \ |
| 127 | + | grep -o "\"issue_url\":.*/[0-9]"".*" \ |
| 128 | + | sed -E 's/.*\/([0-9]+)",.*/\1/') |
| 129 | + |
| 130 | + - name: Validate Changelog Pull Request |
| 131 | + if: ${{ (steps.get-branch.outputs.branch == 'develop') && (steps.pr-changelog.outputs.number == '') }} |
| 132 | + run: | |
| 133 | + echo Invalid Pull Request Creation |
| 134 | + exit 2 |
| 135 | +
|
| 136 | + - name: Approve the Changelog Pull Request |
| 137 | + id: approve-changelog |
| 138 | + if: ${{ steps.get-branch.outputs.branch == 'develop' }} |
| 139 | + run: | |
| 140 | + echo ::set-output name=approved::$(curl --request POST \ |
| 141 | + --url https://github.cds.internal.unity3d.com/api/v3/repos/${{ github.repository }}/pulls/${{ steps.pr-changelog.outputs.number }}/reviews \ |
| 142 | + --header 'authorization: bearer ${{ secrets.GIT_TOKEN_APPROVAL }}' \ |
| 143 | + --header 'content-type: application/json' \ |
| 144 | + --data '{"event":"APPROVE"}' \ |
| 145 | + | grep -o '.*"state": *"APPROVED".*') |
| 146 | +
|
| 147 | + - name: Validate Changelog Pull Request State |
| 148 | + if: ${{ (steps.get-branch.outputs.branch == 'develop') && (steps.approve-changelog.outputs.approved == '') }} |
| 149 | + run: | |
| 150 | + echo Could not approve the PR |
| 151 | + exit 3 |
| 152 | +
|
| 153 | + - name: Checkout the current branch |
| 154 | + if: ${{ steps.get-branch.outputs.branch == 'develop' }} |
| 155 | + run: | |
| 156 | + git checkout ${{ steps.get-branch.outputs.branch }} |
| 157 | + git pull |
| 158 | +
|
| 159 | + - name: Push the changes to develop |
| 160 | + id: push-develop |
| 161 | + if: ${{ steps.get-branch.outputs.branch == 'develop' }} |
| 162 | + run: | |
| 163 | + echo ::set-output name=success::$(curl --request PUT \ |
| 164 | + --url https://github.cds.internal.unity3d.com/api/v3/repos/${{ github.repository }}/pulls/${{ steps.pr-changelog.outputs.number }}/merge \ |
| 165 | + --header 'authorization: bearer ${{ secrets.GIT_TOKEN_APPROVAL }}' \ |
| 166 | + --header 'content-type: application/json' \ |
| 167 | + | grep -o '.*"merged": *true.*') |
| 168 | +
|
| 169 | + - name: Push the changes to the current branch |
| 170 | + if: ${{ (steps.tag-match.outputs.tag != '') && (steps.get-branch.outputs.branch != 'develop') }} |
| 171 | + run: git push |
| 172 | + |
| 173 | + - name: Validate Push to the current branch |
| 174 | + if: ${{ (steps.get-branch.outputs.branch == 'develop') && (steps.push-develop.outputs.success == '') }} |
| 175 | + run: | |
| 176 | + echo Could not push the PR |
| 177 | + exit 4 |
| 178 | +
|
| 179 | + - name: Go back to the original commit |
| 180 | + if: ${{ steps.tag-match.outputs.tag != '' }} |
| 181 | + run: | |
| 182 | + git pull |
| 183 | + git checkout $GITHUB_SHA |
| 184 | +
|
| 185 | + - name: Create a branch to merge |
| 186 | + if: ${{ steps.tag-match.outputs.tag != '' }} |
| 187 | + run: | |
| 188 | + git fetch origin |
| 189 | + git checkout -b ${{ steps.get-branch.outputs.staging }} |
| 190 | +
|
| 191 | + - name: Update package.json |
| 192 | + if: ${{ steps.tag-match.outputs.tag != '' }} |
| 193 | + run: > |
| 194 | + sed -i "s/\"version\": \"0.0.0\"/\"version\": \"${{ steps.tag-match.outputs.version }}\"/g" package.json |
| 195 | +
|
| 196 | + - name: Update CHANGELOG.md |
| 197 | + if: ${{ steps.tag-match.outputs.tag != '' }} |
| 198 | + run: sed -i "s/\\[\\\$ReleasedVersion\\\$\\]/[${{ steps.tag-match.outputs.version }}] - ${{ steps.date.outputs.date }}/" CHANGELOG.md |
| 199 | + |
| 200 | + - name: Update All Other Files |
| 201 | + if: ${{ steps.tag-match.outputs.tag != '' }} |
| 202 | + run: find . -type f -not -path '*/\.*' -exec sed -i "s/\\\$ReleasedVersion\\\$/v${{ steps.tag-match.outputs.version }}/g" {} + |
| 203 | + |
| 204 | + - name: Create Staging |
| 205 | + if: ${{ steps.tag-match.outputs.tag != '' }} |
| 206 | + run: | |
| 207 | + git add . |
| 208 | + git commit -m "[skip ci] Updated the package version to ${{ steps.tag-match.outputs.version }}" |
| 209 | + git merge --strategy=ours origin/master --allow-unrelated-histories -m "Released Candidate ${{ steps.tag-match.outputs.version }}" |
| 210 | + git push -f --set-upstream origin ${{ steps.get-branch.outputs.staging }} |
| 211 | +
|
| 212 | + - name: Create Master Pull Request |
| 213 | + id: pr-master |
| 214 | + if: ${{ steps.tag-match.outputs.tag != '' }} |
| 215 | + run: | |
| 216 | + echo ::set-output name=number::$(curl -i \ |
| 217 | + --request POST \ |
| 218 | + --header "authorization: bearer ${{ secrets.GITHUB_TOKEN }}" \ |
| 219 | + --header 'content-type: application/json' \ |
| 220 | + https://github.cds.internal.unity3d.com/api/v3/repos/${{ github.repository }}/pulls \ |
| 221 | + --data '{"title":"Staging ${{ steps.tag-match.outputs.version }}", "body":"This PR is auto-generated by workflow/update-version-on-release", "head":"${{ steps.get-branch.outputs.staging }}", "base":"master"}' \ |
| 222 | + | grep -o "\"issue_url\":.*/[0-9]"".*" \ |
| 223 | + | sed -E 's/.*\/([0-9]+)",.*/\1/') |
| 224 | + |
| 225 | + - name: Validate Master Pull Request |
| 226 | + if: ${{ (steps.tag-match.outputs.tag != '') && (steps.pr-master.outputs.number == '') }} |
| 227 | + run: | |
| 228 | + echo Invalid Pull Request Creation |
| 229 | + exit 5 |
| 230 | +
|
| 231 | + - name: Approve the Master Pull Request |
| 232 | + id: approve-master |
| 233 | + if: ${{ steps.tag-match.outputs.tag != '' }} |
| 234 | + run: | |
| 235 | + echo ::set-output name=approved::$(curl --request POST \ |
| 236 | + --url https://github.cds.internal.unity3d.com/api/v3/repos/${{ github.repository }}/pulls/${{ steps.pr-master.outputs.number }}/reviews \ |
| 237 | + --header 'authorization: bearer ${{ secrets.GIT_TOKEN_APPROVAL }}' \ |
| 238 | + --header 'content-type: application/json' \ |
| 239 | + --data '{"event":"APPROVE"}' \ |
| 240 | + | grep -o '.*"state": *"APPROVED".*') |
| 241 | +
|
| 242 | + - name: Validate Master Pull Request State |
| 243 | + if: ${{ (steps.tag-match.outputs.tag != '') && (steps.approve-master.outputs.approved == '') }} |
| 244 | + run: | |
| 245 | + echo Could not approve the PR |
| 246 | + exit 6 |
| 247 | + |
| 248 | + - name: Push the Changes to Master |
| 249 | + if: ${{ steps.tag-match.outputs.tag != '' }} |
| 250 | + run: | |
| 251 | + git checkout master |
| 252 | + git pull |
| 253 | + git merge ${{ steps.get-branch.outputs.staging }} |
| 254 | + git pull |
| 255 | + git push |
| 256 | + |
| 257 | + - name: Update Tags |
| 258 | + if: ${{ steps.tag-match.outputs.tag != '' }} |
| 259 | + run: | |
| 260 | + git tag -a -f -m "Released Candidate ${{ steps.tag-match.outputs.version }}" ${{ steps.tag-match.outputs.rc }} |
| 261 | + git push -f origin ${{ steps.tag-match.outputs.rc }} |
| 262 | + git push --delete origin ${{ steps.tag-match.outputs.tag }} |
0 commit comments