improvements #11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy | |
| on: | |
| push: | |
| branches: | |
| - "*" | |
| jobs: | |
| prepare: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| secrets: ${{ steps.extract-secrets.outputs.secrets }} | |
| env_flag: ${{ steps.check-env.outputs.env_flag }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Extract secrets | |
| id: extract-secrets | |
| run: | | |
| SECRETS_LIST=$(echo '${{ toJSON(secrets) }}' | jq -r 'to_entries | map(select(.key | startswith("github_") | not) | select(.key != "CLOUDFLARE_API_TOKEN") | .key) | tostring') | |
| echo "secrets=$SECRETS_LIST" >> $GITHUB_OUTPUT | |
| - name: Check wrangler.toml for environments | |
| id: check-env | |
| run: | | |
| BRANCH_NAME=${GITHUB_REF#refs/heads/} | |
| if [ ! -f "wrangler.toml" ]; then | |
| echo "env_flag=" >> $GITHUB_OUTPUT | |
| exit 0 | |
| fi | |
| if grep -q "\\[env\\.${BRANCH_NAME}\\]" wrangler.toml; then | |
| echo "env_flag=--env ${BRANCH_NAME}" >> $GITHUB_OUTPUT | |
| else | |
| echo "env_flag=" >> $GITHUB_OUTPUT | |
| fi | |
| set-secrets: | |
| needs: prepare | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| secret: ${{ fromJson(needs.prepare.outputs.secrets) }} | |
| # Allow all parallel jobs to run even if one fails | |
| fail-fast: false | |
| # Run up to 10 parallel jobs | |
| max-parallel: 10 | |
| steps: | |
| - name: Install wrangler | |
| run: npm install -g wrangler | |
| - name: Set secret | |
| env: | |
| CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| SECRET_VALUE: ${{ secrets[matrix.secret] }} | |
| run: | | |
| if [ -n "${{ needs.prepare.outputs.env_flag }}" ]; then | |
| echo "Setting ${{ matrix.secret }} for environment ${{ needs.prepare.outputs.env_flag }}" | |
| echo "$SECRET_VALUE" | wrangler secret put "${{ matrix.secret }}" ${{ needs.prepare.outputs.env_flag }} | |
| else | |
| echo "Setting ${{ matrix.secret }} for default environment" | |
| echo "$SECRET_VALUE" | wrangler secret put "${{ matrix.secret }}" | |
| fi | |
| deploy: | |
| needs: [prepare, set-secrets] | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: "20" | |
| - name: Install Dependencies | |
| run: npm install | |
| - name: Deploy | |
| uses: cloudflare/wrangler-action@v3 | |
| with: | |
| apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| command: deploy ${{ needs.prepare.outputs.env_flag }} |