Foobar #90
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 Preview | |
on: | |
pull_request: | |
types: [opened, synchronize, reopened] | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
environment: prod | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Log in to GitHub Container Registry | |
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin | |
- name: Build Docker image | |
run: | | |
docker build -t ghcr.io/${{ github.repository }}/t7lab-website:${{ github.head_ref }} . | |
docker push ghcr.io/${{ github.repository }}/t7lab-website:${{ github.head_ref }} | |
- name: Setup SSH | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ secrets.SSH_GITHUB_PRIVATE }} | |
host-keys: ${{ vars.SSH_HOST }} | |
- name: Add Host to known_hosts | |
run: | | |
ssh-keyscan -H ${{ vars.SSH_HOST }} >> ~/.ssh/known_hosts | |
- name: Deploy Docker Container on Hetzner | |
run: | | |
ssh github@${{ vars.SSH_HOST }} "docker login ghcr.io -u '${{ github.actor }}' --password '${{ secrets.GITHUB_TOKEN }}' && \ | |
docker stop preview-${{ github.head_ref }} || true && \ | |
docker rm preview-${{ github.head_ref }} || true && \ | |
docker pull ghcr.io/${{ github.repository }}/t7lab-website:${{ github.head_ref }} && \ | |
docker run -d --name preview-${{ github.head_ref }} -p 8085:8080 ghcr.io/${{ github.repository }}/t7lab-website:${{ github.head_ref }}" | |
- name: Configure NGINX for Preview | |
run: | | |
scp ./scripts/setup_nginx.sh github@${{vars.SSH_HOST}}:/home/github/setup_nginx.sh | |
ssh github@${{ vars.SSH_HOST }} "chmod +x /home/github/setup_nginx.sh && /home/github/setup_nginx.sh ${{ github.head_ref }} 8085" | |
- name: Check and Add DNS Record in Cloudflare | |
run: | | |
SUBDOMAIN="${{ github.head_ref }}--preview.t7lab.com" | |
# First, check if the record already exists | |
CHECK_RESPONSE=$(ssh github@${{ vars.SSH_HOST }} 'curl -s -w "\n%{http_code}" --request GET \ | |
--url "https://api.cloudflare.com/client/v4/zones/${{ vars.CLOUDFLARE_ZONE_ID }}/dns_records?type=A&name='$SUBDOMAIN'" \ | |
--header "X-Auth-Email: ${{ vars.CLOUDFLARE_EMAIL }}" \ | |
--header "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_KEY }}" \ | |
--header "Content-Type: application/json"') | |
CHECK_HTTP_STATUS=$(echo "$CHECK_RESPONSE" | tail -n1) | |
CHECK_BODY=$(echo "$CHECK_RESPONSE" | sed '$ d') | |
if [ "$CHECK_HTTP_STATUS" -eq 200 ]; then | |
RECORD_COUNT=$(echo "$CHECK_BODY" | jq '.result | length') | |
if [ "$RECORD_COUNT" -gt 0 ]; then | |
echo "DNS record for $SUBDOMAIN already exists. Skipping creation." | |
exit 0 | |
fi | |
else | |
echo "Failed to check existing DNS records. Status: $CHECK_HTTP_STATUS" | |
echo "Response: $CHECK_BODY" | |
exit 1 | |
fi | |
# Create a new record if it doesn't exist | |
TIMESTAMP=$(date +%s) | |
CREATE_RESPONSE=$(ssh github@${{ vars.SSH_HOST }} 'curl -s -w "\n%{http_code}" --request POST \ | |
--url https://api.cloudflare.com/client/v4/zones/${{ vars.CLOUDFLARE_ZONE_ID }}/dns_records \ | |
--header "X-Auth-Email: ${{ vars.CLOUDFLARE_EMAIL }}" \ | |
--header "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_KEY }}" \ | |
--header "Content-Type: application/json" \ | |
--data '"'"'{"name":"'$SUBDOMAIN'","proxied":true,"settings":{},"tags":[],"ttl":3600,"content":"${{ vars.SSH_HOST }}","type":"A","comment":"Created at '$TIMESTAMP'"}'"'"'') | |
CREATE_HTTP_STATUS=$(echo "$CREATE_RESPONSE" | tail -n1) | |
CREATE_BODY=$(echo "$CREATE_RESPONSE" | sed '$ d') | |
echo "Create HTTP Status: $CREATE_HTTP_STATUS" | |
echo "Create Response Body: $CREATE_BODY" | |
if [ "$CREATE_HTTP_STATUS" -eq 200 ]; then | |
echo "DNS record created successfully for $SUBDOMAIN" | |
else | |
echo "Failed to create DNS record for $SUBDOMAIN" | |
exit 1 | |
fi |