Skip to content

Create temporary site and deploying #23

Create temporary site and deploying

Create temporary site and deploying #23

name: Create temporary site and deploying
on:
create:
workflow_dispatch:
jobs:
build:
if: ${{ github.ref_type == 'branch' && startsWith(github.ref_name, 'feature-') }}
runs-on: ubuntu-latest
environment: github-pages
strategy:
matrix:
node-version: [20.x]
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.ref_name }}
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Installing dependencies
run: sudo apt-get install -y jq
- name: Creating the new website's CNAME value on Cloudflare and creating a new repository
run: |
echo "CNAME_ID=$(echo -n ${{ github.run_id }} | sha256sum | cut -c-32)" >> $GITHUB_ENV
export REAL_ID=$(curl --request POST \
--url https://api.cloudflare.com/client/v4/zones/${{ secrets.CNAME_ZONE_ID }}/dns_records \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer ${{ secrets.CNAME_KEY }}' \
--header 'X-Auth-Email: [email protected]' \
--data '{
"content": "ghostvaibhav.github.io",
"name": "${{ github.ref_name }}",
"proxied": true,
"type": "CNAME",
"comment": "Temporary Feature Branch",
"id": "'"$CNAME_ID"'",
"ttl": 3600
}' | jq '.result.id')
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GENERATE_TEMPORARY_BRANCH_WEBSITE }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/orgs/Temporary-Feature-Branch/repos \
-d '{
"name": "${{ github.ref_name }}-Simple",
"description": '"$REAL_ID"',
"homepage": "https://github.com/GhostVaibhav/Simple",
"visibility": "public",
"has_issues": false,
"has_projects": false,
"has_wiki": false,
"auto_init": true
}'
- name: Installing dependencies
run: npm install
- name: Writing to environment variable
run: |
touch .env
echo 'REACT_APP_RECAPTCHA_KEY=${{ secrets.REACT_APP_RECAPTCHA_KEY }}' >> .env
- name: Build the website
run: npm run build
- name: Creating the CNAME file
run: echo '${{ github.ref_name }}.ghostvaibhav.com' > ./build/CNAME
- name: Pushes to another repository
uses: cpina/github-action-push-to-another-repository@main
env:
API_TOKEN_GITHUB: ${{ secrets.GENERATE_TEMPORARY_BRANCH_WEBSITE }}
with:
source-directory: 'build'
destination-github-username: 'Temporary-Feature-Branch'
destination-repository-name: '${{ github.ref_name }}-Simple'
user-email: [email protected]
target-branch: main
- name: Enabling GitHub Pages for the temporary repository
run: |
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GENERATE_TEMPORARY_BRANCH_WEBSITE }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/Temporary-Feature-Branch/${{ github.ref_name }}-Simple/pages \
-d '{"build_type":"legacy","source":{"branch":"main","path":"/"}}'