diff --git a/Dockerfile b/Dockerfile index 51578e9..7aa63c9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Container image that runs your code -FROM alpine:3.16 +FROM alpine:latest RUN apk add aws-cli jq diff --git a/README.md b/README.md index f2ddbf3..14f269f 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Example: ``` - name: Wait for Amplify to finish remote build - uses: duckbytes/amplify-build-status@v1.1 + uses: duckbytes/amplify-build-status@v2 with: app-id: ${{ secrets.AMPLIFY_APP_ID }} branch-name: ${{ github.ref_name }} @@ -31,7 +31,7 @@ Click on *Edit backend* and copy the appId value. ### Inputs -`app-id`, `branch-name` and `commit-id` are all required input. You also must set `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_REGION` in your environnment variables. +`app-id`, `branch-name` and `commit-id` are all required input. You also must set `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_REGION` in your environment variables. Other inputs are: @@ -41,8 +41,20 @@ Other inputs are: ### Outputs - `status` # The build status output according to the AWS CLI. +- `backend_environment` # The environment name. +- `graphql_endpoint` # The GraphQL endpoint. + +You can access these values later in your workflow like this: + +`${{ steps.amplify_status.outputs.environment_name }}` ## Known issues +Sometimes the Amplify console doesn't automatically associate a backend with a new deployment. + +Amplify will successfully complete the new build, but it is necessary click `(Edit)` next to "Continuous deploys set up" and set it to the correct backend before the action can return the environment name and GraphQL endpoint. + +**only on V1** + If you are connecting a branch to Amplify for the first time, the commit-id may be `HEAD` instead of the commit sha. -Any subsequent bulds triggered by commits will use the actual commit sha. +Any subsequent builds triggered by commits will use the actual commit sha. diff --git a/action.yml b/action.yml index 3728faa..2e5afce 100644 --- a/action.yml +++ b/action.yml @@ -29,6 +29,10 @@ inputs: outputs: status: description: 'The result of the build.' + backend_environment: + description: 'The environment name' + graphql_endpoint: + description: 'The GraphQL endpoint.' runs: using: 'docker' image: 'Dockerfile' diff --git a/entrypoint.sh b/entrypoint.sh index c1e9d42..14ce73e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,5 +1,7 @@ #!/bin/sh -l +set -e + APP_ID=$1 BRANCH_NAME=$2 COMMIT_ID=$3 @@ -43,13 +45,65 @@ if [[ $TIMEOUT -lt 0 ]]; then exit 1 fi +get_backend_env_name () { + local env_name; + local env_arn; + local next_token=""; + local list_result; + # get backendEnvironmentArn from get branch first + env_arn=$(aws amplify get-branch --app-id "$APP_ID" --branch-name "$BRANCH_NAME" | jq -r ".branch.backendEnvironmentArn") + # search the list of backend environments for the environment name + while : ; do + list_result=$(aws amplify list-backend-environments --app-id "$APP_ID" --next-token "$next_token") + env_name=$(echo $list_result | jq -r ".backendEnvironments[] | select(.backendEnvironmentArn == \"$env_arn\") | .environmentName") + if [[ -n $env_name ]]; then + env_name=$(echo $env_name | tr -d " \t\n\r") + break + fi + next_token=$(echo $list_result | jq -r ".nextToken") + next_token=$(echo $next_token | tr -d " \t\n\r") + if [[ -z $next_token ]] || [[ $next_token == "null" ]]; then + break + fi + done + exit_status=$? + echo "$env_name" + return $exit_status +} + +get_backend_graphql_endpoint () { + local endpoint; + local env_name; + local test; + env_name=$(get_backend_env_name) + echo "Found env name getting graphql endpoint: $env_name" >&2 + endpoint=$(aws amplifybackend get-backend --app-id "$APP_ID" --backend-environment-name "$env_name" | jq -r ".AmplifyMetaConfig" | jq -r ".api.platelet.output.GraphQLAPIEndpointOutput") + exit_status=$? + endpoint=$(echo $endpoint | tr -d " \t\n\r") + echo "$endpoint" + return $exit_status +} + + +write_output () { + local env_name; + local graphql_endpoint; + echo "status=$STATUS" >> $GITHUB_OUTPUT + env_name=$(get_backend_env_name) + graphql_endpoint=$(get_backend_graphql_endpoint) + echo "Found environment name: $env_name" + echo "Found graphql endpoint: $graphql_endpoint" + echo "environment_name=$env_name" >> $GITHUB_OUTPUT + echo "graphql_endpoint=$graphql_endpoint" >> $GITHUB_OUTPUT +} + get_status () { local status; - status=$(aws amplify list-jobs --app-id "$1" --branch-name "$2" | jq -r ".jobSummaries[] | select(.commitId == \"$3\") | .status") + status=$(aws amplify list-jobs --app-id "$APP_ID" --branch-name "$BRANCH_NAME" | jq -r ".jobSummaries[] | select(.commitId == \"$COMMIT_ID\") | .status") exit_status=$? # it seems like sometimes status ends up with a new line in it? # strip it out - status=$(echo $status | tr '\n' ' ') + status=$(echo $status | tr -d " \t\n\r") echo "$status" return $exit_status } @@ -71,11 +125,11 @@ fi if [[ $STATUS == "SUCCEED" ]]; then echo "Build Succeeded!" - echo "status=$STATUS" >> $GITHUB_OUTPUT + echo $(write_output) exit 0 elif [[ $STATUS == "FAILED" ]]; then echo "Build Failed!" - echo "status=$STATUS" >> $GITHUB_OUTPUT + echo $(write_output) no_fail_check fi @@ -105,7 +159,7 @@ seconds=$(( $TIMEOUT * 60 )) count=0 if [[ "$WAIT" == "false" ]]; then - echo "status=$STATUS" >> $GITHUB_OUTPUT + echo $(write_output) exit 0 elif [[ "$WAIT" == "true" ]]; then while [[ $STATUS != "SUCCEED" ]]; do @@ -121,7 +175,7 @@ elif [[ "$WAIT" == "true" ]]; then fi if [[ $STATUS == "FAILED" ]]; then echo "Build Failed!" - echo "status=$STATUS" >> $GITHUB_OUTPUT + echo $(write_output) no_fail_check else echo "Build in progress... Status: $STATUS" @@ -129,5 +183,5 @@ elif [[ "$WAIT" == "true" ]]; then count=$(( $count + 30 )) done echo "Build Succeeded!" - echo "status=$STATUS" >> $GITHUB_OUTPUT + echo $(write_output) fi