betahub
betahub

Reputation: 21

Github actions to deploy static site to AWS S3

I am trying to deploy static content to AWS S3 from Github actions. I created AWS id and secret environment variables

and have this as main.yml

name: S3CI
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Configure AWS Credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-west-2
    - name: Build static site
    - run: yarn install && npm run-script build
    - name: Deploy static site to S3 bucket
      run: aws s3 sync ./dist/ s3://awss3-blog --delete

But Github actions failed with error

Invalid Workflow File

DETAILS every step must define a uses or run key

Upvotes: 2

Views: 2201

Answers (3)

Caio Gomes
Caio Gomes

Reputation: 778

This is a full example. Just pay attentions to the variables that you have to set and remove the Cloudfront invalidation if you don't need it. This repo: https://github.com/caiocsgomes/caiogomes.me has it implemented, building a static website with Hugo and deploying to s3.

# Workflow name
name: S3 Deploy

on:
  workflow_dispatch:
  push:
    paths:
      - 'app/**'
      - '.github/workflows/deploy.yml'


jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    env:
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      AWS_DEFAULT_REGION: sa-east-1
      BUCKET_NAME: caiogomes.me
    steps:
        - name: Install hugo
          run: sudo apt install hugo

        - name: Install aws cli
          id: install-aws-cli
          uses: unfor19/install-aws-cli-action@v1
          with:
            version: 2
            verbose: false
            arch: amd64
            rootdir: ""
            workdir: "" 

        - name: Set AWS credentials
          run: export AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }} && export AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}

        - name: Checkout repository
          uses: actions/checkout@v3
          with:
            submodules: 'true'

        - name: Build
          run: cd app/ && hugo

        - name: Upload files to S3
          run: aws s3 sync app/public/ s3://${{ env.BUCKET_NAME }}/ --exact-timestamps --delete

  create-cloudfront-invalidation:
    needs: build-and-deploy
    runs-on: ubuntu-latest
    env:
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      AWS_DEFAULT_REGION: sa-east-1
      CLOUDFRONT_DISTRIBUTION_ID: ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }}
    steps:
      - name: Install aws cli
        id: install-aws-cli
        uses: unfor19/install-aws-cli-action@v1
        with:
          version: 2
          verbose: false
          arch: amd64
          rootdir: ""
          workdir: "" 

      - name: Set AWS credentials
        run: export AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }} && export AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}

      - name: Invalidate clodufront distribution
        run: aws cloudfront create-invalidation --distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} --paths "/*"

Upvotes: 0

GullerYA
GullerYA

Reputation: 1776

Usually, always actually from my own experience, GitHub is showing clearly the invalid part of the YAML. In my cases, it is almost always complain about the tabs instead of spaces, and yes, I'm very mad about it!!!

In your case, as @smac89 already mentioned, it is the line starting - run, which is wrongly NOT associated with the previous - name because of that dash, so the - name became orphan as well.

To the point of deploying to S3: I warmly suggest (as I already did somewhere else) to do it just with the CLI and without any additional action/plugin.

It is as simple as:

- name: Deploy static site to S3 bucket
  env:
    AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
    AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  run: aws s3 sync ./dist/ s3://awss3-blog --delete

As you can see, it is exactly the same effort from the secrets perspective, but simpler, independent, cleaner etc. BTW, region is not required and may safely be omitted.

Upvotes: 1

smac89
smac89

Reputation: 43206

It has to do with this line:

- run: yarn install && npm run-script build

But it is specifically complaining about this step:

- name: Build static site

Remove the - infront of the run if you want the above step to use that run command

Upvotes: 0

Related Questions