Create Java Release Action #1930
This file contains hidden or 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: Create Java Release Action | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| repository: | |
| description: 'GIT Repository project to run the workflow' | |
| required: true | |
| type: string | |
| tag: | |
| description: 'Tag value (without v prefix)' | |
| required: true | |
| type: string | |
| base_branch: | |
| description: 'Base branch to create the RC TAG. Default is develop' | |
| type: string | |
| default: "develop" | |
| required: false | |
| increment_snapshot: | |
| description: 'Increment SNAPSHOT version in a branch next-release' | |
| type: boolean | |
| default: false | |
| merge_in_main: | |
| description: 'For RELEASE pipeline (not RC), merge in main. Default is true' | |
| type: boolean | |
| default: true | |
| permissions: write-all | |
| jobs: | |
| create-release: | |
| runs-on: ubuntu-latest | |
| env: | |
| GH_TOKEN: ${{ secrets.GITBOT_TOKEN_FOR_RELEASE }} | |
| SETTINGS: | | |
| <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 | |
| http://maven.apache.org/xsd/settings-1.0.0.xsd"> | |
| <profiles> | |
| <profile> | |
| <id>ci-profile</id> | |
| <activation> | |
| <activeByDefault>true</activeByDefault> | |
| </activation> | |
| <repositories> | |
| <repository> | |
| <id>pn-codeartifact-repo</id> | |
| <name>AWS Code Artifact</name> | |
| <url>https://${env.CODEARTIFACT_DOMAIN_FULL}/maven/${env.CODEARTIFACT_REPO}/</url> | |
| </repository> | |
| </repositories> | |
| </profile> | |
| </profiles> | |
| <servers> | |
| <server> | |
| <id>pn-codeartifact</id> | |
| <username>aws</username> | |
| <password>${env.CODEARTIFACT_AUTH_TOKEN}</password> | |
| </server> | |
| </servers> | |
| <mirrors> | |
| <mirror> | |
| <id>pn-codeartifact</id> | |
| <name>pn-codeartifact</name> | |
| <url>https://${env.CODEARTIFACT_DOMAIN_FULL}/maven/${env.CODEARTIFACT_REPO}/</url> | |
| <mirrorOf>*</mirrorOf> | |
| </mirror> | |
| </mirrors> | |
| </settings> | |
| MAVEN_CONFIG: --settings /tmp/maven-settings.xml --no-transfer-progress | |
| CODEARTIFACT_DOMAIN_NAME: ${{ vars.CODEARTIFACT_DOMAIN_NAME }} | |
| CODEARTIFACT_DOMAIN_FULL: ${{ vars.CODEARTIFACT_DOMAIN_FULL }} | |
| CODEARTIFACT_REPO: ${{ vars.CODEARTIFACT_REPO }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| repository: ${{ inputs.repository }} | |
| token: ${{ secrets.GITBOT_TOKEN_FOR_RELEASE }} | |
| ref: ${{ inputs.base_branch }} | |
| fetch-depth: 0 | |
| - name: setup git config | |
| run: | | |
| git config user.name "GitHub Actions Bot" | |
| git config user.email "<>" | |
| - name: create Maven settings.xml | |
| run: | | |
| echo $SETTINGS > /tmp/maven-settings.xml | |
| cat /tmp/maven-settings.xml | |
| - name: Configure AWS Credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws-region: eu-central-1 | |
| role-session-name: GitHubReleaseSession | |
| - name: Set up JDK | |
| uses: actions/setup-java@v4 | |
| with: | |
| distribution: 'corretto' | |
| java-version: '17' | |
| cache: 'maven' | |
| - name: check tag and pom version consistency | |
| run: | | |
| tag=${{ inputs.tag }} | |
| if [[ "$tag" == *"RC"* ]]; then | |
| base_branch=$([ ${{ inputs.base_branch }} = "" ] && echo "develop" || echo "${{ inputs.base_branch }}") | |
| echo "Check consistency from branch: $base_branch" | |
| git checkout $base_branch | |
| else | |
| git checkout develop | |
| git pull origin develop | |
| echo "Creating RELEASE version $tag ..." | |
| release_exists=$(git ls-remote origin release/"$tag" | wc -l) | |
| if [[ $release_exists -eq 1 ]]; then | |
| git checkout release/$tag | |
| git pull origin release/$tag | |
| echo "Check consistency from branch: release/$tag" | |
| else | |
| echo "Branch release/$tag does not exit. Check consistency from branch: develop" | |
| fi | |
| fi | |
| export CODEARTIFACT_AUTH_TOKEN=$( aws codeartifact get-authorization-token --domain $CODEARTIFACT_DOMAIN_NAME --query authorizationToken --output text ) | |
| current_version=$(./mvnw help:evaluate -Dexpression=project.version -q -DforceStdout $MAVEN_CONFIG) | |
| echo "Current version pom: $current_version" | |
| echo "Tag version: $tag" | |
| suffix_snap="-SNAPSHOT" | |
| suffix_rc="-RC" | |
| current_version=${current_version%"$suffix_snap"} | |
| current_version=$(echo "$current_version" | sed "s/$suffix_rc.*//") | |
| echo "Current version pom without suffix: $current_version" | |
| if [[ "$tag" == *"$current_version"* ]]; then | |
| echo "The tag and the pom versions are consistent." | |
| else | |
| echo "The tag and the pom versions are not consistent!" | |
| exit 1 | |
| fi | |
| - name: run RC flow | |
| if: contains(inputs.tag, 'RC') | |
| run: | | |
| export CODEARTIFACT_AUTH_TOKEN=$( aws codeartifact get-authorization-token --domain $CODEARTIFACT_DOMAIN_NAME --query authorizationToken --output text ) | |
| tag=${{ inputs.tag }} | |
| base_branch=$([ ${{ inputs.base_branch }} = "" ] && echo "develop" || echo "${{ inputs.base_branch }}") | |
| # Esegui git fetch | |
| git fetch | |
| # Sposta sul ramo $base_branch e allineati al ramo remoto | |
| git checkout $base_branch | |
| git pull origin $base_branch | |
| echo "Check no snapshots dependencies" | |
| ./mvnw org.apache.maven.plugins:maven-enforcer-plugin:3.3.0:enforce -Denforcer.rules=requireReleaseDeps $MAVEN_CONFIG | |
| # Verifica se il tag contiene "RC" | |
| # Prendi versione corrente del progetto | |
| current_version=$(./mvnw help:evaluate -Dexpression=project.version -q -DforceStdout $MAVEN_CONFIG) | |
| suffix="-SNAPSHOT" | |
| release_branch_name=${current_version%"$suffix"} #esempio, da 0.0.1-SNAPSHOT a 0.0.1 | |
| release_exists=$(git ls-remote origin release/"$release_branch_name" | wc -l) | |
| if [[ $release_exists -eq 1 ]]; then | |
| git checkout release/"$release_branch_name" | |
| git pull origin release/"$release_branch_name" | |
| else | |
| git checkout -b release/"$release_branch_name" | |
| fi | |
| git merge --no-edit $base_branch | |
| # Modifica la versione del pom.xml con il tag dato in input (esempio da 0.0.1-SNAPSHOT a 0.0.1-RC-1) | |
| ./mvnw build-helper:parse-version versions:set -DnewVersion=$tag -U versions:commit $MAVEN_CONFIG | |
| git add pom.xml | |
| git commit -m "[$tag] Modifica versione nel pom.xml" | |
| git push origin release/"$release_branch_name" | |
| # Crea il tag dal ramo base_branch con il prefisso "v" (esempio v0.0.1-RC-1) | |
| git tag -a "v$tag" -m "Release Candidate $tag" | |
| git push origin "v$tag" | |
| - name: run RELEASE flow | |
| if: "!contains(inputs.tag, 'RC')" | |
| run: | | |
| export CODEARTIFACT_AUTH_TOKEN=$( aws codeartifact get-authorization-token --domain $CODEARTIFACT_DOMAIN_NAME --query authorizationToken --output text ) | |
| tag=${{ inputs.tag }} | |
| echo "Creating RELEASE tag: $tag" | |
| # Esegui git fetch | |
| git fetch | |
| # Sposta sul ramo develop e allineati al ramo remoto | |
| git checkout develop | |
| git pull origin develop | |
| echo "Creating RELEASE version $tag ..." | |
| release_exists=$(git ls-remote origin release/"$tag" | wc -l) | |
| if [[ $release_exists -eq 1 ]]; then | |
| git checkout release/$tag | |
| git pull origin release/$tag | |
| else | |
| echo "Branch release/$tag does not exit. Creating it from develop branch..." | |
| git checkout -b release/$tag | |
| fi | |
| echo "Check no snapshots dependencies" | |
| ./mvnw org.apache.maven.plugins:maven-enforcer-plugin:3.3.0:enforce -Denforcer.rules=requireReleaseDeps $MAVEN_CONFIG | |
| echo "Edit pom version to $tag" | |
| ./mvnw build-helper:parse-version versions:set -DnewVersion=$tag -U versions:commit $MAVEN_CONFIG | |
| git add pom.xml | |
| git commit -m "[$tag] Create Release Version" | |
| git push origin release/$tag | |
| merge_in_main=${{ inputs.merge_in_main }} | |
| if [[ $merge_in_main = true ]]; then | |
| git checkout main | |
| git pull origin main | |
| git merge release/$tag | |
| git push origin main | |
| # Crea il tag sul branch main (esempio v0.0.1) | |
| echo "Creating tag v$tag" | |
| gh release create v$tag --generate-notes --target main | |
| else | |
| # Crea il tag sul branch sorgente release (esempio v0.0.1) | |
| echo "Creating tag v$tag" | |
| gh release create v$tag --generate-notes --target release/$tag | |
| fi | |
| - name: run increment SNAPSHOT | |
| if: inputs.increment_snapshot | |
| run: | | |
| export CODEARTIFACT_AUTH_TOKEN=$( aws codeartifact get-authorization-token --domain $CODEARTIFACT_DOMAIN_NAME --query authorizationToken --output text ) | |
| echo "Incrementing patch SNAPSHOT version from develop branch..." | |
| git checkout develop | |
| # Verifica se esiste il branch "next-release" | |
| next_exists=$(git ls-remote origin next-release | wc -l) | |
| if [[ $next_exists -eq 1 ]]; then | |
| git checkout next-release | |
| git pull origin next-release | |
| else | |
| git checkout -b next-release | |
| fi | |
| # Incrementa la versione patch nel pom.xml e pusha sul ramo next-release (esempio da 0.0.1-RC-1 a 0.0.2-SNAPSHOT) | |
| ./mvnw build-helper:parse-version versions:set -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.nextIncrementalVersion}.\0-SNAPSHOT -U versions:commit $MAVEN_CONFIG | |
| next_version=$(./mvnw help:evaluate -Dexpression=project.version -q -DforceStdout $MAVEN_CONFIG) | |
| git add pom.xml | |
| git commit -m "[$next_version] Incrementa versione minor nel pom.xml" | |
| git push origin next-release | |