dev

It’s nice to host your own blog in AWS using S3 and CloudFront but deployment can quickly become a hassle. I intend to set up a fully automated pipeline but for the time being I just developed the following script to deploy the site.

The script does the following:

  • Build Jekyll site
  • Delete the contents of the target bucket
  • Upload the site contents to the target bucket
  • Invalidate CloudFormation

Also note that it assumes:

  • The bucket is set up yo enable static wrbsite hosting
  • There is a CloudFront distribtuion setup to use this bucket as source
  • There is a DNS entry pointing to the CloudFront distribtuion.

Assuming those have already been created and set up, the deployment function works like below:

function Deploy-Blog 
{
    param
    (
      [string] $s3BucketName,
      [string] $cloudFrontDistributionId,
      [bool] $runBuild = $true
    )
    
    if ($runBuild)
    {
        Write-Host "Building blog"
        bundle exec jekyll build    
    }
    
    Write-Host "Clearing bucket"
    aws s3 rm s3://$s3BucketName --recursive
    
    Write-Host "Uploading site to staging bucket"
    aws s3 cp ./_site s3://$s3BucketName --acl public-read --recursive
    
    Write-Host "Invalidate CloudFront distribution"
    aws cloudfront create-invalidation --distribution-id $cloudFrontDistributionId --paths "/*"    
}

and the consuming function looks like this:

To deploy to staging and confirm the changes work fine, run:

. ".\deploy.ps1" 

$STAGING_BUCKET_NAME = "staging.my.domain.com"
$STAGING_DISTRIBUTION_ID = "STAGING_DIST_ID"

Deploy-Blog $STAGING_BUCKET_NAME $STAGING_DISTRIBUTION_ID

This script build the Jekyll site and deploys to staging. Then to promote to production, run:

. ".\deploy.ps1" 

$PROD_BUCKET_NAME = "my.domain.com"
$PROD_DISTRIBUTION_ID = "PROD_DIST_ID"

Deploy-Blog $PROD_BUCKET_NAME $PROD_DISTRIBUTION_ID $false

Note the build parameter is set to false as we don’t need to build the site again. We just want to deploy the previous output we deployed to staging stragith to production.

dev

I hate when .DS_Store files get checked in and create conflicts when working on different Mac computers.

In the StackOverflow referenced below I found the answer. In order to do it in all my repositories I wrote this little PowerShell script:

Get-ChildItem -Directory | ForEach-Object { 
    Write-Host "Change directory to repo"
    Set-Location $_.FullName
    Get-Location | Write-Host

    find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
    echo .DS_Store >> .gitignore

    git add .gitignore
    git commit -m '.DS_Store banished!'
    git push
}

Resources

dev

Powershell comes with lots of great cmdlets. In this post I’d like to show an easy way to convert CSV to JSON and vice versa.

CSV to JSON

Import-Csv /path/to/csv | ConvertTo-Json | Out-File /path/to/json

JSON to CSV

(Get-Content -Path /path/to/json -Raw | ConvertFrom-Json) | Export-CSV /path/to/csv -NoTypeInformation