wildebeest/.github/workflows/deploy.yml

113 wiersze
3.8 KiB
YAML

name: Deploy
on:
push:
branches:
- main
repository_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- name: Ensure CF_DEPLOY_DOMAIN and CF_ZONE_ID are defined
run: |
if [ -z "$CF_ZONE_ID" ]
then
echo "CF_ZONE_ID not defined"
exit 1
fi
if [ -z "$CF_DEPLOY_DOMAIN" ]
then
echo "CF_DEPLOY_DOMAIN not defined"
exit 1
fi
env:
CF_ZONE_ID: ${{ secrets.CF_ZONE_ID }}
CF_DEPLOY_DOMAIN: ${{ secrets.CF_DEPLOY_DOMAIN }}
# this is needed to get the lowercase version of the actor name
# TODO: switch to some lowercase function in the future when Actions supports it
- name: Set lowercase actor name
run: |
echo $GH_ACTOR | awk '{ print "ACTOR_LOWER=" tolower($0) }' >> ${GITHUB_ENV}
env:
GH_ACTOR: ${{ github.actor }}
- uses: actions/checkout@v2
- uses: hashicorp/setup-terraform@v2
- name: Install package
run: |
sudo apt-get -y install jq
- name: Setup node.js
uses: actions/setup-node@v3
with:
node-version: 18
- name: Create D1 database
uses: cloudflare/wrangler-action@2.0.0
with:
command: d1 create wildebeest-${{ env.ACTOR_LOWER }}
apiToken: ${{ secrets.CF_API_TOKEN }}
continue-on-error: true
env:
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
- name: retrieve D1 database
uses: cloudflare/wrangler-action@2.0.0
with:
command: d1 list | grep wildebeest-${{ env.ACTOR_LOWER }} | awk '{print "d1_id="$2}' >> $GITHUB_ENV
apiToken: ${{ secrets.CF_API_TOKEN }}
env:
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
- name: migrate D1 database
uses: cloudflare/wrangler-action@2.0.0
with:
command: d1 migrations apply wildebeest-${{ env.ACTOR_LOWER }}
apiToken: ${{ secrets.CF_API_TOKEN }}
preCommands: |
echo "*** pre commands ***"
echo -e "[[d1_databases]]\nbinding=\"DATABASE\"\ndatabase_name=\"wildebeest-${{ env.ACTOR_LOWER }}\"\ndatabase_id=\"${{ env.d1_id }}\"" >> wrangler.toml
echo "******"
env:
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
- name: retrieve Zero Trust organization
run: |
auth_domain=$(curl https://api.cloudflare.com/client/v4/accounts/${{ secrets.CF_ACCOUNT_ID }}/access/organizations \
-H 'Authorization: Bearer ${{ secrets.CF_API_TOKEN }}' | jq -r '.result.auth_domain')
printf "auth_domain=$auth_domain" >> $GITHUB_ENV
- name: Init
run: terraform init
working-directory: ./tf
- name: Configure
run: terraform plan && terraform apply -auto-approve
continue-on-error: true
working-directory: ./tf
env:
TF_VAR_cloudflare_account_id: ${{ secrets.CF_ACCOUNT_ID }}
TF_VAR_cloudflare_api_token: ${{ secrets.CF_API_TOKEN }}
TF_VAR_cloudflare_zone_id: ${{ secrets.CF_ZONE_ID }}
TF_VAR_cloudflare_deploy_domain: ${{ secrets.CF_DEPLOY_DOMAIN }}
TF_VAR_gh_username: ${{ env.ACTOR_LOWER }}
TF_VAR_d1_id: ${{ env.d1_id }}
TF_VAR_access_auth_domain: ${{ env.auth_domain }}
- name: Publish
uses: cloudflare/wrangler-action@2.0.0
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
preCommands: |
echo "*** pre commands ***"
yarn
yarn build
cp -rv ./frontend/dist/* .
echo "******"
command: pages publish --project-name=wildebeest-${{ env.ACTOR_LOWER }} .
env:
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}