diff --git a/action.yml b/action.yml index 9fce1a7..3f4772e 100644 --- a/action.yml +++ b/action.yml @@ -9,6 +9,7 @@ branding: color: yellow inputs: + # Main parameters preview-branch: description: Branch on which the previews will be deployed. required: false @@ -18,63 +19,93 @@ inputs: required: false default: pr-preview source-dir: - description: Directory containing files to deploy. + description: > + Directory containing files to deploy. + + Required when deploying a preview; will raise a warning if specified + when removing a preview. required: false - default: . + default: "" action: description: > Determines what this action will do when it is executed. Supported - values: `deploy`, `remove`, `auto` (default). + values: `deploy`, `remove`. If set to `deploy`, will attempt to deploy the preview and overwrite any existing preview in that location. If set to `remove`, will attempt to remove the preview in that location. + required: true - If set to `auto`, the action will try to determine whether to deploy - or remove the preview. It will deploy the preview on - `pull_request.types.synchronize` and `.opened` events, and remove it - on `pull_request.types.closed` events. It will not do anything for - all other events. `auto` is the default value. + # Parameters for simulating a pull request + pr-number: + description: > + The number of the PR to comment on when updating the preview + deployment. If the initiating event is `pull_request` or + `pull_request_target`, defaults to the number of the initiating PR. + Required otherwise. required: false - default: auto + default: ${{ github.event.number }} runs: using: composite steps: - - name: Store environment variables + - name: Store environment variables and validate inputs env: action: ${{ inputs.action }} umbrella: ${{ inputs.umbrella-dir }} - pr: ${{ github.event.number }} + source_dir: ${{ inputs.source-dir }} + pr: ${{ inputs.pr-number }} actionref: ${{ github.action_ref }} actionrepo: ${{ github.action_repository }} run: | - echo "action=$action" >> $GITHUB_ENV - echo "targetdir=$umbrella/pr-$pr" >> $GITHUB_ENV - echo "pr=$pr" >> $GITHUB_ENV + if [ -z "$action" ]; then + echo "::error::Action input is required (deploy/remove)" >&2 + exit 1 + fi - pagesurl=$(echo $GITHUB_REPOSITORY | sed 's/\//.github.io\//') - echo "pagesurl=$pagesurl" >> $GITHUB_ENV + if [ "$action" != "deploy" ] && [ "$action" != "remove" ]; then + echo "::error::Unknown action $action" >&2 + exit 1 + fi + if [ "$action" = "deploy" ] && [ -z "$source_dir" ]; then + echo "::error::Source dir must be defined when deploying" >&2 + exit 1 + fi - echo "emptydir=$(mktemp -d)" >> $GITHUB_ENV - echo "datetime=$(date '+%Y-%m-%d %H:%M %Z')" >> $GITHUB_ENV + if [ "$action" = "remove" ] && [ -n "$source_dir" ]; then + echo "::warning::Source dir should not be defined when removing" >&2 + fi - echo "actionref=$actionref" >> $GITHUB_ENV - echo "actionrepo=$actionrepo" >> $GITHUB_ENV - shell: bash + if [ -z "$pr" ]; then + echo "::error::PR number required for non-pull_request event" >&2 + exit 1 + fi - - name: Determine action version - run: >- - ${{ github.action_path }}/lib/find-current-git-tag.sh - -p $actionrepo -f $actionref + echo "action=$action" >> "$GITHUB_ENV" + + echo "targetdir=$umbrella/pr-$pr" >> "$GITHUB_ENV" + echo "pr=$pr" >> "$GITHUB_ENV" + + pagesurl=$(echo $GITHUB_REPOSITORY | sed 's/\//.github.io\//') + echo "pagesurl=$pagesurl" >> "$GITHUB_ENV" + + echo "emptydir=$(mktemp -d)" >> "$GITHUB_ENV" + echo "datetime=$(date '+%Y-%m-%d %H:%M %Z')" >> "$GITHUB_ENV" + + echo "actionref=$actionref" >> "$GITHUB_ENV" + echo "actionrepo=$actionrepo" >> "$GITHUB_ENV" shell: bash - - name: Determine auto action - if: env.action == 'auto' - run: ${{ github.action_path }}/lib/determine-auto-action.sh + - name: Determine action version + run: | + version=$( + "${{ github.action_path }}/lib/find-current-git-tag.sh" \ + -p $actionrepo -f $actionref + ) + echo "action_version=$version" >> "$GITHUB_ENV" shell: bash - name: Deploy preview directory @@ -92,6 +123,7 @@ runs: uses: marocchino/sticky-pull-request-comment@v2 with: header: pr-preview + number: ${{ env.pr }} message: "\ [PR Preview Action]\ (${{ github.server_url }}/${{ env.actionrepo }}) @@ -99,7 +131,7 @@ runs: :---: - :rocket: Deployed preview to + 🛫 Deployed preview to https://${{ env.pagesurl }}/${{ env.targetdir }}/ on branch [`${{ inputs.preview-branch }}`](\ @@ -123,6 +155,7 @@ runs: uses: marocchino/sticky-pull-request-comment@v2 with: header: pr-preview + number: ${{ env.pr }} message: "\ [PR Preview Action]\ (${{ github.server_url }}/${{ env.actionrepo }}) @@ -130,7 +163,7 @@ runs: :---: - Preview removed because the pull request was closed. + 🛬 Preview removed because the pull request was closed. ${{ env.datetime }} " diff --git a/lib/determine-auto-action.sh b/lib/determine-auto-action.sh deleted file mode 100755 index 4e62062..0000000 --- a/lib/determine-auto-action.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env bash - -case $GITHUB_EVENT_NAME in - "pull_request" | "pull_request_target") - echo "event_type is $GITHUB_EVENT_NAME; proceeding" - ;; - *) - echo "unknown event $GITHUB_EVENT_NAME; no action to take" - echo "action=none" >> "$GITHUB_ENV" - exit 0 - ;; -esac - -event_type=$(jq -r ".action" "$GITHUB_EVENT_PATH") -echo "event_type is $event_type" - -case $event_type in - "opened" | "reopened" | "synchronize") - echo "action=deploy" >> "$GITHUB_ENV" - ;; - "closed") - echo "action=remove" >> "$GITHUB_ENV" - ;; - *) - echo "unknown event type $event_type; no action to take" - echo "action=none" >> "$GITHUB_ENV" - ;; -esac diff --git a/lib/find-current-git-tag.sh b/lib/find-current-git-tag.sh index c92ded9..dab9a6e 100755 --- a/lib/find-current-git-tag.sh +++ b/lib/find-current-git-tag.sh @@ -6,7 +6,7 @@ helpFunction() { echo -e "\t-p GitHub repository to clone, format: owner/repo" echo -e "\t-f Reference of repository to clone" exit 1 -} +} >&2 while getopts "p:f:" opt; do case "$opt" in @@ -17,11 +17,11 @@ while getopts "p:f:" opt; do done if [ -z "$github_repository" ] || [ -z "$git_ref" ]; then - echo "some parameters are empty" + echo "some parameters are empty" >&2 helpFunction fi -echo "Cloning repository $github_repository at ref $git_ref" +echo "Cloning repository $github_repository at ref $git_ref" >&2 git clone --bare --single-branch --branch "$git_ref" "https://github.com/$github_repository" bare_pr_preview cd bare_pr_preview || exit 1 @@ -30,4 +30,5 @@ action_version=$(git describe --tags --match "v*.*.*" \ || git describe --tags \ || git rev-parse HEAD) -echo "action_version=$action_version" >> "$GITHUB_ENV" +echo "$action_version" +exit 0