From 24f063881033864b5c6fd4e86af3396fedcb6ff7 Mon Sep 17 00:00:00 2001 From: Diego Lagos <92735530+diegolagospagopa@users.noreply.github.com> Date: Fri, 3 Jun 2022 21:54:46 +0200 Subject: [PATCH] feat: Release docker version (#2) * test * added template page with colors * completed colors logic * added thanks to mmumshad * force push * fix github workflow * primo * more tags * give release plugins --- .github/workflows/docker-github-packages.yaml | 55 ++++++++++++++ .github/workflows/pr-title.yml | 55 ++++++++++++++ .github/workflows/release.yml | 33 ++++++++ README.md | 5 ++ pom.xml | 5 ++ .../springbootshowcase/AppController.java | 42 ++++++++++ .../springbootshowcase/RootController.java | 76 +++++++------------ src/main/resources/application.properties | 3 +- .../resources/templates/indexTemplate.html | 12 +++ 9 files changed, 234 insertions(+), 52 deletions(-) create mode 100644 .github/workflows/docker-github-packages.yaml create mode 100644 .github/workflows/pr-title.yml create mode 100644 .github/workflows/release.yml create mode 100644 src/main/java/it/pagopa/devops/springbootshowcase/AppController.java create mode 100644 src/main/resources/templates/indexTemplate.html diff --git a/.github/workflows/docker-github-packages.yaml b/.github/workflows/docker-github-packages.yaml new file mode 100644 index 0000000..d13ce7b --- /dev/null +++ b/.github/workflows/docker-github-packages.yaml @@ -0,0 +1,55 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +name: Create and publish a Docker image + +on: + push: + branches: + - "docker-repo" + tags: + - "v*" + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Log in to the Container registry + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=sha + + - name: Build and push Docker image + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/.github/workflows/pr-title.yml b/.github/workflows/pr-title.yml new file mode 100644 index 0000000..bc673f5 --- /dev/null +++ b/.github/workflows/pr-title.yml @@ -0,0 +1,55 @@ +name: "Validate PR title" + +on: + pull_request_target: + types: + - opened + - edited + - synchronize + +jobs: + main: + name: Validate PR title + runs-on: ubuntu-latest + steps: + # Please look up the latest version from + # https://github.com/amannn/action-semantic-pull-request/releases + - uses: amannn/action-semantic-pull-request@v3.4.6 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + # Configure which types are allowed. + # Default: https://github.com/commitizen/conventional-commit-types + types: | + fix + feat + docs + chore + breaking + # Configure that a scope must always be provided. + requireScope: false + # Configure additional validation for the subject based on a regex. + # This example ensures the subject starts with an uppercase character. + subjectPattern: ^[A-Z].+$ + # If `subjectPattern` is configured, you can use this property to override + # the default error message that is shown when the pattern doesn't match. + # The variables `subject` and `title` can be used within the message. + subjectPatternError: | + The subject "{subject}" found in the pull request title "{title}" + didn't match the configured pattern. Please ensure that the subject + starts with an uppercase character. + # For work-in-progress PRs you can typically use draft pull requests + # from Github. However, private repositories on the free plan don't have + # this option and therefore this action allows you to opt-in to using the + # special "[WIP]" prefix to indicate this state. This will avoid the + # validation of the PR title and the pull request checks remain pending. + # Note that a second check will be reported if this is enabled. + wip: true + # When using "Squash and merge" on a PR with only one commit, GitHub + # will suggest using that commit message instead of the PR title for the + # merge commit, and it's easy to commit this by mistake. Enable this option + # to also validate the commit message for one commit PRs. + validateSingleCommit: true + # Related to `validateSingleCommit` you can opt-in to validate that the PR + # title matches a single commit to avoid confusion. + validateSingleCommitMatchesPrTitle: true diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..6514539 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,33 @@ +name: Release + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + push: + branches: + - release-dev + paths-ignore: + - "CODEOWNERS" + - "**.md" + - ".**" + +jobs: + release: + name: Release + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + persist-credentials: false + fetch-depth: 0 + + - name: Release + uses: cycjimmy/semantic-release-action@v3 + with: + semantic_version: 19.0.2 + extra_plugins: | + @semantic-release/release-notes-generator@10.0.3 + @semantic-release/git@10.0.1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/README.md b/README.md index ce1007f..2e2bc83 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # devops-springboot-gitlab-workflow + Application written in Spring boot to test pipelines using the gitlab workflow + +## Thanks to + + for the idea about colors diff --git a/pom.xml b/pom.xml index 584c798..515aebe 100644 --- a/pom.xml +++ b/pom.xml @@ -36,8 +36,13 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-starter-thymeleaf + + diff --git a/src/main/java/it/pagopa/devops/springbootshowcase/AppController.java b/src/main/java/it/pagopa/devops/springbootshowcase/AppController.java new file mode 100644 index 0000000..4f4b3dd --- /dev/null +++ b/src/main/java/it/pagopa/devops/springbootshowcase/AppController.java @@ -0,0 +1,42 @@ +package it.pagopa.devops.springbootshowcase; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/app") +public class AppController { + + @Value("${MY_ENV_1:}") + private String myEnv1; + + @GetMapping(path = "/", produces=MediaType.APPLICATION_JSON_VALUE) + public Map root() + { + HashMap map = new HashMap<>(); + map.put("root", "ok"); + return map; + } + + @GetMapping(path = "/status", produces=MediaType.APPLICATION_JSON_VALUE) + public Map status() + { + HashMap map = new HashMap<>(); + map.put("status", "ok"); + return map; + } + + @GetMapping(path = "/envs", produces=MediaType.APPLICATION_JSON_VALUE) + public Map envs() + { + HashMap map = new HashMap<>(); + map.put("MY_ENV_1", myEnv1); + return map; + } +} diff --git a/src/main/java/it/pagopa/devops/springbootshowcase/RootController.java b/src/main/java/it/pagopa/devops/springbootshowcase/RootController.java index 3ec0d5d..c63350e 100644 --- a/src/main/java/it/pagopa/devops/springbootshowcase/RootController.java +++ b/src/main/java/it/pagopa/devops/springbootshowcase/RootController.java @@ -1,64 +1,40 @@ package it.pagopa.devops.springbootshowcase; -import java.util.HashMap; -import java.util.Map; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.List; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.MediaType; +import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.http.HttpStatus; +import org.springframework.web.servlet.ModelAndView; +import java.util.Random; -@RestController -@RequestMapping("/") +@Controller public class RootController { - @Value("${MY_ENV_1:}") - private String myEnv1; + private List colors = Arrays.asList(new String[]{"red","green","blue","blue2","darkblue","pink","lightgoldenrodyellow","black"}); - @GetMapping(path = "/", produces=MediaType.APPLICATION_JSON_VALUE) - public Map root() - { - HashMap map = new HashMap<>(); - map.put("root", "ok"); - return map; + @GetMapping("/") + public ModelAndView passParametersWithModelAndView() throws UnknownHostException { + ModelAndView modelAndView = new ModelAndView("indexTemplate"); + modelAndView.addObject("color", getOneColor("")); + modelAndView.addObject("hostname", InetAddress.getLocalHost().getHostName()); + return modelAndView; } - @GetMapping(path = "/status", produces=MediaType.APPLICATION_JSON_VALUE) - public Map status() - { - HashMap map = new HashMap<>(); - map.put("status", "ok"); - return map; - } - - - @GetMapping(path = "/envs", produces=MediaType.APPLICATION_JSON_VALUE) - public Map envs() - { - HashMap map = new HashMap<>(); - System.getenv().forEach((k, v) -> { - map.put(k, v); - }); - return map; - } - /* - * Liveness & Readiness - */ - @GetMapping(path = "/live", produces=MediaType.APPLICATION_JSON_VALUE) - @ResponseStatus(HttpStatus.OK) - public String live() - { - return "live"; - } + private String getOneColor(String choiseColor){ + Random r = new Random(); - @GetMapping(path = "/ready", produces=MediaType.APPLICATION_JSON_VALUE) - @ResponseStatus(HttpStatus.OK) - public String ready() - { - return "ready"; + if (choiseColor != null && choiseColor != ""){ + return colors.stream() + .filter(color -> color.equals(choiseColor)) + .findAny() + .orElse(null); + } else { + return colors.get(r.nextInt(colors.size())); + } } + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f8f0ebb..ace0993 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,4 @@ -logging.level.org.springframework=INFO -logging.level.com.howtodoinjava=INFO +logging.level.org.springframework=DEBUG #output to a temp_folder/file logging.file=${java.io.tmpdir}/application.log diff --git a/src/main/resources/templates/indexTemplate.html b/src/main/resources/templates/indexTemplate.html new file mode 100644 index 0000000..74104d1 --- /dev/null +++ b/src/main/resources/templates/indexTemplate.html @@ -0,0 +1,12 @@ + + +Hello from PagoPA + +
+ +

+ +