From 8ee3186336d4082c02597fb0da106b4028c4ee62 Mon Sep 17 00:00:00 2001
From: Pieter van Staden <pieter@bob.co.za>
Date: Mon, 5 May 2025 13:00:11 +0200
Subject: [PATCH] INFRASTRUCTURE :: Update the CICD on how we package the 
 cater tagged releases

---
 .distignore    |  1 +
 .gitattributes |  8 ++++++++
 .gitignore     | 18 +++++++++++++---
 .gitlab-ci.yml | 56 ++++++++++++++++++++++++++++++++++++++++++++++++--
 package.json   |  2 +-
 5 files changed, 79 insertions(+), 6 deletions(-)
 create mode 100644 .gitattributes

diff --git a/.distignore b/.distignore
index e06a047..7b0d49b 100644
--- a/.distignore
+++ b/.distignore
@@ -9,4 +9,5 @@ make-zip.sh
 update-version.js
 .gitlab-ci.yml
 .gitignore
+.gitattributes
 package
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..d561145
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,8 @@
+# Exclude specific files and directories from archives
+/.husky export-ignore
+/.distignore export-ignore
+/.gitignore export-ignore
+/.gitlab-ci.yml export-ignore
+/make-zip.sh export-ignore
+/update-version.js export-ignore
+/.gitattributes export-ignore
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
index 6450684..f570bab 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,16 @@
-node_modules
+# packages, not needed in final build
+node_modules/
+
+#general
+.DS_Store
+phpcs.xml
+phpunit.xml
+Thumbs.db
+wp-cli.local.yml
+*.sql
+*.tar.gz
 *.zip
-.idea
-/.idea/
+package-lock.json
+
+#PhpStorm
+.idea/
\ No newline at end of file
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index bc49012..d83053c 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -2,9 +2,11 @@ image: shiplogic/ci-wp-plugin:node18
 
 variables:
   GIT_SUBMODULE_STRATEGY: recursive
+  MAX_TAGS_TO_KEEP: 5  # Number of most recent tag versions to keep
 
 stages:
   - deploy
+  - tag_deploy
 
 deploy:
   stage: deploy
@@ -17,7 +19,57 @@ deploy:
     - export AWS_SECRET_ACCESS_KEY
   script:
     - ./make-zip.sh
-    - aws s3 cp bobgo-magento-plugin.zip s3://bobgo-s3-magento-plugin/ --region=af-south-1
+    - aws s3 cp bobgo-magento-plugin.zip s3://bobgo-s3-magento-plugin-dev/ --region=af-south-1
   rules:
-    - if: '$CI_COMMIT_BRANCH == "dev" && $CI_COMMIT_TAG == null'
+    - if: '$CI_COMMIT_BRANCH == "dev"'
       when: always
+
+tag_deploy:
+  stage: tag_deploy
+  image: shiplogic/ci-wp-plugin:node18
+  before_script:
+    # Export AWS credentials for prod branch (since tags are created from prod)
+    - AWS_ACCESS_KEY_ID_KEY="prod_AWS_ACCESS_KEY_ID"
+    - AWS_SECRET_ACCESS_KEY_KEY="prod_AWS_SECRET_ACCESS_KEY"
+    - AWS_ACCESS_KEY_ID=$(eval echo -e "\$$AWS_ACCESS_KEY_ID_KEY")
+    - AWS_SECRET_ACCESS_KEY=$(eval echo -e "\$$AWS_SECRET_ACCESS_KEY_KEY")
+    - export AWS_ACCESS_KEY_ID
+    - export AWS_SECRET_ACCESS_KEY
+    
+    # Check if variables are set (without exposing values)
+    - if [ -z "$AWS_ACCESS_KEY_ID" ]; then echo "AWS_ACCESS_KEY_ID is empty"; else echo "AWS_ACCESS_KEY_ID is set"; fi
+    - if [ -z "$AWS_SECRET_ACCESS_KEY" ]; then echo "AWS_SECRET_ACCESS_KEY is empty"; else echo "AWS_SECRET_ACCESS_KEY is set"; fi
+    
+    # Verify tag was created from prod branch
+    - git fetch origin prod
+    - COMMIT_BRANCH=$(git branch -r --contains $CI_COMMIT_SHA | grep "origin/prod" || echo "")
+    - if [ -z "$COMMIT_BRANCH" ]; then echo "Tag was not created from prod branch. Skipping deployment."; exit 1; fi
+  script:
+    # Download the tagged version archive
+    - TAG_ARCHIVE_URL="https://gitlab.bob.co.za/bob-public-utils/bobgo-magento-extension/-/archive/${CI_COMMIT_TAG}/bobgo-magento-extension-${CI_COMMIT_TAG}.zip"
+    - curl -o "bobgo-magento-extension-${CI_COMMIT_TAG}.zip" "$TAG_ARCHIVE_URL"
+    
+    # Upload to S3 bucket both as versioned and as "latest"
+    - aws s3 cp "bobgo-magento-extension-${CI_COMMIT_TAG}.zip" "s3://bobgo-s3-magento-plugin-prod/tags/bobgo-magento-extension-${CI_COMMIT_TAG}.zip" --region=af-south-1 --acl public-read
+    - aws s3 cp "bobgo-magento-extension-${CI_COMMIT_TAG}.zip" "s3://bobgo-s3-magento-plugin-prod/latest/latest.zip" --region=af-south-1 --acl public-read
+    
+    # Cleanup old tag versions - keep only the most recent MAX_TAGS_TO_KEEP
+    - echo "Cleaning up old tag versions, keeping only the most recent $MAX_TAGS_TO_KEEP..."
+    - >
+      aws s3 ls s3://bobgo-s3-magento-plugin-prod/tags/ --region=af-south-1 | 
+      grep "bobgo-magento-extension-" | 
+      sort -r | 
+      tail -n +$((MAX_TAGS_TO_KEEP + 1)) | 
+      awk '{print $4}' | 
+      while read KEY; do 
+        echo "Deleting old tag version: $KEY"; 
+        aws s3 rm "s3://bobgo-s3-magento-plugin-prod/tags/$KEY" --region=af-south-1; 
+      done
+    
+    # Output the URLs
+    - echo "Tagged version URL: https://magento-plugin.bobgo.co.za/tags/bobgo-magento-extension-${CI_COMMIT_TAG}.zip"
+    - echo "Latest version URL: https://magento-plugin.bobgo.co.za/latest/latest.zip"
+    - echo "Retained the latest $MAX_TAGS_TO_KEEP tag versions in S3 bucket."
+  rules:
+    - if: '$CI_COMMIT_TAG'
+      when: always
\ No newline at end of file
diff --git a/package.json b/package.json
index e44822d..7ca71f8 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
 {
   "name": "bobgo-magento-plugin",
   "description": "Bob Go magento plugin",
-  "version": "1.0.41",
+  "version": "1.0.50",
   "license": "GPL-2.0-or-later",
   "scripts": {
     "prepare": "husky install",
-- 
GitLab