Skip to content

Commit 1c33212

Browse files
authored
Refactor GitHub Actions Workflows (#168)
Don't rely on shell scripts from build-tools anymore. Main build.yml can be run on forks, as it doesn't need any secrets. Use two additional workflows for snapshots and releases. Similar to pmd/build-tools#68 Refs pmd/pmd#4328
2 parents eb70e14 + e81b63a commit 1c33212

File tree

3 files changed

+272
-42
lines changed

3 files changed

+272
-42
lines changed

.github/workflows/build.yml

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,53 @@
1-
name: build
1+
name: Build
22
on:
3+
pull_request:
4+
merge_group:
35
push:
46
branches:
5-
- main
7+
- '**'
8+
# don't run on dependabot branches. Dependabot will create pull requests, which will then be run instead
9+
- '!dependabot/**'
610
tags:
711
- '**'
8-
pull_request:
12+
workflow_dispatch:
913
schedule:
1014
# build it monthly: At 05:00 on day-of-month 1.
1115
- cron: '0 5 1 * *'
12-
workflow_dispatch:
16+
17+
# if another commit is added to the same branch or PR (same github.ref),
18+
# then cancel already running jobs and start a new build.
19+
concurrency:
20+
group: ${{ github.workflow }}-${{ github.ref }}
21+
cancel-in-progress: true
22+
23+
permissions:
24+
contents: read # to fetch code (actions/checkout)
25+
26+
env:
27+
LANG: 'en_US.UTF-8'
1328

1429
jobs:
15-
build:
30+
compile:
1631
runs-on: ubuntu-latest
17-
continue-on-error: false
18-
if: ${{ !contains(github.event.head_commit.message, '[skip ci]') }}
32+
timeout-minutes: 10
33+
defaults:
34+
run:
35+
shell: bash
1936
steps:
20-
- uses: actions/checkout@v4
21-
- uses: actions/cache@v4
22-
with:
23-
path: |
24-
~/.m2/repository
25-
~/.cache
26-
key: ${{ runner.os }}-${{ hashFiles('**/pom.xml') }}
27-
restore-keys: |
28-
${{ runner.os }}-
29-
- name: Set up Ruby 3.3
30-
uses: ruby/setup-ruby@v1
31-
with:
32-
ruby-version: 3.3
33-
- name: Setup Environment
34-
shell: bash
35-
run: |
36-
echo "LANG=en_US.UTF-8" >> $GITHUB_ENV
37-
echo "MAVEN_OPTS=-Dmaven.wagon.httpconnectionManager.ttlSeconds=180 -Dmaven.wagon.http.retryHandler.count=3" >> $GITHUB_ENV
38-
echo "PMD_CI_SCRIPTS_URL=https://raw.githubusercontent.com/pmd/build-tools/30/scripts" >> $GITHUB_ENV
39-
- name: Check Environment
40-
shell: bash
41-
run: |
42-
f=check-environment.sh; \
43-
mkdir -p .ci && \
44-
( [ -e .ci/$f ] || curl -sSL "${PMD_CI_SCRIPTS_URL}/$f" > ".ci/$f" ) && \
45-
chmod 755 .ci/$f && \
46-
.ci/$f
47-
- name: Build
48-
run: .ci/build.sh
49-
shell: bash
50-
env:
51-
PMD_CI_SECRET_PASSPHRASE: ${{ secrets.PMD_CI_SECRET_PASSPHRASE }}
52-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
53-
PMD_CI_GPG_PRIVATE_KEY: ${{ secrets.PMD_CI_GPG_PRIVATE_KEY }}
54-
MAVEN_GPG_PASSPHRASE: ${{ secrets.PMD_CI_GPG_PASSPHRASE }}
37+
- uses: actions/checkout@v4
38+
- uses: actions/setup-java@v4
39+
with:
40+
distribution: 'temurin'
41+
java-version: '11'
42+
cache: 'maven'
43+
- name: Build with Maven
44+
run: |
45+
./mvnw --show-version --errors --batch-mode \
46+
-Pshading \
47+
verify
48+
- uses: actions/upload-artifact@v4
49+
with:
50+
name: compile-artifact
51+
if-no-files-found: error
52+
path: |
53+
target/pmd-designer-*.jar
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
name: Publish Release
2+
3+
on:
4+
workflow_run:
5+
workflows: [Build]
6+
types:
7+
- completed
8+
branches:
9+
- '**'
10+
- '!main'
11+
- '!dependabot/**'
12+
13+
permissions:
14+
contents: read # to fetch code (actions/checkout)
15+
16+
env:
17+
LANG: 'en_US.UTF-8'
18+
19+
jobs:
20+
check-version:
21+
# only run in the official pmd/pmd-designer repo, where we have access to the secrets and not on forks
22+
# and only run for _successful_ push workflow runs on tags.
23+
if: ${{ github.repository == 'pmd/designer'
24+
&& contains(fromJSON('["push", "workflow_dispatch"]'), github.event.workflow_run.event)
25+
&& github.event.workflow_run.head_branch != 'main'
26+
&& github.event.workflow_run.conclusion == 'success' }}
27+
runs-on: ubuntu-latest
28+
timeout-minutes: 10
29+
defaults:
30+
run:
31+
shell: bash
32+
outputs:
33+
VERSION: ${{ steps.version.outputs.VERSION }}
34+
steps:
35+
- uses: actions/checkout@v4
36+
with:
37+
ref: ${{ github.event.workflow_run.head_branch }}
38+
- uses: actions/setup-java@v4
39+
with:
40+
distribution: 'temurin'
41+
java-version: '11'
42+
cache: 'maven'
43+
- name: Determine Version
44+
id: version
45+
env:
46+
REF: ${{ github.event.workflow_run.head_branch }}
47+
run: |
48+
if ! git show-ref --exists "refs/tags/$REF"; then
49+
echo "::error ::Tag $REF does not exist, aborting."
50+
exit 1
51+
fi
52+
53+
VERSION=$(./mvnw --batch-mode --no-transfer-progress help:evaluate -Dexpression=project.version -q -DforceStdout)
54+
echo "Determined VERSION=$VERSION"
55+
if [[ "$VERSION" = *-SNAPSHOT ]]; then
56+
echo "::error ::VERSION=$VERSION is a snapshot version, aborting."
57+
exit 1
58+
fi
59+
echo "VERSION=$VERSION" >> "$GITHUB_OUTPUT"
60+
- name: Add Job Summary
61+
env:
62+
WORKFLOW_RUN_DISPLAY_TITLE: ${{ github.event.workflow_run.display_title }}
63+
WORKFLOW_RUN_NAME: ${{ github.event.workflow_run.name }}
64+
WORKFLOW_RUN_NUMBER: ${{ github.event.workflow_run.run_number }}
65+
WORKFLOW_RUN_HTML_URL: ${{ github.event.workflow_run.html_url }}
66+
VERSION: ${{ steps.version.outputs.VERSION }}
67+
TAG: ${{ github.event.workflow_run.head_branch }}
68+
run: |
69+
echo "### Run Info" >> "${GITHUB_STEP_SUMMARY}"
70+
echo "Building Version: ${VERSION}" >> "${GITHUB_STEP_SUMMARY}"
71+
echo "" >> "${GITHUB_STEP_SUMMARY}"
72+
echo "Tag: ${TAG}" >> "${GITHUB_STEP_SUMMARY}"
73+
echo "" >> "${GITHUB_STEP_SUMMARY}"
74+
echo "Called by [${WORKFLOW_RUN_DISPLAY_TITLE} (${WORKFLOW_RUN_NAME} #${WORKFLOW_RUN_NUMBER})](${WORKFLOW_RUN_HTML_URL})" >> "${GITHUB_STEP_SUMMARY}"
75+
echo "" >> "${GITHUB_STEP_SUMMARY}"
76+
77+
deploy-to-maven-central:
78+
needs: check-version
79+
# use environment maven-central, where secrets are configured for OSSRH_*
80+
environment:
81+
name: maven-central
82+
url: https://repo.maven.apache.org/maven2/net/sourceforge/pmd/pmd-designer/
83+
runs-on: ubuntu-latest
84+
timeout-minutes: 20
85+
permissions:
86+
contents: write # to create a new release
87+
defaults:
88+
run:
89+
shell: bash
90+
steps:
91+
- uses: actions/checkout@v4
92+
with:
93+
ref: ${{ github.event.workflow_run.head_branch }}
94+
- uses: actions/setup-java@v4
95+
with:
96+
distribution: 'temurin'
97+
java-version: '11'
98+
cache: 'maven'
99+
server-id: ossrh
100+
server-username: MAVEN_USERNAME
101+
server-password: MAVEN_PASSWORD
102+
gpg-passphrase: MAVEN_GPG_PASSPHRASE
103+
gpg-private-key: ${{ secrets.PMD_CI_GPG_PRIVATE_KEY }}
104+
- name: Build and Publish
105+
env:
106+
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
107+
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
108+
MAVEN_GPG_PASSPHRASE: ${{ secrets.PMD_CI_GPG_PASSPHRASE }}
109+
run: |
110+
./mvnw --show-version --errors --batch-mode \
111+
-Psign,shading \
112+
deploy
113+
- name: Prepare Release Notes
114+
run: |
115+
BEGIN_LINE=$(grep -n "^## " CHANGELOG.md|head -1|cut -d ":" -f 1)
116+
BEGIN_LINE=$((BEGIN_LINE + 1))
117+
END_LINE=$(grep -n "^## " CHANGELOG.md|head -2|tail -1|cut -d ":" -f 1)
118+
END_LINE=$((END_LINE - 1))
119+
RELEASE_BODY="$(head -$END_LINE CHANGELOG.md | tail -$((END_LINE - BEGIN_LINE)))"
120+
echo "$RELEASE_BODY" > release_notes.md
121+
- name: Create Release
122+
env:
123+
TAG_NAME: ${{ github.event.workflow_run.head_branch }}
124+
VERSION: ${{ needs.check-version.outputs.VERSION }}
125+
run: |
126+
# Note: The release asset is the shaded jar
127+
gh release create "$TAG_NAME" "target/pmd-designer-${VERSION}.jar" \
128+
--verify-tag \
129+
--notes-file release_notes.md \
130+
--title "$VERSION"
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
name: Publish Snapshot
2+
3+
on:
4+
workflow_run:
5+
workflows: [Build]
6+
types:
7+
- completed
8+
branches:
9+
- main
10+
11+
permissions:
12+
contents: read # to fetch code (actions/checkout)
13+
14+
env:
15+
LANG: 'en_US.UTF-8'
16+
17+
jobs:
18+
check-version:
19+
# only run in the official pmd/pmd-designer repo, where we have access to the secrets and not on forks
20+
# and only run for _successful_ push workflow runs on branch "main".
21+
if: ${{ github.repository == 'pmd/pmd-designer'
22+
&& contains(fromJSON('["push", "workflow_dispatch", "schedule"]'), github.event.workflow_run.event)
23+
&& github.event.workflow_run.head_branch == 'main'
24+
&& github.event.workflow_run.conclusion == 'success' }}
25+
runs-on: ubuntu-latest
26+
timeout-minutes: 10
27+
defaults:
28+
run:
29+
shell: bash
30+
outputs:
31+
VERSION: ${{ steps.version.outputs.VERSION }}
32+
steps:
33+
- uses: actions/checkout@v4
34+
with:
35+
ref: main
36+
- uses: actions/setup-java@v4
37+
with:
38+
distribution: 'temurin'
39+
java-version: '11'
40+
cache: 'maven'
41+
- name: Determine Version
42+
id: version
43+
run: |
44+
VERSION=$(./mvnw --batch-mode --no-transfer-progress help:evaluate -Dexpression=project.version -q -DforceStdout)
45+
echo "Determined VERSION=$VERSION"
46+
if [[ "$VERSION" != *-SNAPSHOT ]]; then
47+
echo "::error ::VERSION=$VERSION is not a snapshot version, aborting."
48+
exit 1
49+
fi
50+
echo "VERSION=$VERSION" >> "$GITHUB_OUTPUT"
51+
- name: Add Job Summary
52+
env:
53+
WORKFLOW_RUN_DISPLAY_TITLE: ${{ github.event.workflow_run.display_title }}
54+
WORKFLOW_RUN_NAME: ${{ github.event.workflow_run.name }}
55+
WORKFLOW_RUN_NUMBER: ${{ github.event.workflow_run.run_number }}
56+
WORKFLOW_RUN_HTML_URL: ${{ github.event.workflow_run.html_url }}
57+
VERSION: ${{ steps.version.outputs.VERSION }}
58+
BRANCH: ${{ github.event.workflow_run.head_branch }}
59+
run: |
60+
echo "### Run Info" >> "${GITHUB_STEP_SUMMARY}"
61+
echo "Building Version: ${VERSION}" >> "${GITHUB_STEP_SUMMARY}"
62+
echo "" >> "${GITHUB_STEP_SUMMARY}"
63+
echo "Branch: ${BRANCH}" >> "${GITHUB_STEP_SUMMARY}"
64+
echo "" >> "${GITHUB_STEP_SUMMARY}"
65+
echo "Called by [${WORKFLOW_RUN_DISPLAY_TITLE} (${WORKFLOW_RUN_NAME} #${WORKFLOW_RUN_NUMBER})](${WORKFLOW_RUN_HTML_URL})" >> "${GITHUB_STEP_SUMMARY}"
66+
echo "" >> "${GITHUB_STEP_SUMMARY}"
67+
68+
deploy-to-maven-central:
69+
needs: check-version
70+
# use environment maven-central, where secrets are configured for OSSRH_*
71+
environment:
72+
name: maven-central
73+
url: https://oss.sonatype.org/content/repositories/snapshots/net/sourceforge/pmd/pmd-designer/
74+
runs-on: ubuntu-latest
75+
timeout-minutes: 20
76+
defaults:
77+
run:
78+
shell: bash
79+
steps:
80+
- uses: actions/checkout@v4
81+
with:
82+
ref: main
83+
- uses: actions/setup-java@v4
84+
with:
85+
distribution: 'temurin'
86+
java-version: '11'
87+
cache: 'maven'
88+
server-id: ossrh
89+
server-username: MAVEN_USERNAME
90+
server-password: MAVEN_PASSWORD
91+
gpg-passphrase: MAVEN_GPG_PASSPHRASE
92+
gpg-private-key: ${{ secrets.PMD_CI_GPG_PRIVATE_KEY }}
93+
- name: Build and Publish
94+
env:
95+
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
96+
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
97+
MAVEN_GPG_PASSPHRASE: ${{ secrets.PMD_CI_GPG_PASSPHRASE }}
98+
run: |
99+
./mvnw --show-version --errors --batch-mode \
100+
-Psign,shading \
101+
deploy

0 commit comments

Comments
 (0)