Procházet zdrojové kódy

Create getContentfulData.yml

赵璟 před 2 roky
rodič
revize
054aef781d
1 změnil soubory, kde provedl 68 přidání a 0 odebrání
  1. 68 0
      .github/workflows/getContentfulData.yml

+ 68 - 0
.github/workflows/getContentfulData.yml

@@ -0,0 +1,68 @@
+name: Fetch data from contentful graphql
+
+on: workflow_dispatch
+
+jobs:
+  fetch-data:
+    runs-on: ubuntu-latest
+
+    steps:
+      - uses: actions/checkout@v2
+
+      # Define some variables for convenience
+      - name: Set up variables
+        env:
+          ACCESS_TOKEN: ${{ secrets.CONTENTFUL_GRAPHQLTOKEN }}
+          LOCALES: "en-US,zh-CN"
+          LIMIT: 100
+          SPACE_ID: ffrhttfighww
+          API_URL: https://graphql.contentful.com/content/v1/spaces/$SPACE_ID
+        run: |
+          echo "ACCESS_TOKEN=$ACCESS_TOKEN" >> $GITHUB_ENV
+          echo "LOCALES=$LOCALES" >> $GITHUB_ENV
+          echo "LIMIT=$LIMIT" >> $GITHUB_ENV
+          echo "API_URL=$API_URL" >> $GITHUB_ENV
+
+      # Define a function to generate query string from a file and variables
+      - name: Define query function
+        run: |
+          query() {
+            FILE=$1
+            LOCALE=$2
+            SKIP=$3
+            jq -n --argfile query $FILE --arg locale $LOCALE --arg skip $SKIP '$query + {variables: {locale: $locale, skip: ($skip|tonumber)}}'
+          }
+
+      # Loop through different queries and locales
+      - name: Fetch data from contentful graphql
+        run: |
+          mkdir -p appmanage/static/json
+          IFS=',' read -ra LOCALE_ARRAY <<< "$LOCALES"
+          for QUERY_FILE in catalog_query.json product_query.json; do
+            for LOCALE in "${LOCALE_ARRAY[@]}"; do
+              # Get the output file name from the query file name and locale
+              OUTPUT_FILE=${QUERY_FILE%_query.json}_$LOCALE.json
+              SKIP=0
+              TOTAL=0
+              while [ $SKIP -le $TOTAL ]; do
+                # Generate the query string from the file and variables
+                QUERY=$(query $QUERY_FILE $LOCALE $SKIP)
+                # Send the query to the API and get the response
+                RESPONSE=$(curl -X POST \
+                  -H "Content-Type: application/json" \
+                  -H "Authorization: Bearer $ACCESS_TOKEN" \
+                  -d "$QUERY" \
+                  $API_URL)
+                # Append the items to the output file
+                echo $RESPONSE | jq '.data.productCollection.items' >> appmanage/static/json/$OUTPUT_FILE
+                # Update the total and skip values
+                TOTAL=$(echo $RESPONSE | jq '.data.productCollection.total')
+                SKIP=$((SKIP + LIMIT))
+              done
+            done
+          done
+
+      - name: Commit and push changes
+        uses: stefanzweifel/git-auto-commit-action@v4
+        with:
+          commit_message: Update catalog.json and product.json