Skip to main content

Deploying a static site to s3 with Jenkins

Back-end Development

As developers, we want to streamline and take as much human interaction out of the deployment process.  This is where Continuous Integration comes in.  At Rapid Development, we use Jenkins as our open source solution for handling all the details and process surrounding the deployment of a site.

I have recently written about hosting a static site on s3 and securing it with a certificate. The last part then is pushing code.

In Jenkins, ensure that the Jenkins Pipeline Deployment plugin is enabled.

Then, in the root of your project, create a Jenkinsfile.  Here is an example of what that jenkinsfile should look like.

pipeline {
  agent any
  stages {
    stage('Deploy to s3') {
      when {
        branch 'master'
      }
      steps {
        slackSend channel: "#<channel>", message: "Deployment Starting: ${env.JOB_NAME}, build number ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
        echo 'Deploying to RDG AWS s3 bucket.'
        withAWS(region:'<AWS Region: like us-east-1>', credentials:'RDG AWS') {
          s3Delete(bucket: '<Bucket Name>', path:'**/*')
          s3Upload(bucket: '<Bucket Name>', includePathPattern:'**/*')
        }
      }
    }
  }
  post {
    success {
      slackSend channel: "#<channel>", color: "good", message: "Deployment Complete: ${env.JOB_NAME}, build number ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
    }
    failure {
      slackSend channel: "#<channel>", color: "danger", message: "Deployment Failed: ${env.JOB_NAME}, build number ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
    }
    // always {
    //   slackSend ...
    // }
  }
}

 

You will notice that AWS credentials are referenced in this file.  This should be configured as you would with any credentials in Jenkins.

Now that you have that in place, go into the Blue Ocean interface within Jenkins (getting Jenkins up and running, and connected to your code repository is beyond the scope of this article).

Create a NEW PIPELINE and select the appropriate repository that contains your site and Jenkinsfile

It should detect the Jenkinsfile and use that for configuration.  Assuming what details you provided are correct, this should be all that is needed.