Skip to content
name: Build and Publish Library
on:
pull_request:
types: [ closed ]
branches: [ develop, master ]
permissions:
contents: write
packages: write
checks: write
pull-requests: write
jobs:
build:
runs-on: ubuntu-latest
if: |
github.event.pull_request.merged == true &&
(
(github.base_ref == 'develop' && startsWith(github.event.pull_request.title, 'NEW_RELEASE')) ||
(github.base_ref == 'master' && github.head_ref == 'develop')
)
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Necesario para obtener todos los tags
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
cache: 'gradle'
- name: Setup Git
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Cache Gradle packages
uses: actions/cache@v3
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle
- name: Build with Gradle
uses: gradle/gradle-build-action@v2
with:
arguments: build
- name: Archive build artifacts
uses: actions/upload-artifact@v3
with:
name: build-artifacts
path: build/libs
publish:
needs: build
runs-on: ubuntu-latest
if: |
success() &&
github.event.pull_request.merged == true &&
(
(github.base_ref == 'develop' && startsWith(github.event.pull_request.title, 'NEW_RELEASE')) ||
(github.base_ref == 'master' && github.head_ref == 'develop')
)
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Necesario para obtener todos los tags
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Download build artifacts
uses: actions/download-artifact@v3
with:
name: build-artifacts
path: build/libs
- name: Setup Git
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
- name: Determine version
id: determine_version
run: |
if [[ "${{ github.base_ref }}" == "develop" ]]; then
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
MAJOR=$(echo $LAST_TAG | cut -d. -f1)
MINOR=$(echo $LAST_TAG | cut -d. -f2)
PATCH=$(echo $LAST_TAG | cut -d. -f3)
NEW_VERSION="${MAJOR}.${MINOR}.$((PATCH + 1))"
echo "version=${NEW_VERSION}-SNAPSHOT" >> $GITHUB_OUTPUT
echo "env=development" >> $GITHUB_OUTPUT
elif [[ "${{ github.base_ref }}" == "master" ]]; then
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
VERSION=${LAST_TAG#v}
MAJOR=$(echo $VERSION | cut -d. -f1)
MINOR=$(echo $VERSION | cut -d. -f2)
PATCH=$(echo $VERSION | cut -d. -f3)
NEW_VERSION="${MAJOR}.${MINOR}.$((PATCH + 1))"
echo "version=${NEW_VERSION}" >> $GITHUB_OUTPUT
echo "env=production" >> $GITHUB_OUTPUT
fi
- name: Display version
run: |
echo "Version: ${{ steps.determine_version.outputs.version }}"
echo "Environment: ${{ steps.determine_version.outputs.env }}"
- name: Publish to GitHub Packages
uses: gradle/gradle-build-action@v2
with:
arguments: build publish
env:
VERSION: ${{ steps.determine_version.outputs.version }}
USERNAME: ${{ github.actor }}
TOKEN: ${{ secrets.GITHUB_TOKEN }}
ENVIRONMENT: ${{ steps.determine_version.outputs.env }}
- name: Create and push tag(for master)
if: github.base_ref == 'master'
run: |
git tag v${{ steps.determine_version.outputs.version }}
git push origin v${{ steps.determine_version.outputs.version }}
- name: Create Release(for master)
if: github.base_ref == 'master'
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.determine_version.outputs.version }}
release_name: Release ${{ steps.determine_version.outputs.version }}
body: |
Changes in this Release
- Merged from develop to master
- PR: #${{ github.event.pull_request.number }}
draft: false
prerelease: false
cleanup-packages:
needs: publish
runs-on: ubuntu-latest
if: github.event.pull_request.merged == true && (github.base_ref == 'master' || github.base_ref == 'develop')
steps:
- name: Delete old packages
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Definir el nombre del paquete y el número de versiones a mantener
PACKAGE_NAME="es.zyrcled.cloud_vision_connection_lib"
VERSIONS_TO_KEEP_PROD=5
VERSIONS_TO_KEEP_DEV=3
# Función para eliminar versiones antiguas
delete_old_versions() {
local versions_to_keep=$1
local version_filter=$2
PACKAGE_VERSIONS=$(gh api \
-H "Accept: application/vnd.github+json" \
/user/packages/maven/$PACKAGE_NAME/versions \
--jq ".[] | select(.name | $version_filter) | .id")
echo "$PACKAGE_VERSIONS" | sort -rn | tail -n +$((versions_to_keep + 1)) | while read VERSION_ID; do
echo "Deleting version $VERSION_ID"
gh api \
--method DELETE \
-H "Accept: application/vnd.github+json" \
/user/packages/maven/$PACKAGE_NAME/versions/$VERSION_ID
done
}
# Eliminar versiones antiguas de producción
if [[ "${{ github.base_ref }}" == "master" ]]; then
echo "Cleaning up production versions"
delete_old_versions $VERSIONS_TO_KEEP_PROD "test(\"^[0-9]+\\\\.[0-9]+\\\\.[0-9]+$\")"
fi
# Eliminar versiones antiguas de desarrollo
if [[ "${{ github.base_ref }}" == "develop" ]]; then
echo "Cleaning up development versions"
delete_old_versions $VERSIONS_TO_KEEP_DEV "contains(\"-SNAPSHOT\")"
fi