-
Notifications
You must be signed in to change notification settings - Fork 1
170 lines (145 loc) · 5.34 KB
/
build-and-publish_library.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
name: Build and Publish Library
on:
push:
branches:
- master
permissions:
contents: write
packages: write
checks: write
pull-requests: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Necesario para obtener todos los tags
- name: Set up JDK 21
uses: actions/setup-java@v4
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
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Necesario para obtener todos los tags
- name: Set up JDK 21
uses: actions/setup-java@v4
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: |
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}" >> $GITHUB_OUTPUT
echo "env=prod" >> $GITHUB_OUTPUT
- 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)
run: |
git tag v${{ steps.determine_version.outputs.version }}
git push origin v${{ steps.determine_version.outputs.version }}
- name: Create Release(for 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
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="dev.root101.commons"
VERSIONS_TO_KEEP_PROD=10
VERSIONS_TO_KEEP_DEV=5
# 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