Slide 1

Slide 1 text

Automatisiere deine Prozesse mit GitHub Actions! Alexander Schwartz, Principal Software Engineer @ Red Hat Java User Group Hamburg | 2024-06-12

Slide 2

Slide 2 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 2 Automatisiere deine Prozesse mit GitHub Actions! Motivation GitBot der auf Aktionen reagiert REST API für Änderungen Pull Requests prüfen und annotieren Daten abfragen per GraphQL Website auf GitHub Pages publizieren Build ausführen bei neuen Commit Eigene interaktive UIs bauen Dependencies aktualisieren via Dependabot Releases erstellen

Slide 3

Slide 3 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 3 Git ist eine Versionsverwaltung – und was ist GitHub? Git: Software zur dezentralen Versionsverwaltung von Quellcode. Git-Repository: Datenbank mit Quellcode zu einem konkreten Software-Projekt. GitHub: Amerikanisches Unternehmen, welches cloudbasierte Dienste und APIs rund um Git-Repositories anbietet.

Slide 4

Slide 4 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 4 Hello world: Website bauen, die Inhalte von aus zwei Repositories enthält Website Git 1 Build Git 2 Standard ?

Slide 5

Slide 5 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 5 Hello World: Einen Web Hook ausführen name: Website on: push: branches: - main paths: - 'doc/**' jobs: triggerNetlify: runs-on: ubuntu-latest steps: - name: Trigger Build shell: bash env: NETLIFY_BUILD_TOKEN: ${{ secrets.NETLIFY_BUILD_TOKEN }} run: > curl -f -X POST -d {} https://api.netlify.com/build_hooks/${NETLIFY_BUILD_TOKEN} Repo: asciidoctor/asciidoctor-intellij-plugin

Slide 6

Slide 6 text

GitHub Workflow Cache CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 7 Den Code prüfen und Artefakte publizieren Push Checkout Build Publish Artefact

Slide 7

Slide 7 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 8 Den Code prüfen • Build • Cache • Conditionals • Secrets # .github/workflows/build.yml name: Java CI with Maven on: push: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: actions/setup-java@v3 with: distribution: 'temurin' java-version: '11' cache: 'maven' - name: Build with Maven run: | ./mvnw -B test Repo: ahus1/dropwizard-keycloak-integration

Slide 8

Slide 8 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 9 Den Code prüfen • Build • Cache • Conditionals • Secrets # add to the steps: - name: Cache Maven Wrapper uses: actions/cache@v3 with: path: .mvn/wrapper/maven-wrapper.jar key: ${{ runner.os }}-maven-wrapper- ${{ hashFiles('**/maven-wrapper.properties') }} restore-keys: | ${{ runner.os }}-maven-wrapper-

Slide 9

Slide 9 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 10 Den Code prüfen • Build • Cache • Conditionals • Secrets on: push: branches-ignore: - 'dependabot/**'

Slide 10

Slide 10 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 11 Artefakt publizieren • Build • Cache • Conditionals • Secrets # add to the steps: - name: publish if: > ${{ success() && github.event_name != 'pull_request' && github.ref == 'refs/heads/master' }} env: SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} run: | mvn deploy --settings cisettings.xml -DskipTests=true -B

Slide 11

Slide 11 text

GitHub Workflow Cache CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 12 Den Code prüfen und Artefakte publizieren Push Checkout Build Publish Artefact

Slide 12

Slide 12 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 13 Draft Release anlegen (1/2) # Remove old release drafts by using the curl request for the available releases with draft flag - name: Remove Old Release Drafts env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | gh api repos/${{ github.repository }}/releases \ --jq '.[] | select(.draft == true) | .id' \ | xargs -I '{}' gh api -X DELETE repos/${{ github.repository }}/releases/{} Repo: asciidoctor/asciidoctor-intellij-plugin

Slide 13

Slide 13 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 14 Draft Release anlegen (2/2) # Create new release draft - which is not publicly visible and requires manual acceptance - name: Create Release Draft env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | gh release create "${{ needs.build.outputs.version }}" \ --draft \ --title "v${{ needs.build.outputs.version }}" \ --notes "$(cat << 'EOM' ${{ needs.build.outputs.changelog }} EOM )" Repo: asciidoctor/asciidoctor-intellij-plugin

Slide 14

Slide 14 text

Dependabot CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 15 Dependabot aktualisiert GitHub Actions in Workflows Neues Release der GHA Pull Request .github └─ dependabot.yml Repo: asciidoctor/asciidoctor-intellij-plugin

Slide 15

Slide 15 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 16 Dependabot aktualisiert (auch) GitHub Actions in Workflows version: 2 updates: - package-ecosystem: "gradle" directory: "/" schedule: interval: "daily" - package-ecosystem: "github-actions" directory: "/" schedule: interval: "daily" Repo: asciidoctor/asciidoctor-intellij-plugin

Slide 16

Slide 16 text

Workflow mit einer Custom GitHub Action CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 17 Einen eigenen GitBot bauen Event (z. B. Kommentar) Aktion via GitHub REST API o. Ä., (z. B. Rerun Failed Workflow Jobs) Repo: keycloak/keycloak-gh-actionbot

Slide 17

Slide 17 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 18 Einen einfachen GitBot bauen name: Keycloak GitHub bot on: issue_comment: types: - created permissions: actions: write pull-requests: write jobs: act: runs-on: ubuntu-latest steps: - uses: keycloak/[email protected] with: github_token: ${{ secrets.GITHUB_TOKEN }} „uses“ enthält ein GitHub Repository: Owner, Repository und Version Repo: keycloak/keycloak-gh-actionbot

Slide 18

Slide 18 text

19 CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz Einen einfachen GitBot bauen # action.yml name: Keycloak Bot description: Bot reacting on comments inputs: github_token: required: true debug: required: false default: "false" runs: using: node16 main: dist/index.js keycloak-gh-actionbot ├─ action.yml ├─ index.js ├─ package.json └─ dist └─ index.js Repo: keycloak/keycloak-gh-actionbot

Slide 19

Slide 19 text

20 CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz Einen einfachen GitBot bauen // index.js import core from "@actions/core"; import { context } from "@actions/github"; import { Octokit } from "octokit"; async function run() { const githubToken = core.getInput("github_token"); const octokit = new Octokit({ auth: githubToken }); if (context.eventName === "issue_comment") { /* permission checks skipped until finally: */ await octokit.rest.actions.reRunWorkflowFailedJobs({ owner: context.payload.repository.owner.login, repo: context.payload.repository.name, run_id: job.run_id, }) } keycloak-gh-actionbot ├─ action.yml ├─ index.js ├─ package.json └─ dist └─ index.js Repo: keycloak/keycloak-gh-actionbot

Slide 20

Slide 20 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 21 Sicherheit bei GitHub Actions Berechtigungen: Die Rechte können nur grob eingeschränkt werden. Vertrauenswürdige Quellen: Alle „action/…“ sind von GitHub, alle anderen von anderen Personen. Versionierung: Eine Version wie „@2“ zeigt auf einen Branch oder Tag, welcher vom Owner jederzeit umgehängt werden kann. Auch SHA-1 (böswillige) Kollisionen sind möglich.

Slide 21

Slide 21 text

22 CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz Einen einfachenGitBot bauen // index.js import core from "@actions/core"; import { context } from "@actions/github"; import { Octokit } from "octokit"; async function run() { const githubToken = core.getInput("github_token"); const octokit = new Octokit({ auth: githubToken }); if (context.eventName === "issue_comment") { /* permission checks skipped until finally: */ await octokit.rest.actions.reRunWorkflowFailedJobs({ owner: context.payload.repository.owner.login, repo: context.payload.repository.name, run_id: job.run_id, }) } keycloak-gh-actionbot ├─ action.yml ├─ index.js ├─ package.json └─ dist └─ index.js Repo: keycloak/keycloak-gh-actionbot

Slide 22

Slide 22 text

Anwendung lauscht auf REST Interface und reagiert auf Events CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 23 Einen komplexeren GitBot bauen GitHub Webhooks Labels setzen Repo: keycloak/keycloak-github-bot Instabile Tests reporten Bug-Triage unterstützen …

Slide 23

Slide 23 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 24 Umsetzung für Keycloak Reporting für instabile Testfälle mvn test -Dsurefire.rerunFailingTestsCount=2 ... https://maven.apache.org/surefire/maven-surefire-plugin/examples/rerun-failing-tests.html

Slide 24

Slide 24 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 25 Umsetzung für Keycloak Maven Surefire XML report flaky failure stack trace flaky failure System.out log message success https://maven.apache.org/surefire/maven-surefire-plugin/examples/rerun-failing-tests.html (ebenso: „flakyError“)

Slide 25

Slide 25 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 26 Umsetzung für Keycloak Upload Flaky Tests Action (Pull Requests und Nightly Runs) for dir in $(find -type d -name 'surefire-reports*'); do for i in $(grep -l -E '

Slide 26

Slide 26 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 27 Umsetzung für Keycloak Einfacher Fall: Nightly Run 1. Branch prüfen (main, latest release) 2. Artefakte mit „flaky-tests-“ suchen, herunterladen und parsen 3. GitHub-Issue zum Test existiert schon: • Kommentar hinzufügen 4. GitHub-Issue existiert noch nicht: • Issue erstellen https://github.com/keycloak/keycloak-github-bot/blob/main/src/main/java/org/keycloak/gh/bot/ReportFlakyTests.java

Slide 27

Slide 27 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 28 Umsetzung für Keycloak Einfacher Fall: Nightly Run

Slide 28

Slide 28 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 29 Umsetzung für Keycloak Etwas komplizierter: Pull requests 1. Branch prüfen (main, latest release) 2. Artefakte mit „flaky-tests-“ suchen, herunterladen und parsen 3. Label „flaky-test“ dem Pull Request hinzufügen 4. GitHub-Issue zum Test existiert schon: • Kommentar dem Issue hinzufügen 5. GitHub-Issue existiert noch nicht: • Kommentar dem Pull-Request hinzufügen, inklusive 1-Click-Issue-Erstellung (Ausführung Erfordert auf GitHub Berechtigung mit Schreibzugriff auf das Repository) https://github.com/keycloak/keycloak-github-bot/blob/main/src/main/java/org/keycloak/gh/bot/ReportFlakyTests.java

Slide 29

Slide 29 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 30 Umsetzung für Keycloak Kommentar für unbekannten instabilen Test im Pull Request

Slide 30

Slide 30 text

Workflow mit einer GitHub Action die CodeQL erzeugt (z. B. JetBrains Qodana) CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 31 Code-Qualität prüfen mit CodeQL Pull Request mit Code oder Docs SARIF report mit Upload, Annotation im Pull Request HTML Report mit Abweichungen Repo: asciidoctor/asciidoctor-intellij-plugin

Slide 31

Slide 31 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 32 Code-Qualität prüfen mit CodeQL // steps in my GitHub workflow - name: 'Qodana for Docs' uses: JetBrains/[email protected] with: args: > --linter,jetbrains/qodana-jvm-community:2022.2, -v,${{ github.workspace }}/grazie:/opt/idea/plugins/grazie, -v,${{ github.workspace }}/asciidoctor-intellij... --baseline,doc/qodana-baseline.sarif.json - name: Upload SARIF report to GitHub uses: github/codeql-action/upload-sarif@v2 with: sarif_file: ${{ runner.temp }}/qodana/results/qodana.sarif.json Repo: asciidoctor/asciidoctor-intellij-plugin

Slide 32

Slide 32 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 33 Code-Qualität prüfen mit CodeQL Repo: asciidoctor/asciidoctor-intellij-plugin

Slide 33

Slide 33 text

Terraform CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 34 GitHub Configuration-as-Code GitHub REST API terraform.tfstate main.tf

Slide 34

Slide 34 text

(statische) Website bauen CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 35 Eine Website publizieren mit GitHub Pages (neuer Prozess) Code- Änderung Website ist live Archiv hochladen an die Workflow- Instanz GitHub Pages publizieren Repo: keycloak/keycloak-benchmark

Slide 35

Slide 35 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 36 Eine Website publizieren mit GitHub Pages (neuer Prozess) // add as job in workflow build-and-publish-docs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Node.js uses: actions/setup-node@v3 with: node-version: '16.x' cache: 'yarn' cache-dependency-path: 'antora/yarn.lock' - name: Build docs working-directory: antora run: ./build.sh - name: Upload artifact uses: actions/upload-pages-artifact@v1 with: path: antora/_site/keycloak-benchmark Repo: keycloak/keycloak-benchmark

Slide 36

Slide 36 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 37 Eine Website publizieren mit GitHub Pages (neuer Prozess) // add as job in workflow github-pages: environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} name: GitHub Pages runs-on: ubuntu-latest needs: - build permissions: pages: write id-token: write steps: - name: Deploy to GitHub Pages id: deployment uses: actions/deploy-pages@v1 Repo: keycloak/keycloak-benchmark

Slide 37

Slide 37 text

Report erstellen (GitHub APIs mit Java abfragen) CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 38 Dashboard mit GitHub CLI für Auswertungen erstellen Cron Job Website ist live Website Bauen (Jekyll konvertiert Markdown in HTML) GitHub Pages publizieren Repo: stianst/keycloak-dashboard

Slide 38

Slide 38 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 39 Dashboard mit GitHub CLI für Auswertungen erstellen build-dashboard: runs-on: ubuntu-latest outputs: build: ${{ steps.report.outputs.build }} steps: - uses: actions/checkout@v3 - id: report env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | ./update-data.sh ${{ github.event.inputs.category }} if ( ! git diff --exit-code &>/dev/null ); then git config --global user.email "…" git config --global user.name "github-actions[bot]" git add --all git commit -m "Updated data" git push fi Repo: stianst/keycloak-dashboard

Slide 39

Slide 39 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 40 Dashboard mit GitHub CLI für Auswertungen erstellen <#list bugStats as bugStat> ${bugStat.label} ${bugStat.count} <#if bugStat.closedCount?has_content> ${bugStat.closedCount} <#else> #if>

Slide 40

Slide 40 text

CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 41 Daten per GraphQL abfragen # workflow runtimes query { search(query: "repo:keycloak/keycloak is:pr is:merged merged:$$FROM_DATE$$", type: ISSUE, first: $$FIRST$$) { edges { node { ... on PullRequest { commits(last: 1) { edges { node { pullRequest { number, author{login}, mergedAt, baseRefName, commits(last: 1) { edges { node { commit { checkSuites(first: 10) { edges { node { createdAt updatedAt workflowRun { workflow { name

Slide 41

Slide 41 text

Keycloak GitHub Action Bot (GitHub Bot) keycloak/keycloak-gh-actionbot IntelliJ AsciiDoc Plugin (Releases, Webhook, Qodana, CodeQL) asciidoctor/asciidoctor-intellij-plugin Keycloak Dropwizard (Build, Sonatype Nexus) ahus1/keycloak-dropwizard-integration Keycloak Static Dashboard (Java, Markdown, Jekyll) stianst/keycloak-dashboard CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 42 Beispiele @ahus1de Keycloak Bot (Quarkus, GitHub extension) keycloak/keycloak-github-bot Keycloak Benchmark (Maven Build, Antora Static Documentation Site) keycloak/keycloak-benchmark GitHub REST API https://docs.github.com/en/rest

Slide 42

Slide 42 text

Kontakt Alexander Schwartz Principal Software Engineer [email protected] https://www.ahus1.de @ahus1de CC BY-NC-SA 4.0 | Juni 2024 | Automatisiere deine Prozesse mit GitHub Actions! | Alexander Schwartz 43