<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Alex Justesen</title>
    <description>The latest articles on Forem by Alex Justesen (@alexjustesen).</description>
    <link>https://forem.com/alexjustesen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F753841%2F471f9713-a5a7-48ca-bae4-3eb152874617.jpeg</url>
      <title>Forem: Alex Justesen</title>
      <link>https://forem.com/alexjustesen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/alexjustesen"/>
    <language>en</language>
    <item>
      <title>Learn from my wasted time, Laravel gate without a user</title>
      <dc:creator>Alex Justesen</dc:creator>
      <pubDate>Thu, 25 Nov 2021 17:12:04 +0000</pubDate>
      <link>https://forem.com/alexjustesen/learn-from-my-wasted-time-laravel-gate-without-a-user-4ldi</link>
      <guid>https://forem.com/alexjustesen/learn-from-my-wasted-time-laravel-gate-without-a-user-4ldi</guid>
      <description>&lt;p&gt;How to write a gate for an optional user, don't be like me and waste hours on it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--by5gTo9s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ht62szstbjm62m9vf2sb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--by5gTo9s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ht62szstbjm62m9vf2sb.png" alt="Image description" width="880" height="751"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>gates</category>
    </item>
    <item>
      <title>Laravel CI/CD Pipeline w/ Lasso and GitHub Actions</title>
      <dc:creator>Alex Justesen</dc:creator>
      <pubDate>Mon, 15 Nov 2021 17:50:00 +0000</pubDate>
      <link>https://forem.com/alexjustesen/laravel-cicd-pipeline-w-lasso-and-github-actions-53gm</link>
      <guid>https://forem.com/alexjustesen/laravel-cicd-pipeline-w-lasso-and-github-actions-53gm</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Last updated: November, 15th, 2021&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;TL;DR: &lt;a href="https://github.com/alexjustesen/lasso-ci-cd" rel="noopener noreferrer"&gt;Example Repository&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This guide will walk you though how I use GitHub Actions to create a CI/CD pipeline for &lt;a href="https://laravel.com/" rel="noopener noreferrer"&gt;Laravel&lt;/a&gt; applications. We'll use &lt;a href="https://getlasso.dev/" rel="noopener noreferrer"&gt;Lasso&lt;/a&gt; from Sam Carré to compile Webpack assets and upload them to a S3 storage provider and then trigger a deployment using the workflow.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Warning: This article is very much a work-in-progress and in need of polishing my brain dump.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is CI/CD?
&lt;/h3&gt;

&lt;p&gt;"CI/CD" is Continuous (Integeration or Iteration) / Continuous (Development, Delivery or Deployment) is a process of developing, testing and deploying your code. You'll also likely see this referred to as an "Agile Development Cycle".&lt;/p&gt;

&lt;p&gt;I recently answered a &lt;a href="https://www.reddit.com/r/laravel/comments/j9fsa4/how_to_manage_and_maintain_laravel_application/g8kzlv1?utm_source=share&amp;amp;utm_medium=web2x&amp;amp;context=3" rel="noopener noreferrer"&gt;Reddit&lt;/a&gt; thread which sparked this whole thing. If you're still unsure what CI/CD is I suggest doing some &lt;a href="https://googlethatforyou.com/?q=what%20is%20ci%2Fcid" rel="noopener noreferrer"&gt;Googling&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Assumptions
&lt;/h3&gt;

&lt;p&gt;Let's all get on the same page so we don't make a 🍑 out of you and me.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You're using GitHub as your source code management (SCM) platform and/or want to use GitHub Actions.&lt;/li&gt;
&lt;li&gt;You're using Laravel and that you're familiar with Laravel's file system and in specific using the S3 driver.&lt;/li&gt;
&lt;li&gt;You compile your assets using Laravel Mix and they get published into the default /public directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Branching
&lt;/h3&gt;

&lt;p&gt;For this example we're going to use a pretty simple main, develop and feature branching model. Develop will be our default branch where all code is based from and main will be our production code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwac-cdn.atlassian.com%2Fdam%2Fjcr%3Aa9cea7b7-23c3-41a7-a4e0-affa053d9ea7%2F04%2520%281%29.svg%3FcdnVersion%3D1315" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwac-cdn.atlassian.com%2Fdam%2Fjcr%3Aa9cea7b7-23c3-41a7-a4e0-affa053d9ea7%2F04%2520%281%29.svg%3FcdnVersion%3D1315" alt="Gitflow workflow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Source: &lt;a href="https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow" rel="noopener noreferrer"&gt;https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Process
&lt;/h3&gt;

&lt;p&gt;We're going to go into some detail but the overall summary of the CI/CD pipeline looks like this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a new branch off &lt;code&gt;develop&lt;/code&gt; to do your work in.&lt;/li&gt;
&lt;li&gt;Create a pull request (PR) to bring those changes into the &lt;code&gt;develop&lt;/code&gt; branch.&lt;/li&gt;
&lt;li&gt;On the creation of the PR into our &lt;code&gt;develop&lt;/code&gt; branch run our builds and tests.&lt;/li&gt;
&lt;li&gt;When merged into &lt;code&gt;develop&lt;/code&gt; run a workflow to compile and upload the assets to an S3 bucket using Lasso and deploy the code.&lt;/li&gt;
&lt;li&gt;Once you have enough code in &lt;code&gt;develop&lt;/code&gt; for a release, open a PR into the &lt;code&gt;main&lt;/code&gt; branch and we'll repeat steps 3 and 4 for production.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Configuring
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Installing Dependencies
&lt;/h3&gt;

&lt;p&gt;First we need to require a couple of composer packages.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Require Amazon S3 &lt;code&gt;composer require league/flysystem-aws-s3-v3 ~1.0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Require Lasso &lt;code&gt;composer require sammyjo20/lasso&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Configuring Laravel
&lt;/h3&gt;

&lt;p&gt;Next we need to update Laravel's .env file with additional S3 driver options and the Lasso environment. &lt;a href="https://github.com/alexjustesen/lasso-ci-cd/blob/develop/.env.example" rel="noopener noreferrer"&gt;Example .env file&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add &lt;code&gt;AWS_ENDPOINT=&lt;/code&gt; &lt;code&gt;after AWS_BUCKET&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;LASSO_ENV=&lt;/code&gt; after &lt;code&gt;APP_&lt;/code&gt; variables.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Configuring Lasso
&lt;/h3&gt;

&lt;p&gt;Next let's get Lasso configured, for a full explaination of the settings go to the Lasso &lt;a href="https://github.com/Sammyjo20/Lasso#configuration" rel="noopener noreferrer"&gt;readme&lt;/a&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Publish Lasso's config file &lt;code&gt;php artisan vendor:publish --tag=lasso-config&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In &lt;code&gt;config/lasso.php&lt;/code&gt; change &lt;code&gt;'disk' =&amp;gt; 'assets'&lt;/code&gt; to &lt;code&gt;'disk' =&amp;gt; 's3'&lt;/code&gt; to use the S3 drivers.&lt;/li&gt;
&lt;li&gt;Optional, change &lt;code&gt;upload_to&lt;/code&gt; if you plan on using the same bucket for multiple sites. You'll notice in this repository I've set it to &lt;code&gt;lasso_ci_cd/lasso&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Configure .gitignore
&lt;/h3&gt;

&lt;p&gt;Since the whole point of this is to automate our deployments and no longer commit our assets to GitHub we need to update our .gitignore file as well.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add Lasso's temp directory &lt;code&gt;.lasso&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Add our public asset directories and any additional directories and files.

&lt;ul&gt;
&lt;li&gt;/public/css/*&lt;/li&gt;
&lt;li&gt;/public/img/*&lt;/li&gt;
&lt;li&gt;/public/js/*&lt;/li&gt;
&lt;li&gt;/public/favicon.ico&lt;/li&gt;
&lt;li&gt;/public/mix-manifest.json&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  GitHub Actions
&lt;/h2&gt;

&lt;p&gt;To get us started we're going to create and run two workflows, one to do our testing (CI) and one to do our deploying (CD).&lt;/p&gt;

&lt;h3&gt;
  
  
  Continuous Improvement Workflow (Test)
&lt;/h3&gt;

&lt;p&gt;The goal of this workflow is to run our build and tests when a PR is opened and when any changes are made to code where a PR is already open.&lt;/p&gt;

&lt;h4&gt;
  
  
  Workflow Additions
&lt;/h4&gt;

&lt;p&gt;We're only going to add one section here to make sure our NPM dependencies install and package correctly. After the &lt;code&gt;Copy .env&lt;/code&gt; step add the &lt;code&gt;Install npm dependencies and package&lt;/code&gt; step to your workflow.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This workflow can be found here: &lt;a href="https://github.com/alexjustesen/lasso-ci-cd/blob/develop/.github/workflows/test.yml" rel="noopener noreferrer"&gt;test.yml&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Continuous Deployment Workflow (Deploy)
&lt;/h3&gt;

&lt;p&gt;The goal of this workflow is to run when a PR is merged into develop or mail, run our tests, compile and upload assets with Lasso and finally deploy the code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This workflow can be found here: &lt;a href="https://github.com/alexjustesen/lasso-ci-cd/blob/develop/.github/workflows/deploy.yml" rel="noopener noreferrer"&gt;deploy.yml&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Using Secrets
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Important: Never hard code passwords or tokens into your code, use secrets.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before we can use secrets in our workflow we need to create them in the repository. Go to Settings -&amp;gt; Secrets in your repository and add the following variables.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;AWS_ACCESS_KEY_ID&lt;/td&gt;
&lt;td&gt;The public key to your account or API user.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AWS_SECRET_ACCESS_KEY&lt;/td&gt;
&lt;td&gt;The secret key to your account or API user.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AWS_DEFAULT_REGION&lt;/td&gt;
&lt;td&gt;The default region where you bucket resides.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AWS_BUCKET&lt;/td&gt;
&lt;td&gt;The bucket's name.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AWS_ENDPOINT&lt;/td&gt;
&lt;td&gt;The endpoint should be a fully qualified domain name including the protocol (https://).&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: You can use any s3 compliant API service, I use Backblaze's B2 service because it's cheaper than AWS.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In addition to the changes we made to test our NPM dependencies we'll add additional steps to complete the process.&lt;/p&gt;

&lt;p&gt;We're going to add a new step to compile and upload our assets to our S3 bucket and we'll call it &lt;code&gt;Publish assets with Lasso&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Deployment
&lt;/h2&gt;

&lt;p&gt;Lastly you need to add &lt;code&gt;php artisan lasso:pull&lt;/code&gt; to your deployment script, I suggest doing this prior to clearing or filling any caches.&lt;/p&gt;

&lt;p&gt;Almost forgot, don't forget to update your S3 variables in your &lt;code&gt;.env&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Well, that's pretty much it. At this point you should be up and running, should you have an issues getting this setup comment below or reach out on &lt;a href="https://twitter.com/alexjustesen" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus
&lt;/h3&gt;

&lt;p&gt;I use &lt;a href="https://ploi.io/register?referrer=Q6l9H3OiyYW58yEcwjy8" rel="noopener noreferrer"&gt;Ploi.io&lt;/a&gt; (referral link) and Forge to host my Laravel applications.&lt;/p&gt;

&lt;p&gt;The steps called &lt;code&gt;Trigger develop deployment&lt;/code&gt; and &lt;code&gt;Trigger production deployment&lt;/code&gt; in &lt;code&gt;deploy.yml&lt;/code&gt; deploy to different environments based on branch name.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Branch / Environment&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;DEVELOP_WEBHOOK&lt;/td&gt;
&lt;td&gt;develop/develop&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PROD_WEBHOOK&lt;/td&gt;
&lt;td&gt;main/production&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This pattern allows me to deploy code from my &lt;code&gt;develop&lt;/code&gt; branch to my preview environment and code from my &lt;code&gt;main&lt;/code&gt; branch to my production environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disclaimer
&lt;/h2&gt;

&lt;p&gt;Testing and deployment pipelines have many forms, each is specific to the needs of the project and &lt;strong&gt;should evolve over time&lt;/strong&gt;. This is just one way of doing it, find the process that works for you. &lt;/p&gt;

</description>
      <category>laravel</category>
      <category>ci</category>
      <category>cd</category>
      <category>github</category>
    </item>
  </channel>
</rss>
