Skip to content

Commit 3ae184c

Browse files
committed
ci: update workflow
1 parent 37ddfa4 commit 3ae184c

File tree

10 files changed

+406
-117
lines changed

10 files changed

+406
-117
lines changed

.github/workflows/cd-deploy.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: Publish package to the Maven Central Repository
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
environment:
7+
description: Environment
8+
type: string
9+
required: true
10+
11+
jobs:
12+
publish:
13+
environment: ${{ inputs.environment }}
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Set up JDK
20+
uses: actions/setup-java@v4
21+
with:
22+
java-version: 8
23+
distribution: 'corretto'
24+
server-id: sonatype-central
25+
server-username: OSSRH_USERNAME
26+
server-password: OSSRH_TOKEN
27+
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
28+
29+
- name: Cache Maven packages
30+
uses: actions/cache@v4
31+
with:
32+
path: ~/.m2
33+
key: ${{ runner.os }}-deploy-${{ hashFiles('**/pom.xml') }}
34+
restore-keys: ${{ runner.os }}-deploy-${{ hashFiles('**/pom.xml') }}
35+
36+
- name: Configure Git user
37+
run: |
38+
git config user.email "actions@github.com"
39+
git config user.name "GitHub Actions"
40+
41+
- name: Publish Snapshot to the Maven Central Repository
42+
if: ${{ inputs.environment == 'snapshot' }}
43+
run: |
44+
mvn deploy -P snapshot
45+
env:
46+
MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
47+
OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }}
48+
OSSRH_TOKEN: ${{ secrets.OSSRH_TOKEN }}
49+
50+
- id: release-vars
51+
if: ${{ inputs.environment == 'release' }}
52+
name: Set output variables
53+
run: |
54+
RELEASE_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout | grep -e '^[^\[]' | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+')
55+
RELEASE_TAG=v${RELEASE_VERSION}
56+
57+
echo "release-version=$RELEASE_VERSION" >> $GITHUB_OUTPUT
58+
echo "release-tag=$RELEASE_TAG" >> $GITHUB_OUTPUT
59+
60+
- name: Publish Release to the Maven Central Repository
61+
if: ${{ inputs.environment == 'release' }}
62+
run: |
63+
mvn -B release:prepare -P release
64+
git push origin tag $RELEASE_TAG
65+
mvn -B release:perform -P release
66+
env:
67+
MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
68+
OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }}
69+
OSSRH_TOKEN: ${{ secrets.OSSRH_TOKEN }}
70+
RELEASE_VERSION: ${{ steps.release-vars.outputs.release-version }}
71+
RELEASE_TAG: ${{ steps.release-vars.outputs.release-tag }}
72+
73+
- name: Create Release
74+
if: ${{ inputs.environment == 'release' }}
75+
run: |
76+
gh release create ${RELEASE_TAG} --generate-notes
77+
env:
78+
RELEASE_TAG: ${{ steps.release-vars.outputs.release-tag }}
79+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Build, Gates and Pull Request
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- develop
7+
types:
8+
- closed
9+
10+
permissions:
11+
contents: write
12+
pull-requests: write
13+
14+
jobs:
15+
variables:
16+
if: ${{ github.event.pull_request.merged == true }}
17+
runs-on: ubuntu-latest
18+
outputs:
19+
version: ${{ steps.environment.outputs.version }}
20+
target-branch: ${{ steps.environment.outputs.target-branch }}
21+
steps:
22+
- name: Checkout repository
23+
uses: actions/checkout@v4
24+
25+
- name: Set up JDK
26+
uses: actions/setup-java@v4
27+
with:
28+
java-version: 17
29+
distribution: "corretto"
30+
cache: "maven"
31+
32+
- id: environment
33+
name: Set output environment passed to the reusable workflow
34+
run: |
35+
VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout | grep -e '^[^\[]' | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+')
36+
echo "version=$VERSION" >> $GITHUB_OUTPUT
37+
echo "target-branch=release/$VERSION" >> $GITHUB_OUTPUT
38+
39+
ci:
40+
needs: variables
41+
uses: ./.github/workflows/ci-maven.yml
42+
secrets: inherit
43+
44+
gates:
45+
needs: ci
46+
if: success()
47+
uses: ./.github/workflows/ci-gates.yml
48+
secrets: inherit
49+
50+
pull-request:
51+
needs: [gates, variables]
52+
uses: ./.github/workflows/ci-pull-request.yml
53+
secrets: inherit
54+
with:
55+
type: Snapshot
56+
labels: automatic,snapshot
57+
source-branch: master
58+
target-branch: ${{ needs.variables.outputs.target-branch }}

.github/workflows/cd-release.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Build and Publish Release
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
types:
8+
- closed
9+
10+
permissions:
11+
contents: write
12+
pull-requests: write
13+
14+
jobs:
15+
ci:
16+
if: ${{ github.event.pull_request.merged == true }}
17+
uses: ./.github/workflows/ci-maven.yml
18+
secrets: inherit
19+
20+
release:
21+
needs: ci
22+
uses: ./.github/workflows/cd-deploy.yml
23+
secrets: inherit
24+
with:
25+
environment: release

.github/workflows/cd-snapshot.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Build, Publish Snapshot and Pull Request
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- release/**
7+
types:
8+
- closed
9+
10+
permissions:
11+
contents: write
12+
pull-requests: write
13+
14+
jobs:
15+
ci:
16+
if: ${{ github.event.pull_request.merged == true }}
17+
uses: ./.github/workflows/ci-maven.yml
18+
secrets: inherit
19+
20+
snapshot:
21+
needs: ci
22+
uses: ./.github/workflows/cd-deploy.yml
23+
secrets: inherit
24+
with:
25+
environment: snapshot
26+
27+
pull-request:
28+
needs: snapshot
29+
uses: ./.github/workflows/ci-pull-request.yml
30+
secrets: inherit
31+
with:
32+
type: Release
33+
labels: automatic,release
34+
source-branch: master
35+
target-branch: master

.github/workflows/ci-gates.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: SonarCloud
2+
3+
on:
4+
workflow_call:
5+
6+
jobs:
7+
sonarcloud:
8+
environment: sonarcloud
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout repository
12+
uses: actions/checkout@v4
13+
with:
14+
fetch-depth: 0
15+
16+
- name: Set up JDK 17
17+
uses: actions/setup-java@v4
18+
with:
19+
java-version: 17
20+
distribution: "corretto"
21+
cache: "maven"
22+
23+
- name: Cache Maven packages
24+
uses: actions/cache@v4
25+
with:
26+
path: |
27+
~/.sonar/cache
28+
~/.m2
29+
key: ${{ runner.os }}-sonar-${{ hashFiles('**/pom.xml') }}
30+
restore-keys: ${{ runner.os }}-sonar-${{ hashFiles('**/pom.xml') }}
31+
32+
- name: Build and analyze
33+
run: mvn -B verify sonar:sonar -P sonar
34+
env:
35+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

.github/workflows/ci-maven.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Java CI with Maven
2+
3+
on:
4+
workflow_call:
5+
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
java-version: [8, 11, 17, 21]
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v4
15+
16+
- name: Set up JDK
17+
uses: actions/setup-java@v4
18+
with:
19+
java-version: ${{ matrix.java-version }}
20+
distribution: "corretto"
21+
cache: "maven"
22+
23+
- name: Cache Maven packages
24+
uses: actions/cache@v4
25+
with:
26+
path: ~/.m2
27+
key: ${{ runner.os }}-build-${{ hashFiles('**/pom.xml') }}-${{ matrix.java-version }}
28+
restore-keys: ${{ runner.os }}-build-${{ hashFiles('**/pom.xml') }}-${{ matrix.java-version }}
29+
30+
- name: Compile with Maven
31+
run: mvn -T 2C clean generate-sources compile --file pom.xml
32+
33+
test:
34+
runs-on: ubuntu-latest
35+
strategy:
36+
matrix:
37+
java-version: [8, 11, 17, 21]
38+
steps:
39+
- name: Checkout repository
40+
uses: actions/checkout@v4
41+
42+
- name: Set up JDK
43+
uses: actions/setup-java@v4
44+
with:
45+
java-version: ${{ matrix.java-version }}
46+
distribution: "corretto"
47+
cache: "maven"
48+
49+
- name: Cache Maven packages
50+
uses: actions/cache@v4
51+
with:
52+
path: ~/.m2
53+
key: ${{ runner.os }}-test-${{ hashFiles('**/pom.xml') }}-${{ matrix.java-version }}
54+
restore-keys: ${{ runner.os }}-test-${{ hashFiles('**/pom.xml') }}-${{ matrix.java-version }}
55+
56+
- name: Test with Maven
57+
run: mvn -T 2C test-compile test --file pom.xml
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Create Pull Request
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
source-branch:
7+
description: Source branch
8+
type: string
9+
required: true
10+
target-branch:
11+
description: Target branch
12+
type: string
13+
required: true
14+
labels:
15+
description: Labels to PR
16+
type: string
17+
required: true
18+
type:
19+
description: PR Type
20+
type: string
21+
required: true
22+
23+
jobs:
24+
open-pr:
25+
runs-on: ubuntu-latest
26+
steps:
27+
- name: Checkout repository
28+
uses: actions/checkout@v4
29+
30+
- name: Create branch
31+
run: |
32+
REPO="${{ github.repository }}"
33+
BRANCH="${{ inputs.target-branch }}"
34+
BASE="${{ inputs.source-branch }}"
35+
36+
echo "Checking if branch '$BRANCH' exists in repository '$REPO'..."
37+
38+
if gh api "repos/$REPO/branches/$BRANCH" --silent >/dev/null 2>&1; then
39+
echo "Branch '$BRANCH' already exists."
40+
else
41+
echo "Branch '$BRANCH' does not exist. It will be created from '$BASE'."
42+
43+
BASE_SHA=$(gh api "repos/$REPO/git/ref/heads/$BASE" --jq .object.sha)
44+
45+
if [ -z "$BASE_SHA" ]; then
46+
echo "Error: Could not retrieve the SHA of base branch '$BASE'"
47+
exit 1
48+
fi
49+
50+
gh api --method POST "repos/$REPO/git/refs" \
51+
-f ref="refs/heads/$BRANCH" \
52+
-f sha="$BASE_SHA"
53+
54+
echo "Branch '$BRANCH' successfully created!"
55+
fi
56+
env:
57+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
58+
59+
- name: Create Pull Request (${{ inputs.type }})
60+
uses: peter-evans/create-pull-request@v7
61+
with:
62+
title: Auto-created pull request into `${{ inputs.target-branch }}` from `${{ github.ref_name }}`
63+
token: ${{ secrets.GITHUB_TOKEN }}
64+
committer: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
65+
author: ${{ github.actor }} <${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com>
66+
commit-message: Auto Pull Request (${{ inputs.type }})
67+
body: Auto-created Pull Request
68+
branch: ${{ github.ref }}
69+
base: ${{ inputs.target-branch }}
70+
labels: ${{ inputs.labels }}
71+
assignees: ${{ github.actor }}
72+
reviewers: mvallim

0 commit comments

Comments
 (0)