Merge pull request #51 from mikucat0309/main #93
This file contains 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: [main] | |
paths-ignore: | |
- 'docs/**' | |
- 'README.md' | |
repository_dispatch: | |
concurrency: deploy | |
env: | |
wranglerVersion: 3.4.0 | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
timeout-minutes: 60 | |
steps: | |
- name: Set name suffix | |
run: | | |
if [ -z "$OVERRIDE_NAME_SUFFIX" ] | |
then | |
echo $GH_OWNER | awk '{ print "NAME_SUFFIX=" tolower($0) }' >> ${GITHUB_ENV} | |
else | |
echo $OVERRIDE_NAME_SUFFIX | awk '{ print "NAME_SUFFIX=" tolower($0) }' >> ${GITHUB_ENV} | |
fi | |
env: | |
GH_OWNER: ${{ github.repository_owner }} | |
OVERRIDE_NAME_SUFFIX: ${{ vars.OVERRIDE_NAME_SUFFIX }} | |
- uses: actions/checkout@v3 | |
- 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 | |
cache: 'yarn' | |
- name: Create D1 database | |
uses: cloudflare/wrangler-action@2.0.0 | |
with: | |
wranglerVersion: ${{ env.wranglerVersion }} | |
command: d1 create ccip-${{ env.NAME_SUFFIX }} | |
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: | |
wranglerVersion: ${{ env.wranglerVersion }} | |
command: d1 list --json | jq -r '.[] | select(.name == "ccip-${{ env.NAME_SUFFIX }}") | .uuid' | awk '{print "d1_id="$1}' >> $GITHUB_ENV | |
apiToken: ${{ secrets.CF_API_TOKEN }} | |
preCommands: | | |
echo "*** pre commands ***" | |
apt-get update && apt-get -y install jq | |
echo "******" | |
env: | |
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} | |
- name: migrate D1 database | |
uses: cloudflare/wrangler-action@2.0.0 | |
with: | |
wranglerVersion: ${{ env.wranglerVersion }} | |
command: d1 migrations apply ccip-${{ env.NAME_SUFFIX }} | |
apiToken: ${{ secrets.CF_API_TOKEN }} | |
preCommands: | | |
echo "*** pre commands ***" | |
sed -i "s/__DATABASE_NAME__/ccip-${{ env.NAME_SUFFIX }}/g" wrangler.toml | |
sed -i "s/__DATABASE_ID__/${{ env.d1_id }}/g" wrangler.toml | |
echo "******" | |
env: | |
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} | |
- name: retrieve Terraform state KV namespace | |
uses: cloudflare/wrangler-action@2.0.0 | |
with: | |
wranglerVersion: ${{ env.wranglerVersion }} | |
command: kv:namespace list | jq -r '.[] | select( .title == "ccip-terraform-${{ env.NAME_SUFFIX }}-state" ) | .id' | awk '{print "tfstate_kv="$1}' >> $GITHUB_ENV | |
apiToken: ${{ secrets.CF_API_TOKEN }} | |
preCommands: | | |
echo "*** pre commands ***" | |
apt-get update && apt-get -y install jq | |
echo "******" | |
env: | |
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} | |
- name: Init | |
run: terraform init | |
working-directory: ./tf | |
- name: download Terraform state | |
uses: cloudflare/wrangler-action@2.0.0 | |
with: | |
wranglerVersion: ${{ env.wranglerVersion }} | |
command: kv:key get --namespace-id=${{ env.tfstate_kv }} terraform.tfstate > ./tf/terraform.tfstate | |
postCommands: | | |
echo "*** post commands ***" | |
chmod 777 ./tf/terraform.tfstate | |
echo "******" | |
apiToken: ${{ secrets.CF_API_TOKEN }} | |
env: | |
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} | |
if: ${{ env.tfstate_kv != '' }} | |
- name: Configure | |
run: terraform plan && terraform apply -auto-approve | |
working-directory: ./tf | |
env: | |
TF_VAR_cloudflare_account_id: ${{ secrets.CF_ACCOUNT_ID }} | |
TF_VAR_cloudflare_api_token: ${{ secrets.CF_API_TOKEN }} | |
TF_VAR_name_suffix: ${{ env.NAME_SUFFIX }} | |
TF_VAR_d1_id: ${{ env.d1_id }} | |
- name: retrieve Terraform state KV namespace | |
uses: cloudflare/wrangler-action@2.0.0 | |
with: | |
wranglerVersion: ${{ env.wranglerVersion }} | |
command: kv:namespace list | jq -r '.[] | select( .title == "ccip-terraform-${{ env.NAME_SUFFIX }}-state" ) | .id' | awk '{print "tfstate_kv="$1}' >> $GITHUB_ENV | |
apiToken: ${{ secrets.CF_API_TOKEN }} | |
preCommands: | | |
echo "*** pre commands ***" | |
apt-get update && apt-get -y install jq | |
echo "******" | |
env: | |
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} | |
if: ${{ env.tfstate_kv == '' }} | |
- name: store Terraform state | |
uses: cloudflare/wrangler-action@2.0.0 | |
with: | |
wranglerVersion: ${{ env.wranglerVersion }} | |
command: kv:key put --namespace-id=${{ env.tfstate_kv }} terraform.tfstate --path=./tf/terraform.tfstate | |
apiToken: ${{ secrets.CF_API_TOKEN }} | |
env: | |
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} | |
- name: Publish | |
uses: cloudflare/wrangler-action@2.0.0 | |
with: | |
wranglerVersion: ${{ env.wranglerVersion }} | |
apiToken: ${{ secrets.CF_API_TOKEN }} | |
preCommands: | | |
echo "*** pre commands ***" | |
yarn | |
yarn build | |
echo "******" | |
command: pages publish --project-name=ccip-${{ env.NAME_SUFFIX }} dist | |
env: | |
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} | |
COMMIT_HASH: ${{ github.sha }} |