Deploying a Repository Inside a GitHub Organization to Vercel Using GitHub Actions
Published on March 28, 2023
Vercel is a popular cloud service platform designed for hosting Next.js applications. While it offers free hosting for personal repositories, it doesn't allow deploying from repositories inside a GitHub Organization unless you subscribe to a paid plan.
In this blog post, we'll create a GitHub Action that duplicates the content of a repository inside a GitHub Organization to a repository in your personal GitHub account, allowing you to leverage Vercel's free hosting services.
NOTE: This approach may not be suitable for all scenarios, and ensuring proper security measures when dealing with sensitive data is essential.
##Prerequisites
- A personal GitHub account
- A repository within a GitHub Organization (source repository)
- A separate repository in your personal account to host the copied files (destination repository)
##Step 1: Generate SSH Keys
You'll need to generate an SSH Deploy Key to securely push your content from the organization's repository to the external repository. Although you can also use a Personal Access Token, an SSH deploy key is recommended as it minimizes the impact of potential security breaches.
ssh-keygen -t ed25519 -C "$(git config user.email)" -N "" -f github-<desitination-repo-name>
Replace the <desitination-repo-name>
with the name of your destination repository and run the command. Afterwards, you should now have both public and private key files:
github-<desitination-repo-name>.pub
(public)github-<desitination-repo-name>
(private)
##Step 2: Add the private key to the Source Repository
- Visit the source repository's GitHub page.
- Click on "Settings" in the repository (not account settings).
- In the left-hand pane, click "Secrets", then "Actions".
- Click on "New repository secret".
- Name it
SSH_DEPLOY_KEY
and paste the contents of the private key file. - Click "Save".
##Step 3: Add the public key to the Destination Repository
- Visit the destination repository's GitHub page.
- Click on "Settings" in the repository (not account settings).
- In the left-hand side pane, click on "Deploy keys."
- Click on "Add deploy key".
- Paste the contents of the public key file.
- Enable "Allow write access".
- Click "Save".
##Step 4: Disable GitHub Actions on the Destination Repository
- Visit the destination repository's GitHub page.
- Click on "Settings" in the repository (not account settings).
- Click on "Actions" and then "General".
- Select "Disable actions".
- Click "Save".
##Step 5: Create a GitHub Action Workflow
Create a new GitHub Action workflow in your source repository by adding a YAML file in the .github/workflows
directory. Name the file push-to-external-repo.yml
and paste the following content:
.github/workflows/push-to-external-repo.yml
1name: (main) push to external repo2on:3 push:4 branches:5 - main6jobs:7 push-to-external-repo:8 runs-on: ubuntu-latest9 steps:10 - name: checkout repository11 uses: actions/checkout@v312 - name: push to external repository13 uses: peaceiris/actions-gh-pages@v314 with:15 deploy_key: ${{ secrets.SSH_DEPLOY_KEY }}16 publish_dir: .17 external_repository: <your-username>/<destination-repo-name>18 publish_branch: main19 allow_empty_commit: true
Replace <your-username>
with your GitHub username and <destination-repo-name>
with the name of the external repository in your personal account.
This action will copy everything from your organization's repository to your personal repository, including your GitHub Action workflows, so follow Step 4 to disable GitHub Actions on the destination repository.
##Step 6: Run your workflow
Push something to your main
branch and see it be pushed to your <your-username>/<destination-repo-name>
repository.
##Step 7: Set up a project on Vercel
- On Vercel's "New Project" page, choose the account linked to the project under the "Import Git Repository" section.
- Find your personal repository in the list and select "Import".
- Vercel will automatically detect the framework and any necessary build settings. You can configure project settings, including build and development settings and environment variables, at this stage or later.
- Click the "Deploy" button. Vercel will create the project and deploy it based on the chosen configurations.