From 2e909a1903c3cf16fde7317db1a7a61db794ea22 Mon Sep 17 00:00:00 2001
From: kcs-bandihareesh <106671992+kcs-bandihareesh@users.noreply.github.com>
Date: Fri, 11 Oct 2024 15:08:00 +0530
Subject: [PATCH] Actions workflow for ace-api (#14)

* added workflow file for ace maven

* updated actions-maven-setting file

* updated workflow to deploy for master branch only
---
 .github/workflows/ace-api.yaml    | 92 +++++++++++++++++++++++++++++++
 travis/actions-maven-settings.xml | 69 +++++++++++++++++++++++
 2 files changed, 161 insertions(+)
 create mode 100644 .github/workflows/ace-api.yaml
 create mode 100644 travis/actions-maven-settings.xml

diff --git a/.github/workflows/ace-api.yaml b/.github/workflows/ace-api.yaml
new file mode 100644
index 0000000..0f9d63a
--- /dev/null
+++ b/.github/workflows/ace-api.yaml
@@ -0,0 +1,92 @@
+name: ace-api
+
+on:
+  push:
+  pull_request:
+  workflow_dispatch:
+
+permissions:
+  contents: read
+  id-token: write
+
+jobs:
+  test:
+    name: Test
+    runs-on: ubuntu-latest
+
+    steps:
+      - name: Checkout code
+        uses: actions/checkout@v4
+
+      - name: Set up JDK 8
+        uses: actions/setup-java@v4
+        with:
+          java-version: '8'
+          distribution: 'temurin'
+
+      - name: Cache Maven dependencies
+        uses: actions/cache@v4
+        with:
+          path: ~/.m2
+          key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
+          restore-keys: |
+            ${{ runner.os }}-maven
+
+      - name: Setup Maven settings
+        run: |
+          mkdir -p ~/.m2
+          cp ./travis/actions-maven-settings.xml ~/.m2/settings.xml
+          sed -i "s/-SNAPSHOT/-github-build-${{ github.run_number }}/" pom.xml
+
+      - name: Run tests
+        run: mvn -e verify
+
+      - name: Display Surefire reports on failure
+        if: failure()
+        run: |
+          for F in target/surefire-reports/*.txt; do echo $F; cat $F; echo; done
+
+  deploy-snapshots:
+    name: Deploy Snapshots
+    runs-on: ubuntu-latest
+    if: github.ref == 'refs/heads/master' && github.event_name == 'push'
+   
+    steps:
+      - name: Checkout code
+        uses: actions/checkout@v4
+
+      - name: Set up JDK 8
+        uses: actions/setup-java@v4
+        with:
+          java-version: '8'
+          distribution: 'temurin'
+
+      - name: Cache Maven dependencies
+        uses: actions/cache@v4
+        with:
+          path: ~/.m2
+          key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
+          restore-keys: |
+            ${{ runner.os }}-maven
+
+      - name: Setup Maven settings
+        run: |
+          mkdir -p ~/.m2
+          cp ./travis/actions-maven-settings.xml ~/.m2/settings.xml
+          sed -i "s/-SNAPSHOT/-github-build-${{ github.run_number }}/" pom.xml
+
+      - name: Authenticate to Google Cloud
+        uses: google-github-actions/auth@v2
+        with:
+          project_id: 'som-rit-infrastructure-prod'
+          workload_identity_provider: 'projects/294515190965/locations/global/workloadIdentityPools/github/providers/susom-github'
+          create_credentials_file: true
+          export_environment_variables: true
+          cleanup_credentials: true
+
+      - name: Set up OAuth2 access token for Maven
+        run: |
+          echo "ACCESS_TOKEN=$(gcloud auth print-access-token)" >> $GITHUB_ENV
+
+      - name: Deploy Snapshots
+        run: mvn --batch-mode -e -DskipTests=true deploy -s ~/.m2/settings.xml
\ No newline at end of file
diff --git a/travis/actions-maven-settings.xml b/travis/actions-maven-settings.xml
new file mode 100644
index 0000000..c41ee9b
--- /dev/null
+++ b/travis/actions-maven-settings.xml
@@ -0,0 +1,69 @@
+<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
+        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+        xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
+                    http://maven.apache.org/xsd/settings-1.0.0.xsd">
+
+<mirrors>
+  <mirror>
+    <mirrorOf>*,!artifact-registry</mirrorOf>
+    <url>https://repo.maven.apache.org/maven2/</url>
+  </mirror>
+</mirrors>
+
+<profiles>
+  <profile>
+    <id>ci-build</id>
+    <repositories>
+      <repository>
+        <id>artifact-registry</id>
+        <url>https://us-west1-maven.pkg.dev/som-rit-infrastructure-prod/ace-maven</url>
+        <releases>
+          <enabled>true</enabled>
+        </releases>
+        <snapshots>
+          <enabled>false</enabled>
+        </snapshots>
+      </repository>
+    </repositories>
+
+    <pluginRepositories>
+      <pluginRepository>
+        <releases>
+          <updatePolicy>never</updatePolicy>
+        </releases>
+        <snapshots>
+          <enabled>false</enabled>
+        </snapshots>
+        <id>central</id>
+        <name>Central Repository</name>
+        <url>https://repo.maven.apache.org/maven2</url>
+      </pluginRepository>
+    </pluginRepositories>
+  </profile>
+</profiles>
+
+<activeProfiles>
+  <!-- Activate the profile for GitHub Actions -->
+  <activeProfile>github-actions</activeProfile>
+</activeProfiles>
+
+<servers>
+  <server>
+    <id>artifact-registry</id>
+    <username>oauth2accesstoken</username>
+    <password>${env.ACCESS_TOKEN}</password> <!-- Access token set as environment variable -->
+  </server>
+
+  <server>
+    <id>ossrh</id>
+    <username>${env.OSSRH_USERNAME}</username>
+    <password>${env.OSSRH_PASSWORD}</password>
+  </server>
+
+  <server>
+    <id>github.com</id>
+    <username>${env.GITHUB_USERNAME}</username>
+    <password>${env.GITHUB_TOKEN}</password>
+  </server>
+</servers>
+</settings>
\ No newline at end of file