16

I want to control Amplify deployments from GitHub Actions because Amplify auto-build

  • doesn't provide a GitHub Environment
  • doesn't watch the CI for failures and will deploy anyways, or
  • requires me to duplicate the CI setup and re-run it in Amplify
  • didn't support running a cypress job out-of-the-box

4 Answers 4

16
  • Turn off auto-build (in the App settings / General / Branches).
  • Add the following script and job

scripts/amplify-deploy.sh

echo "Deploy app $1 branch $2"
JOB_ID=$(aws amplify start-job --app-id $1 --branch-name $2 --job-type RELEASE | jq -r '.jobSummary.jobId')
echo "Release started"
echo "Job ID is $JOB_ID"

while [[ "$(aws amplify get-job --app-id $1 --branch-name $2 --job-id $JOB_ID | jq -r '.job.summary.status')" =~ ^(PENDING|RUNNING)$ ]]; do sleep 1; done
JOB_STATUS="$(aws amplify get-job --app-id $1 --branch-name $2 --job-id $JOB_ID | jq -r '.job.summary.status')"
echo "Job finished"
echo "Job status is $JOB_STATUS"
  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: us-east-1
      AWS_DEFAULT_OUTPUT: json
    steps:
    - uses: actions/checkout@v2
    - name: Deploy
      run: ./scripts/amplify-deploy.sh xxxxxxxxxxxxx master

You could improve the script to fail if the release fails, add needed steps (e.g. lint, test), add a GitHub Environment, etc.

There's also amplify-cli-action but it didn't work for me.

Sign up to request clarification or add additional context in comments.

3 Comments

with this approach, did you get the actual build logs in Github Action? or you will still need to go to amplify console to see the details if anything fails?
@thisismydesign Is it possible if you can share the whole pipeline code here. I have the same questions to run cypress, deploy to aws amplify and i have multiple packages i am using lerna .
Doesn’t this just kick off a build and deploy in Amplify? Is there no way to deploy directly without using the Amplify pipeline?
6

Similar to answer 2 here, but I used tags instead.

Create an action like ci.yml, turn off auto-build on the staging & prod envs in amplify and create the webhook triggers.

name: CI-Staging
on:
  release:
    types: [prereleased]
permissions: read-all # This is required to read the secrets
jobs:
  deploy-staging:
    runs-on: ubuntu-latest
    permissions: read-all # This is required to read the secrets
    steps:
      - name: deploy
        run: |
          URL="${{ secrets.STAGING_DEPLOY_WEBHOOK }}"
          curl -X POST -d {} "$URL" -H "Content-Type: application/json"

name: CI-production
on:
  release:
    types: [released]
permissions: read-all # This is required to read the secrets
jobs:
  deploy-production:
    runs-on: ubuntu-latest
    permissions: read-all # This is required to read the secrets
    steps:
      - name: deploy
        run: |
          URL="${{ secrets.PRODUCTION_DEPLOY_WEBHOOK }}"
          curl -X POST -d {} "$URL" -H "Content-Type: application/json"

1 Comment

Would you mind expanding on this answer a little bit? How do the tags work? I want to set up a series of actions in github so that unit tests run, then ui tests run, and only if those both pass, fire off the build for the correct environment in amplify.
4
  1. Disable automatic builds:
  • Go to App settings > general in the AWS Amplify console and disable automatic builds there.
  1. Go to App settings > Build Settings and create a web hook which is a curl command that will trigger a build.
  • Example: curl -X POST -d {} URL -H "Content-Type: application/json"
  1. Save the URL in GitHub as a secret.
  2. Add the curl script to the GitHub actions YAML script like this:
deploy:
  runs-on: ubuntu-latest
  steps:
  - name: deploy
    run: |
        URL="${{ secrets.WEBHOOK_URL }}"
        curl -X POST -d {} "$URL" -H "Content-Type: application/json"

Comments

0
      - name: Deploy to Amplify (Headless)
        run: |
          cd frontend
          npm install -g @aws-amplify/cli
          amplify pull --appId d12v71fvv6s7j9 -y

          echo "🚀 Publishing app..."
          amplify publish -y

This works for me. I was stuck with this whole day to find solution

make sure you create amplify setup in your repo
to make that
amplify init
amplify add hosting
for appId after intializing the amplify you can see an app created in amplify dashboard you can find the app id there

make sure this configs have correct build and deploy command
.\amplify\.config\project-config.json

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.