Expose your web server on GitHub Actions to the internet using Cloudflare Tunnel
Tags: actionshackathon21, cloudflare, webdev, tunnel
Published: 2021-12-09 04:45:06

### My Workflow
GitHub Actions runners are firewalled from the internet, so you can't test your web server outside of the runners. What if you need to test your web server on GitHub Actions interactively?

This GitHub Action installs the `cloudflared` Cloudflare Tunnel client and allows you to tunnel connections so that you can now access your server inside the GitHub Actions runners to the internet.

This is an example workflow that tunnels the Python Simple HTTP server so that it is accessible over the internet.

```
name: Test setup-cloudflared
on:
  push:
    paths:
      - .github/workflows/test.yml
      
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Setup cloudflared using setup-cloudflared
        uses: AnimMouse/setup-cloudflared@v1
        with:
          cloudflare_tunnel_certificate: ${{ secrets.CLOUDFLARE_TUNNEL_CERTIFICATE }}
          cloudflare_tunnel_credential: ${{ secrets.CLOUDFLARE_TUNNEL_CREDENTIAL }}
          cloudflare_tunnel_configuration: ${{ secrets.CLOUDFLARE_TUNNEL_CONFIGURATION }}
          cloudflare_tunnel_id: ${{ secrets.CLOUDFLARE_TUNNEL_ID }}
          
      - name: Test cloudflared installed by setup-cloudflared using Python HTTP server for 5 minutes
        run: timeout 5m python -m http.server 8000 || true
        
      - name: Shutdown cloudflared using setup-cloudflared/shutdown
        if: always()
        uses: AnimMouse/setup-cloudflared/shutdown@v1
```

### Submission Category: 
Maintainer Must-Haves

### Yaml File or Link to Code
```
name: Setup cloudflared
description: Setup/Install Cloudflare Tunnel client for GitHub Actions
branding:
  icon: cloud
  color: orange
inputs:
  cloudflare_tunnel_certificate:
    description: Cloudflare Tunnel Certificate (cert.pem)
    required: true
  cloudflare_tunnel_credential:
    description: Cloudflare Tunnel Credential encoded in base64 (deadbeef-1234-4321-abcd-123456789ab.json)
    required: true
  cloudflare_tunnel_configuration:
    description: Cloudflare Tunnel Configuration encoded in base64 (config.yml)
    required: true
  cloudflare_tunnel_id:
    description: Cloudflare Tunnel ID (deadbeef-1234-4321-abcd-123456789ab)
    required: true
  autostart:
    description: Autostart Cloudflare Tunnel
    required: false
    default: true
    
runs:
  using: composite
  steps:
    - name: Download cloudflared for Linux
      shell: bash
      working-directory: ${{ runner.temp }}
      run: aria2c -x 16 "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64"
      
    - name: Install cloudflared
      shell: bash
      working-directory: ${{ runner.temp }}
      run: |
        chmod +x cloudflared-linux-amd64
        mv cloudflared-linux-amd64 /usr/local/bin/cloudflared
        
    - name: Login to Cloudflare Tunnel client
      shell: bash
      env:
        cloudflare_tunnel_certificate: ${{ inputs.cloudflare_tunnel_certificate }}
        cloudflare_tunnel_credential: ${{ inputs.cloudflare_tunnel_credential }}
        cloudflare_tunnel_configuration: ${{ inputs.cloudflare_tunnel_configuration }}
        cloudflare_tunnel_id: ${{ inputs.cloudflare_tunnel_id }}
      run: |
        mkdir ~/.cloudflared/
        echo $cloudflare_tunnel_certificate > ~/.cloudflared/cert.pem
        echo $cloudflare_tunnel_credential | base64 -d > ~/.cloudflared/${cloudflare_tunnel_id}.json
        echo $cloudflare_tunnel_configuration | base64 -d > ~/.cloudflared/config.yml
        
    - name: Run Cloudflare Tunnel
      if: inputs.autostart == 'true'
      shell: bash
      env:
        cloudflare_tunnel_id: ${{ inputs.cloudflare_tunnel_id }}
      run: |
        nohup cloudflared tunnel run > ${RUNNER_TEMP}/cloudflared.log 2>&1 &
        echo $! > ${RUNNER_TEMP}/cloudflared.pid
```
{% github https://github.com/AnimMouse/setup-cloudflared %}

### Additional Resources / Info
{% github https://github.com/cloudflare/cloudflared %}

Used by:
{% github https://github.com/AnimMouse/SOCKS5-proxy-actions %}