Skip to content

Commit cc32934

Browse files
author
fgonzalez
committed
feat: new workflow for java services
1 parent 2d27eec commit cc32934

1 file changed

Lines changed: 325 additions & 0 deletions

File tree

Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,325 @@
1+
name: Java 17 Service CI
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
dockerfile-path:
7+
description: "Path to Dockerfile to use for build image"
8+
default: "Dockerfile"
9+
type: string
10+
docker-image-name:
11+
description: "The name of the docker image to delivery"
12+
required: true
13+
type: string
14+
service-name:
15+
description: "The name of the service to deploy"
16+
required: true
17+
type: string
18+
19+
env:
20+
TZ: America/Argentina/Jujuy
21+
MAVEN_USER_HOME: ${{ github.workspace }}/.maven
22+
REGISTRY_URL: 057343230848.dkr.ecr.us-east-1.amazonaws.com
23+
QA_CLUSTER: beplic-qa-eks
24+
PROD_CLUSTER: beplic-prod-eks
25+
NAMESPACE_DEV: beplic-dev
26+
NAMESPACE_QA: beplic-qa
27+
NAMESPACE_PROD: beplic-prod
28+
KAFKA_PACKAGE_NAME: doppler-conversations-generic-kafka-lib
29+
BASE_PACKAGE_NAME: doppler-conversations-base-lib
30+
API_GRAPH_PACKAGE_NAME: doppler-conversations-api-graph-sdk-lib
31+
GH_PACKAGES_OWNER: FromDoppler
32+
ECR_REGION: us-east-1
33+
EKS_REGION: us-east-2
34+
SOURCE_VERSION: "${{ github.ref_name }}+${{ github.sha }}@${{ github.server_url}}/${{ github.repository }}"
35+
IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/${{ inputs.docker-image-name }}
36+
SERVICE: ${{ inputs.service-name }}
37+
38+
jobs:
39+
# sonarqube:
40+
# if: github.ref == 'refs/heads/qa'
41+
# runs-on: ubuntu-latest
42+
# permissions:
43+
# contents: read
44+
# continue-on-error: true
45+
# env:
46+
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
47+
# SONAR_USER_HOME: ${{ runner.temp }}/.sonar
48+
# steps:
49+
# - name: Checkout
50+
# uses: actions/checkout@v4
51+
# with:
52+
# fetch-depth: 0
53+
54+
# - name: Set up Java 17
55+
# uses: actions/setup-java@v4
56+
# with:
57+
# distribution: temurin
58+
# java-version: "17"
59+
# cache: maven
60+
61+
# - name: Cache SonarQube files
62+
# uses: actions/cache@v4
63+
# with:
64+
# path: ${{ env.SONAR_USER_HOME }}/cache
65+
# key: sonar-${{ runner.os }}-${{ hashFiles('**/pom.xml') }}
66+
# restore-keys: |
67+
# sonar-${{ runner.os }}-
68+
69+
# - name: Prepare Maven wrapper
70+
# run: chmod +x mvnw
71+
72+
# - name: Run SonarQube analysis (tests skipped)
73+
# run: ./mvnw -ntp verify sonar:sonar -Dmaven.test.skip=true
74+
75+
maven-package:
76+
runs-on: ubuntu-latest
77+
permissions:
78+
contents: read
79+
packages: read
80+
steps:
81+
- name: Checkout
82+
uses: actions/checkout@v4
83+
with:
84+
fetch-depth: 0
85+
86+
- name: Set up Java 17
87+
uses: actions/setup-java@v4
88+
with:
89+
distribution: temurin
90+
java-version: "17"
91+
cache: maven
92+
93+
- name: Ensure Maven repository folder exists
94+
run: mkdir -p "$MAVEN_USER_HOME"
95+
96+
- name: Cache custom Maven repository
97+
uses: actions/cache@v4
98+
with:
99+
path: ${{ env.MAVEN_USER_HOME }}
100+
key: maven-${{ runner.os }}-${{ hashFiles('**/pom.xml') }}
101+
restore-keys: |
102+
maven-${{ runner.os }}-
103+
104+
- name: Prepare Maven wrapper
105+
run: chmod +x mvnw
106+
107+
- name: Download shared GitHub packages
108+
env:
109+
GH_TOKEN: ${{ secrets.GH_PACKAGES_TOKEN }}
110+
shell: bash
111+
run: |
112+
set -euo pipefail
113+
if [[ -z "${GH_TOKEN:-}" ]]; then
114+
echo "GH_PACKAGES_TOKEN secret is required to download external libraries." >&2
115+
exit 1
116+
fi
117+
118+
download_package() {
119+
local package_name=$1
120+
local label=$2
121+
122+
echo "Resolviendo paquete ${package_name}"
123+
local version_id
124+
version_id=$(gh api "/orgs/${GH_PACKAGES_OWNER}/packages/generic/${package_name}/versions?per_page=1" --jq '.[0].id')
125+
if [[ -z "${version_id}" ]]; then
126+
echo "No se encontró la versión del paquete ${package_name}" >&2
127+
exit 1
128+
fi
129+
130+
local file_info
131+
file_info=$(gh api "/orgs/${GH_PACKAGES_OWNER}/packages/generic/${package_name}/versions/${version_id}/files" --jq '.[0]')
132+
local download_url
133+
download_url=$(jq -r '.download_url' <<<"${file_info}")
134+
local file_name
135+
file_name=$(jq -r '.name' <<<"${file_info}")
136+
137+
if [[ -z "${download_url}" || "${download_url}" == "null" ]]; then
138+
echo "No se pudo resolver la URL de descarga para ${package_name}" >&2
139+
exit 1
140+
fi
141+
142+
mkdir -p external-libs
143+
echo "Descargando ${label} (${file_name})"
144+
gh api --method GET -H "Accept: application/octet-stream" "${download_url}" > "external-libs/${file_name}"
145+
146+
case "${file_name}" in
147+
*.zip)
148+
unzip -o "external-libs/${file_name}" -d .
149+
;;
150+
*.tar|*.tar.gz|*.tgz)
151+
tar -xf "external-libs/${file_name}" -C .
152+
;;
153+
*)
154+
cp "external-libs/${file_name}" "${MAVEN_USER_HOME}/"
155+
;;
156+
esac
157+
}
158+
159+
download_package "${KAFKA_PACKAGE_NAME}" "POC Generic Kafka"
160+
download_package "${BASE_PACKAGE_NAME}" "Beplic Base"
161+
download_package "${API_GRAPH_PACKAGE_NAME}" "Beplic API Graph SDK"
162+
163+
- name: Build and deploy
164+
uses: actions/upload-artifact@v4
165+
with:
166+
name: maven-package
167+
path: |
168+
target/*.jar
169+
target/classes
170+
target/generated-sources
171+
retention-days: 1
172+
173+
docker-hub:
174+
name: Docker image to Docker Hub
175+
runs-on: ubuntu-latest
176+
env:
177+
REGISTRY: docker.io
178+
IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/${{ inputs.docker-image-name }}
179+
steps:
180+
- name: Check out the repo
181+
uses: actions/checkout@v4
182+
183+
- name: Download package artifacts
184+
uses: actions/download-artifact@v4
185+
with:
186+
name: maven-package
187+
path: .
188+
189+
- name: Log in to Docker Hub
190+
uses: docker/login-action@v3
191+
with:
192+
username: ${{ secrets.DOCKERHUB_USERNAME }}
193+
password: ${{ secrets.DOCKERHUB_TOKEN }}
194+
195+
- name: Extract metadata (tags, labels) for Docker
196+
id: meta
197+
uses: docker/metadata-action@v5
198+
with:
199+
images: ${{ env.IMAGE_NAME }}
200+
tags: |
201+
type=semver,pattern={{raw}}
202+
type=semver,pattern=v{{version}}
203+
type=semver,pattern=v{{major}}.{{minor}}
204+
type=semver,pattern=v{{major}}
205+
type=ref,event=branch
206+
- name: Build and push Docker image
207+
uses: docker/build-push-action@v5
208+
with:
209+
context: .
210+
file: ${{ inputs.dockerfile-path }}
211+
push: true
212+
tags: ${{ steps.meta.outputs.tags }}
213+
labels: ${{ steps.meta.outputs.labels }}
214+
build-args: |
215+
version=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.SOURCE_VERSION }}
216+
FROMDOPPLER_GITHUB_PACKAGES_TOKEN=${{ secrets.GITHUB_TOKEN }}
217+
218+
deploy-dev:
219+
runs-on: ubuntu-latest
220+
needs:
221+
- docker-push
222+
if: github.ref == 'refs/heads/develop'
223+
environment:
224+
name: dev
225+
url: http
226+
steps:
227+
- name: Configure kubeconfig
228+
shell: bash
229+
env:
230+
DEV_KUBECONFIG: ${{ secrets.KUBECONFIG_DEV }}
231+
run: |
232+
set -euo pipefail
233+
if [[ -z "${DEV_KUBECONFIG:-}" ]]; then
234+
echo "Se requiere el secreto KUBECONFIG_DEV para desplegar en dev." >&2
235+
exit 1
236+
fi
237+
mkdir -p ~/.kube
238+
printf '%s' "${DEV_KUBECONFIG}" | base64 --decode > ~/.kube/config
239+
240+
- name: Rollout to dev cluster
241+
env:
242+
REF_NAME: ${{ needs.docker-push.outputs.branch_name }}
243+
SHORT_SHA: ${{ needs.docker-push.outputs.short_sha }}
244+
shell: bash
245+
run: |
246+
set -euo pipefail
247+
if [[ -z "${SERVICE}" ]]; then
248+
echo "La variable SERVICE es obligatoria." >&2
249+
exit 1
250+
fi
251+
for service in ${SERVICE}; do
252+
echo "Actualizando ${service} en ${NAMESPACE_DEV}"
253+
kubectl set image deploy/${service} ${service}=${REGISTRY_URL}/${IMAGE_NAME}:${REF_NAME}-${SHORT_SHA} --namespace ${NAMESPACE_DEV}
254+
kubectl rollout status deploy/${service} -n ${NAMESPACE_DEV}
255+
done
256+
257+
deploy-qa:
258+
runs-on: ubuntu-latest
259+
needs:
260+
- docker-push
261+
if: github.ref == 'refs/heads/qa'
262+
environment:
263+
name: qa
264+
steps:
265+
- name: Configure AWS credentials (QA)
266+
uses: aws-actions/configure-aws-credentials@v4
267+
with:
268+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_K8S_QA }}
269+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_K8S_QA }}
270+
aws-region: ${{ env.EKS_REGION }}
271+
272+
- name: Update kubeconfig
273+
run: aws eks update-kubeconfig --name ${QA_CLUSTER} --region ${EKS_REGION} --alias qa
274+
275+
- name: Rollout to QA cluster
276+
env:
277+
REF_NAME: ${{ needs.docker-push.outputs.branch_name }}
278+
SHORT_SHA: ${{ needs.docker-push.outputs.short_sha }}
279+
shell: bash
280+
run: |
281+
set -euo pipefail
282+
if [[ -z "${SERVICE}" ]]; then
283+
echo "La variable SERVICE es obligatoria." >&2
284+
exit 1
285+
fi
286+
for service in ${SERVICE}; do
287+
echo "Actualizando ${service} en ${NAMESPACE_QA}"
288+
kubectl set image deploy/${service} ${service}=${REGISTRY_URL}/${IMAGE_NAME}:${REF_NAME}-${SHORT_SHA} --namespace ${NAMESPACE_QA}
289+
kubectl rollout status deploy/${service} -n ${NAMESPACE_QA}
290+
done
291+
292+
deploy-prod:
293+
runs-on: ubuntu-latest
294+
needs:
295+
- docker-push
296+
if: github.ref == 'refs/heads/master'
297+
environment:
298+
name: prod
299+
steps:
300+
- name: Configure AWS credentials (prod)
301+
uses: aws-actions/configure-aws-credentials@v4
302+
with:
303+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_K8S_PROD }}
304+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_K8S_PROD }}
305+
aws-region: ${{ env.EKS_REGION }}
306+
307+
- name: Update kubeconfig
308+
run: aws eks update-kubeconfig --name ${PROD_CLUSTER} --region ${EKS_REGION} --alias prod
309+
310+
- name: Rollout to prod cluster
311+
env:
312+
REF_NAME: ${{ needs.docker-push.outputs.branch_name }}
313+
SHORT_SHA: ${{ needs.docker-push.outputs.short_sha }}
314+
shell: bash
315+
run: |
316+
set -euo pipefail
317+
if [[ -z "${SERVICE}" ]]; then
318+
echo "La variable SERVICE es obligatoria." >&2
319+
exit 1
320+
fi
321+
for service in ${SERVICE}; do
322+
echo "Actualizando ${service} en ${NAMESPACE_PROD}"
323+
kubectl set image deploy/${service} ${service}=${REGISTRY_URL}/${IMAGE_NAME}:${REF_NAME}-${SHORT_SHA} --namespace ${NAMESPACE_PROD}
324+
kubectl rollout status deploy/${service} -n ${NAMESPACE_PROD}
325+
done

0 commit comments

Comments
 (0)