> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fly.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Laravel GitHub Actions: CICD

Laravel applications are alive and bustling with feature updates and revisions. You'll want to add your Laravel Fly App in a GitHub repository for safe-versions-keeping, healthy-code-reviews, and good-old team-code management.

Once you've set up a repository for your Fly App, you can automate your team's deployment workflow with [GitHub Actions](https://docs.github.com/en/actions)---a really neat way to automate most things you'd manually do during and after code reviews!

## Initialization

In this section, you'll create a new Laravel application, configure and deploy it as a Fly App, and finally add it to a GitHub repository.

1. Start with a [clean slate](/laravel): create a new Laravel project and configure it as a deployable Fly app by using the `flyctl` launch command:

   ```bash theme={null}
   # Initialize your Laravel project
   composer create-project laravel/laravel fly-laravel
   cd fly-laravel

   # Auto generate your `fly.toml` configuration file:
   # Add in the ams region with --region
   # Auto deploy using --now while your at it!
   flyctl launch --region ams --now
   ```

2. Create a new github repository for your current Laravel Fly App. You can create one either through:

   1. **GitHub Console** - Visiting your [create a new repository](https://github.com/new) page in your console

   2. **GitHub CLI** - run the create repo command:

   ```bash theme={null}
   gh repo create <repository-name> --public
   ```

3. Finally, initialize your Laravel Fly App's directory as part of a git repository, and point its remote to the recently created repository above
   ```bash theme={null}
   git init
   git remote add origin git@github.com:<username>/<repository-name>.git
   ```

## GitHub CI Action: Auto Deploy to Fly.io

Once you have your Laravel application set up with a github repository, you can configure some GitHub Actions to auto deploy your changes.

In this guide, you'll be using `Fly.io`'s very own [GitHub action template here](https://github.com/superfly/flyctl-actions) to help deploy your application.

1. Generate a `Fly token` with `fly tokens deploy`

2. Then save your newly generated `FLY_API_TOKEN` in your github repository either through the GitHub Console or GitHub CLI:

   1. **GitHub Console** - Open your repository's Settings tab, then add a secret named `FLY_API_TOKEN` under the Secrets section.

   2. **GitHub CLI** - run the command below

   <CodeGroup>
     ```bash cmd theme={null}
     gh secret set FLY_API_TOKEN --repos <username>/<repository-name>
     ```

     ```output output theme={null}
     ? Paste your secret *******************************************

     ✓ Set Actions secret FLY_API_TOKEN for <username>/<repository-name>
     ```
   </CodeGroup>

3. Once you have your `FLY_API_TOKEN` safely stored as a secret in your github repository, proceed with creating a GitHub workflow yml file in `.github/workflows/main.yml`:

   ```bash theme={null}
   mkdir -p .github/workflows
   touch .github/workflows/main.yml
   ```

   ```yml theme={null}
   name: Fly Deploy
   on: [push]
   env:
     FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
   jobs:
     deploy:
         name: Deploy app
         runs-on: ubuntu-latest
         steps:
           - uses: actions/checkout@v2
           - uses: superfly/flyctl-actions/setup-flyctl@master
           - run: flyctl deploy
   ```

   Let's go through each line above, shall we?

   * **name: Fly Deploy** => This is the Workflow name
   * **on: \[push]** => Any push action to the repository triggers the current workflow
   * **env: \`FLY\_API\_TOKEN\`** => Environment variable set for the GitHub Actions container
   * **jobs:** => This is the list of jobs to run for the workflow
     * **name: Deploy app** => Each job would have a name
     * **runs-on: ubuntu-latest** => Each job would have an container environment we want to run its virtual machine on
     * **steps:** => Each job would have a list of steps, see below:
       * **uses: actions/checkout\@v2** => This is a pre-made GitHub Action. It allows checking out of your repository into the virtual machine spun up for the job
       * **uses: superfly/flyctl-actions/setup-flyctl\@master** => This is one of Fly.io's pre-made GitHub action. It sets up \`flyctl\` in the virtual machine spun up for the job
       * **run: flyctl deploy** => Need I say more?

4. Save and push your changes with:
   ```bash theme={null}
   git add .
   git commit -m "Configure auto-deploy through GitHub Actions"
   git push
   ```

5. Visit the Actions tab of your repository and see how your deployment fares!
