From 4f10765fee7bf6cf8108cfb451afef53befeace9 Mon Sep 17 00:00:00 2001 From: daltonfury42 Date: Sun, 13 Sep 2020 22:10:04 +0530 Subject: [PATCH 1/2] Add auto commenting to PR with the preview url. Also, removed 10 character branch name limit Update README.md Add comment curl Pass comment url to the action Gate for toen Update README.md Update action.yml Update README.md Update entrypoint.sh Update action.yml Update entrypoint.sh Update README.md Update Dockerfile Update entrypoint.sh Update README.md Update entrypoint.sh --- Dockerfile | 3 ++- README.md | 8 +++++--- action.yml | 6 ++++++ entrypoint.sh | 29 ++++++++++++++++++++--------- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8c31ea97..2270de27 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,7 @@ LABEL maintainer="Yinlin Chen " ENV AWSCLI_VERSION='1.18.14' RUN pip install --quiet --no-cache-dir awscli==${AWSCLI_VERSION} +RUN apk --no-cache add curl ADD entrypoint.sh /entrypoint.sh -ENTRYPOINT ["/entrypoint.sh"] \ No newline at end of file +ENTRYPOINT ["/entrypoint.sh"] diff --git a/README.md b/README.md index 26aee28a..733f532e 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ jobs: id: setenvname run: | # use GITHUB_HEAD_REF that is set to PR source branch - echo "##[set-output name=setbranchname;]$(echo ${GITHUB_HEAD_REF} | cut -c-10)" + echo "##[set-output name=setbranchname;]$(echo ${GITHUB_HEAD_REF})" - name: deploy PR preview uses: yinlinchen/amplify-preview-actions@master @@ -42,6 +42,7 @@ jobs: env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} AmplifyAppId: ${{ secrets.AmplifyAppId }} BackendEnvARN: ${{ secrets.BackendEnvARN }} AWS_REGION: 'us-east-1' @@ -49,7 +50,7 @@ jobs: ### Configuration -The following settings must be passed as environment variables as shown in the example. Sensitive information, especially `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`, should be [set as encrypted secrets](https://help.github.com/en/articles/virtual-environments-for-github-actions#creating-and-using-secrets-encrypted-variables) — otherwise, they'll be public to anyone browsing your repository's source code and CI logs. +The following settings must be passed as environment variables as shown in the example. Sensitive information, especially `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`, should be [set as encrypted secrets](https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets) — otherwise, they'll be public to anyone browsing your repository's source code and CI logs. | Key | Value | Suggested Type | Required | Default | | ------------- | ------------- | ------------- | ------------- | ------------- | @@ -58,6 +59,7 @@ The following settings must be passed as environment variables as shown in the e | `AWS_REGION` | The region where you created your bucket. Set to `us-east-1` by default. [Full list of regions here.](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-available-regions) | `env` | **Yes** | `us-east-1` | | `AmplifyAppId` | The unique ID for an Amplify app. For example, `d6a88fjhifqlks` | `secret env` | **Yes** | N/A | | `BackendEnvARN` | The Amazon Resource Name (ARN) for a backend environment that is part of an Amplify app. | `secret env` | **Yes** | N/A | +| `GITHUB_TOKEN` | The GITHUB_TOKEN, required to post the comment with the preview URL | `github env` | **Yes** | N/A | | `NewBackendEnvARN` | The Amazon Resource Name (ARN) for a backend environment that is part of an Amplify app. | `secret env` | No | N/A | ## Inputs @@ -99,4 +101,4 @@ or ## License -This project is distributed under the [MIT license](LICENSE.md). \ No newline at end of file +This project is distributed under the [MIT license](LICENSE.md). diff --git a/action.yml b/action.yml index 386f4f89..98da09a7 100644 --- a/action.yml +++ b/action.yml @@ -7,6 +7,10 @@ inputs: amplify_command: description: 'amplify command: SUPPORTED: [deploy, delete]' required: true + comments_url: + description: 'Github comments url' + default: "${{ github.event.pull_request.comments_url }}" + required: false runs: using: 'docker' @@ -14,6 +18,8 @@ runs: args: - ${{ inputs.branch_name }} - ${{ inputs.amplify_command }} + - ${{ inputs.comments_url }} + branding: icon: 'git-pull-request' color: 'blue' diff --git a/entrypoint.sh b/entrypoint.sh index 71e2d8d8..03e48e62 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,7 +2,11 @@ set -e -if [ -z "$AWS_ACCESS_KEY_ID" ] && [ -z "$AWS_SECRET_ACCESS_KEY" ] ; then +BRANCH_NAME=$1 +AMPLIFY_COMMAND=$2 +COMMENT_URL=$3 + +if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ] ; then echo "You must provide the action with both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables in order to deploy" exit 1 fi @@ -21,12 +25,12 @@ if [ -z "$BackendEnvARN" ] ; then exit 1 fi -if [ -z "$1" ] ; then +if [ -z "$BRANCH_NAME" ] ; then echo "You must provide branch name input parameter in order to deploy" exit 1 fi -if [ -z "$2" ] ; then +if [ -z "$AMPLIFY_COMMAND" ] ; then echo "You must provide amplify_command input parameter in order to deploy" exit 1 fi @@ -38,23 +42,23 @@ ${AWS_REGION} text EOF -case $2 in +case $AMPLIFY_COMMAND in deploy) - sh -c "aws amplify create-branch --app-id=${AmplifyAppId} --branch-name=$1 \ + sh -c "aws amplify create-branch --app-id=${AmplifyAppId} --branch-name=$BRANCH_NAME \ --backend-environment-arn=${BackendEnvARN} --region=${AWS_REGION}" sleep 10 - sh -c "aws amplify start-job --app-id=${AmplifyAppId} --branch-name=$1 --job-type=RELEASE --region=${AWS_REGION}" + sh -c "aws amplify start-job --app-id=${AmplifyAppId} --branch-name=$BRANCH_NAME --job-type=RELEASE --region=${AWS_REGION}" ;; delete) - sh -c "aws amplify delete-branch --app-id=${AmplifyAppId} --branch-name=$1 --region=${AWS_REGION}" + sh -c "aws amplify delete-branch --app-id=${AmplifyAppId} --branch-name=$BRANCH_NAME --region=${AWS_REGION}" ;; *) - echo "amplify command $2 is invalid or not supported" + echo "amplify command $AMPLIFY_COMMAND is invalid or not supported" exit 1 ;; @@ -65,4 +69,11 @@ null null null text -EOF \ No newline at end of file +EOF + +if [ -z "$GITHUB_TOKEN" ] ; then + echo "Skipping comment as GITHUB_TOKEN not provided" +else + SUBDOMAIN_NAME=$(echo $BRANCH_NAME | sed 's/[^a-zA-Z0-9-]/-/') + curl -X POST $COMMENT_URL -H "Content-Type: application/json" -H "Authorization: token $GITHUB_TOKEN" --data '{ "body": "'"Preview branch generated at https://$SUBDOMAIN_NAME.${AmplifyAppId}.amplifyapp.com"'" }' +fi From 45b4f5605a8b891fb8569a0a70c975d196426a69 Mon Sep 17 00:00:00 2001 From: daltonfury42 Date: Mon, 14 Sep 2020 19:20:11 +0530 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 733f532e..2a06fd7c 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ The following settings must be passed as environment variables as shown in the e | `AWS_REGION` | The region where you created your bucket. Set to `us-east-1` by default. [Full list of regions here.](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-available-regions) | `env` | **Yes** | `us-east-1` | | `AmplifyAppId` | The unique ID for an Amplify app. For example, `d6a88fjhifqlks` | `secret env` | **Yes** | N/A | | `BackendEnvARN` | The Amazon Resource Name (ARN) for a backend environment that is part of an Amplify app. | `secret env` | **Yes** | N/A | -| `GITHUB_TOKEN` | The GITHUB_TOKEN, required to post the comment with the preview URL | `github env` | **Yes** | N/A | +| `GITHUB_TOKEN` | The GITHUB_TOKEN, should be supplied if a comment with the preview URL is to be posted on the PR | `github env` | No | N/A | | `NewBackendEnvARN` | The Amazon Resource Name (ARN) for a backend environment that is part of an Amplify app. | `secret env` | No | N/A | ## Inputs