From 4b4e94ae4555b5728d91776c6b9026166679fbd3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=81lex=20Ruiz?= <alejandro.ruiz.becerra@wazuh.com>
Date: Fri, 8 Mar 2024 16:06:15 +0100
Subject: [PATCH] Upload packages to S3 (#179)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Attemtp to automate package's testing

* Add workflow file to upload packages to S3

* Skip testing to test whether the upload works

* Fix package names

* Fix upload workflow name

* Pass secrets to the reusable workflow

* Fix indentation

* Fix indentation

* Remove test workflow from this PR

* Add boolean input to control when the package is uploaded to the S3 bucket

* [UI/UX] Improve inputs description

---------

Signed-off-by: Álex Ruiz <alejandro.ruiz.becerra@wazuh.com>
---
 .github/workflows/build.yml    | 15 ++++++++++++-
 .github/workflows/r_upload.yml | 41 ++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 .github/workflows/r_upload.yml

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index d045b5d4fb103..f68708a1197fb 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -10,10 +10,14 @@ on:
   workflow_dispatch:
     inputs:
       revision:
-        description: "Any string or number used to extend the package's identifier"
+        description: "Revision"
         type: string
         required: true
         default: "1"
+      upload:
+        description: "Upload ?"
+        type: bool
+        default: false
 
 # ==========================
 # Bibliography
@@ -74,3 +78,12 @@ jobs:
     uses: ./.github/workflows/r_test.yml
     with:
       package: wazuh-indexer-${{ needs.version.outputs.version }}-${{ github.event_name == 'push' && '1' || inputs.revision }}_${{ matrix.os.suffix }}_${{ needs.commit_sha.outputs.commit_sha }}.${{ matrix.os.ext }}
+  
+  upload:
+    needs: [version, commit_sha, test]
+    # Upload only on 'workflow_dispatch' event and if 'upload=true'
+    if: ${{ github.event_name == 'push' && inputs.upload }}
+    uses: ./.github/workflows/r_upload.yml
+    with:
+      package: wazuh-indexer-${{ needs.version.outputs.version }}-${{ github.event_name == 'push' && '1' || inputs.revision }}_${{ matrix.os.suffix }}_${{ needs.commit_sha.outputs.commit_sha }}.${{ matrix.os.ext }}
+    secrets: inherit
diff --git a/.github/workflows/r_upload.yml b/.github/workflows/r_upload.yml
new file mode 100644
index 0000000000000..05a08e40394c4
--- /dev/null
+++ b/.github/workflows/r_upload.yml
@@ -0,0 +1,41 @@
+name: Upload (reusable)
+
+# This workflow runs when any of the following occur:
+# - Run from another workflow
+on:
+  workflow_call:
+    inputs:
+      package:
+        description: "The name of the package to upload to Wazuh's S3 development artifactory bucket."
+        required: true
+        type: string
+    secrets:
+      CI_INTERNAL_DEVELOPMENT_BUCKET_USER_ACCESS_KEY:
+        required: true
+      CI_INTERNAL_DEVELOPMENT_BUCKET_USER_SECRET_KEY:
+        required: true
+
+jobs:
+  r_upload:
+    runs-on: ubuntu-latest
+    # Permissions to upload the package
+    permissions:
+      packages: read
+      contents: read
+    steps:
+      - name: Download artifact
+        uses: actions/download-artifact@v4
+        with:
+          name: ${{ inputs.package }}
+          path: artifacts/dist
+
+      - name: Set up AWS CLI
+        uses: aws-actions/configure-aws-credentials@v1
+        with:
+          aws-access-key-id: ${{ secrets.CI_INTERNAL_DEVELOPMENT_BUCKET_USER_ACCESS_KEY }}
+          aws-secret-access-key: ${{ secrets.CI_INTERNAL_DEVELOPMENT_BUCKET_USER_SECRET_KEY }}
+          aws-region: us-east-1
+
+      - name: Upload package to S3
+        run: |
+          aws s3 cp artifacts/dist/${{ inputs.package }} s3://packages-dev.internal.wazuh.com/development/wazuh/4.x/main/packages/