|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +name: generative-ai-snippets |
| 16 | +on: |
| 17 | + push: |
| 18 | + branches: |
| 19 | + - main |
| 20 | + paths: |
| 21 | + - 'generative-ai/snippets/**' |
| 22 | + - '.github/workflows/generative-ai-snippets.yaml' |
| 23 | + pull_request: |
| 24 | + paths: |
| 25 | + - 'generative-ai/snippets/**' |
| 26 | + - '.github/workflows/generative-ai-snippets.yaml' |
| 27 | + pull_request_target: |
| 28 | + types: [labeled] |
| 29 | + paths: |
| 30 | + - 'generative-ai/snippets/**' |
| 31 | + - '.github/workflows/generative-ai-snippets.yaml' |
| 32 | + schedule: |
| 33 | + - cron: '0 0 * * 0' |
| 34 | +jobs: |
| 35 | + test: |
| 36 | + if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' |
| 37 | + runs-on: ubuntu-latest |
| 38 | + timeout-minutes: 120 |
| 39 | + permissions: |
| 40 | + contents: 'read' |
| 41 | + id-token: 'write' |
| 42 | + defaults: |
| 43 | + run: |
| 44 | + working-directory: 'generative-ai/snippets' |
| 45 | + steps: |
| 46 | + |
| 47 | + with: |
| 48 | + ref: ${{github.event.pull_request.head.sha}} |
| 49 | + - uses: 'google-github-actions/[email protected]' |
| 50 | + with: |
| 51 | + workload_identity_provider: 'projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider' |
| 52 | + service_account: '[email protected]' |
| 53 | + create_credentials_file: 'true' |
| 54 | + access_token_lifetime: 600s |
| 55 | + - id: secrets |
| 56 | + uses: 'google-github-actions/get-secretmanager-secrets@v1' |
| 57 | + with: |
| 58 | + secrets: |- |
| 59 | + caip_id:nodejs-docs-samples-tests/nodejs-docs-samples-ai-platform-caip-project-id |
| 60 | + location:nodejs-docs-samples-tests/nodejs-docs-samples-ai-platform-location |
| 61 | + |
| 62 | + with: |
| 63 | + node-version: 16 |
| 64 | + - name: Get npm cache directory |
| 65 | + id: npm-cache-dir |
| 66 | + shell: bash |
| 67 | + run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} |
| 68 | + - uses: actions/cache@v3 |
| 69 | + id: npm-cache |
| 70 | + with: |
| 71 | + path: ${{ steps.npm-cache-dir.outputs.dir }} |
| 72 | + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} |
| 73 | + restore-keys: | |
| 74 | + ${{ runner.os }}-node- |
| 75 | + - name: install repo dependencies |
| 76 | + run: npm install |
| 77 | + working-directory: . |
| 78 | + - name: install directory dependencies |
| 79 | + run: npm install |
| 80 | + - run: npm run build --if-present |
| 81 | + - name: set env vars for scheduled run |
| 82 | + if: github.event.action == 'schedule' |
| 83 | + run: | |
| 84 | + echo "MOCHA_REPORTER_SUITENAME=generative-ai-snippets" >> $GITHUB_ENV |
| 85 | + echo "MOCHA_REPORTER_OUTPUT=${{github.run_id}}_sponge_log.xml" >> $GITHUB_ENV |
| 86 | + echo "MOCHA_REPORTER=xunit" >> $GITHUB_ENV |
| 87 | + - run: npm test |
| 88 | + env: |
| 89 | + LOCATION: ${{ steps.secrets.outputs.location }} |
| 90 | + CAIP_PROJECT_ID: ${{ steps.secrets.outputs.caip_id }} |
| 91 | + - name: upload test results for FlakyBot workflow |
| 92 | + if: github.event.action == 'schedule' && always() |
| 93 | + uses: actions/upload-artifact@v3 |
| 94 | + env: |
| 95 | + MOCHA_REPORTER_OUTPUT: "${{github.run_id}}_sponge_log.xml" |
| 96 | + with: |
| 97 | + name: test-results |
| 98 | + path: generative-ai/snippets/${{ env.MOCHA_REPORTER_OUTPUT }} |
| 99 | + retention-days: 1 |
| 100 | + flakybot: |
| 101 | + permissions: |
| 102 | + contents: 'read' |
| 103 | + id-token: 'write' |
| 104 | + if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail |
| 105 | + uses: ./.github/workflows/flakybot.yaml |
| 106 | + needs: [test] |
0 commit comments