From 737876bc6d5d5ab3df04dde8ffe788617d33a11a Mon Sep 17 00:00:00 2001 From: Antonio Gelameris Date: Wed, 23 Nov 2022 11:25:04 +0100 Subject: [PATCH] Adding CI (#6) * Adding CI * Fix typo * Fix targets * Removing registry part --- .github/workflows/ci.yml | 145 ++++++++++++++++++++++++++++++++++++ .github/workflows/clean.yml | 59 +++++++++++++++ build.sbt | 34 ++------- project/plugins.sbt | 4 + 4 files changed, 214 insertions(+), 28 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/clean.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..835e076 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,145 @@ +# This file was automatically generated by sbt-github-actions using the +# githubWorkflowGenerate task. You should add and commit this file to +# your git repository. It goes without saying that you shouldn't edit +# this file by hand! Instead, if you wish to make changes, you should +# change your sbt build configuration to revise the workflow description +# to meet your needs, then regenerate this file. + +name: Continuous Integration + +on: + push: + branches: ['**'] + tags: ['**'] + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + +jobs: + build: + name: Build and Test + strategy: + matrix: + os: [ubuntu-latest] + scala: [2.13.10] + java: [temurin@11] + runs-on: ${{ matrix.os }} + steps: + - name: Checkout current branch (full) + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Setup Java (temurin@11) + if: matrix.java == 'temurin@11' + uses: actions/setup-java@v2 + with: + distribution: temurin + java-version: 11 + + - name: Cache sbt + uses: actions/cache@v2 + with: + path: | + ~/.sbt + ~/.ivy2/cache + ~/.coursier/cache/v1 + ~/.cache/coursier/v1 + ~/AppData/Local/Coursier/Cache/v1 + ~/Library/Caches/Coursier/v1 + key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }} + + - name: Install node 16 + uses: actions/setup-node@v3 + with: + node-version: 16 + + - name: Installing openapi-generator-cli + run: npm install -g @openapitools/openapi-generator-cli + + - name: Build project + run: sbt ++${{ matrix.scala }} test + + - name: Compress target directories + run: tar cf targets.tar party-process-client/target party-management-client/target user-registry-client/target project/target + + - name: Upload target directories + uses: actions/upload-artifact@v2 + with: + name: target-${{ matrix.os }}-${{ matrix.scala }}-${{ matrix.java }} + path: targets.tar + + publish: + name: Publish Artifacts + needs: [build] + permissions: + contents: read + packages: write + if: github.event_name != 'pull_request' && (github.ref == 'refs/heads/1.0.x' || startsWith(github.ref, 'refs/tags/')) + strategy: + matrix: + os: [ubuntu-latest] + scala: [2.13.10] + java: [temurin@11] + runs-on: ${{ matrix.os }} + steps: + - name: Checkout current branch (full) + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Setup Java (temurin@11) + if: matrix.java == 'temurin@11' + uses: actions/setup-java@v2 + with: + distribution: temurin + java-version: 11 + + - name: Cache sbt + uses: actions/cache@v2 + with: + path: | + ~/.sbt + ~/.ivy2/cache + ~/.coursier/cache/v1 + ~/.cache/coursier/v1 + ~/AppData/Local/Coursier/Cache/v1 + ~/Library/Caches/Coursier/v1 + key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }} + + - name: Download target directories (2.13.10) + uses: actions/download-artifact@v2 + with: + name: target-${{ matrix.os }}-2.13.10-${{ matrix.java }} + + - name: Inflate target directories (2.13.10) + run: | + tar xf targets.tar + rm targets.tar + + - name: Install node 16 + uses: actions/setup-node@v3 + with: + node-version: 16 + + - name: Installing openapi-generator-cli + run: npm install -g @openapitools/openapi-generator-cli + + - name: Get 1.0.x-SNAPSHOTS versionIds + id: version + if: github.ref == 'refs/heads/1.0.x' + uses: castlabs/get-package-version-id-action@v2.0 + with: + version: 1.0.x-SNAPSHOT + + - name: Deleting 1.0.x-SNAPSHOTS versions + if: ${{ github.ref == 'refs/heads/1.0.x' && steps.version.outputs.ids != '' }} + uses: actions/delete-package-versions@v2 + with: + package-version-ids: ${{ steps.version.outputs.ids }} + + - name: Regenerating code + run: sbt ++${{ matrix.scala }} generateCode + + - name: Publish project + run: sbt ++${{ matrix.scala }} +publish diff --git a/.github/workflows/clean.yml b/.github/workflows/clean.yml new file mode 100644 index 0000000..547aaa4 --- /dev/null +++ b/.github/workflows/clean.yml @@ -0,0 +1,59 @@ +# This file was automatically generated by sbt-github-actions using the +# githubWorkflowGenerate task. You should add and commit this file to +# your git repository. It goes without saying that you shouldn't edit +# this file by hand! Instead, if you wish to make changes, you should +# change your sbt build configuration to revise the workflow description +# to meet your needs, then regenerate this file. + +name: Clean + +on: push + +jobs: + delete-artifacts: + name: Delete Artifacts + runs-on: ubuntu-latest + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + steps: + - name: Delete artifacts + run: | + # Customize those three lines with your repository and credentials: + REPO=${GITHUB_API_URL}/repos/${{ github.repository }} + + # A shortcut to call GitHub API. + ghapi() { curl --silent --location --user _:$GITHUB_TOKEN "$@"; } + + # A temporary file which receives HTTP response headers. + TMPFILE=/tmp/tmp.$$ + + # An associative array, key: artifact name, value: number of artifacts of that name. + declare -A ARTCOUNT + + # Process all artifacts on this repository, loop on returned "pages". + URL=$REPO/actions/artifacts + while [[ -n "$URL" ]]; do + + # Get current page, get response headers in a temporary file. + JSON=$(ghapi --dump-header $TMPFILE "$URL") + + # Get URL of next page. Will be empty if we are at the last page. + URL=$(grep '^Link:' "$TMPFILE" | tr ',' '\n' | grep 'rel="next"' | head -1 | sed -e 's/.*.*//') + rm -f $TMPFILE + + # Number of artifacts on this page: + COUNT=$(( $(jq <<<$JSON -r '.artifacts | length') )) + + # Loop on all artifacts on this page. + for ((i=0; $i < $COUNT; i++)); do + + # Get name of artifact and count instances of this name. + name=$(jq <<<$JSON -r ".artifacts[$i].name?") + ARTCOUNT[$name]=$(( $(( ${ARTCOUNT[$name]} )) + 1)) + + id=$(jq <<<$JSON -r ".artifacts[$i].id?") + size=$(( $(jq <<<$JSON -r ".artifacts[$i].size_in_bytes?") )) + printf "Deleting '%s' #%d, %'d bytes\n" $name ${ARTCOUNT[$name]} $size + ghapi -X DELETE $REPO/actions/artifacts/$id + done + done diff --git a/build.sbt b/build.sbt index b882917..6ddfbd9 100644 --- a/build.sbt +++ b/build.sbt @@ -5,10 +5,9 @@ ThisBuild / scalaVersion := "2.13.10" ThisBuild / organization := "it.pagopa" ThisBuild / organizationName := "Pagopa S.p.A." ThisBuild / version := ComputeVersion.version - -ThisBuild / resolvers += "Pagopa Nexus Snapshots" at s"https://${System.getenv("MAVEN_REPO")}/nexus/repository/maven-snapshots/" -ThisBuild / resolvers += "Pagopa Nexus Releases" at s"https://${System.getenv("MAVEN_REPO")}/nexus/repository/maven-releases/" -ThisBuild / credentials += Credentials(Path.userHome / ".sbt" / ".credentials") +ThisBuild / githubOwner := "pagopa" +ThisBuild / githubRepository := "interop-selfcare-proxy-clients" +ThisBuild / resolvers += Resolver.githubPackages("pagopa") val generateCode = taskKey[Unit]("A task for generating the code starting from the swagger definition") val packagePrefix = settingKey[String]("The package prefix derived from the uservice name") @@ -48,14 +47,7 @@ lazy val partyProcessClient = project }, scalacOptions := Seq(), libraryDependencies := Dependencies.Jars.client, - updateOptions := updateOptions.value.withGigahorse(false), - publishTo := { - val nexus = s"https://${System.getenv("MAVEN_REPO")}/nexus/repository/" - if (isSnapshot.value) - Some("snapshots" at nexus + "maven-snapshots/") - else - Some("releases" at nexus + "maven-releases/") - } + updateOptions := updateOptions.value.withGigahorse(false) ) cleanFiles += baseDirectory.value / "party-management-client" / "src" @@ -87,14 +79,7 @@ lazy val partyManagementClient = project }, scalacOptions := Seq(), libraryDependencies := Dependencies.Jars.client, - updateOptions := updateOptions.value.withGigahorse(false), - publishTo := { - val nexus = s"https://${System.getenv("MAVEN_REPO")}/nexus/repository/" - if (isSnapshot.value) - Some("snapshots" at nexus + "maven-snapshots/") - else - Some("releases" at nexus + "maven-releases/") - } + updateOptions := updateOptions.value.withGigahorse(false) ) cleanFiles += baseDirectory.value / "user-registry-client" / "src" @@ -126,14 +111,7 @@ lazy val userRegistryClient = project }, scalacOptions := Seq(), libraryDependencies := Dependencies.Jars.client, - updateOptions := updateOptions.value.withGigahorse(false), - publishTo := { - val nexus = s"https://${System.getenv("MAVEN_REPO")}/nexus/repository/" - if (isSnapshot.value) - Some("snapshots" at nexus + "maven-snapshots/") - else - Some("releases" at nexus + "maven-releases/") - } + updateOptions := updateOptions.value.withGigahorse(false) ) (Compile / compile) := ((Compile / compile) dependsOn partyProcessClient / generateCode).value diff --git a/project/plugins.sbt b/project/plugins.sbt index d9deb01..31ef2da 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -9,3 +9,7 @@ addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.2.2") addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6") addSbtPlugin("com.lightbend.sbt" % "sbt-javaagent" % "0.1.6") + +addSbtPlugin("com.codecommit" % "sbt-github-packages" % "0.5.3") + +ThisBuild / libraryDependencySchemes ++= Seq("org.scala-lang.modules" %% "scala-xml" % VersionScheme.Always)