Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pgibson1-godaddy committed Jul 3, 2023
0 parents commit c8a9523
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/build-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
name: Build and publish image to ghcr.io
on:
release:
types:
- published
workflow_dispatch:
jobs:
publish-pagely-deploy-image:
runs-on: ubuntu-latest
steps:
- name: Generate Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/godaddy-wordpress/pagely-deploy
flavor: |
latest=false
tags: |
type=semver,pattern={{raw}}
type=sha
type=raw,value=latest,enable=${{ !github.event.release.draft && !github.event.release.prerelease }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build and push
uses: docker/build-push-action@v4
with:
push: true
context: "{{defaultContext}}:image"
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
37 changes: 37 additions & 0 deletions .github/workflows/tag-major-version-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
name: Tag major version on release
on:
release:
types:
- published

jobs:
tag-release:
runs-on: ubuntu-latest
steps:
# Checkout current repo
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Tag release with vX tag
shell: bash
run: |
RELEASE_NAME="${GITHUB_REF#refs/tags/}"
echo "Release name: ${RELEASE_NAME}"
if [[ "${RELEASE_NAME}" =~ ^(v[0-9]+)[.] ]]; then
RELEASE_TAG_SHORT="${BASH_REMATCH[1]}"
echo "Release tag short: ${RELEASE_TAG_SHORT}"
# Git config
git config --local user.name github-actions
git config --local user.email [email protected]
# Create tag locally
git tag -f -a "${RELEASE_TAG_SHORT}" -m "Automated GitHub Actions release: ${RELEASE_TAG_SHORT}"
# Delete remote tag
git push origin :refs/tags/"${RELEASE_TAG_SHORT}"
# Push tag to remote
git push origin "${RELEASE_TAG_SHORT}"
else
echo "Release name does not match vX pattern: ${RELEASE_NAME}"
echo "Nothing to do!"
fi
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 GoDaddy WordPress

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Pagely Deploy Action
GitHub action for deploying to [Pagely](https://pagely.com/) Apps

## Inputs

| Name | Requirement | Description |
|-----------------------------|-------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `PAGELY_INTEGRATION_SECRET` | _required_ | Authentication token. Create a CI Integration at [Atomic](https://atomic.pagely.com/account/integrations) to get this. |
| `PAGELY_INTEGRATION_ID` | _required_ | Unique ID for the integration found in Atomic. |
| `PAGELY_APP_ID` | _required_ | Numeric ID of the app you want to deploy to, available in Atomic. |
| `PAGELY_DEPLOY_DEST` | _required_ | Set the subdirectory to deploy to within the app. `/httpdocs` would be the web root of your app. Examples: `/httpdocs`, `/httpdocs/wp-content/plugins/my-plugin` |
| `PAGELY_WORKING_DIR` | _optional_ | The directory that you want deployed relative to the repository root. If you want to deploy your entire repository contents, then you can leave this blank to use the default (`${{ github.workspace }}`), otherwise you should specify the directory prefixed with `${{ github.workspace }}`. For example, if you have built artifacts you wish to deploy inside a directory called `build`, you can specify `${{ github.workspace }}/build`. |

## Example usage

The GitHub workflow below shows an example that pushes the contents of the repository to the `/httpdocs/wp-content/plugins/my-plugin` directory of the Pagely app.

The workflow is triggered on every push to the `main` branch of the repository.

```yaml
---
on:
push:
branches:
- main
jobs:
deploy:
name: Deploy to Pagely App
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Run deploy
uses: godaddy-wordpress/pagely-deploy-action@v1
with:
PAGELY_DEPLOY_DEST: "/httpdocs/wp-content/plugins/my-plugin"
PAGELY_INTEGRATION_SECRET: ${{ secrets.PAGELY_INTEGRATION_SECRET }}
PAGELY_INTEGRATION_ID: ${{ secrets.PAGELY_INTEGRATION_ID }}
PAGELY_APP_ID: ${{ secrets.PAGELY_APP_ID }}
PAGELY_WORKING_DIR: "${{ github.workspace }}" # use the files starting at the repository root
```
23 changes: 23 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
name: Pagely Deploy Action
description: Deploys code or build artifacts to a Pagely app from GitHub Actions.
inputs:
PAGELY_DEPLOY_DEST:
description: Sets the destination subdirectory. You will usually want to prefix this with /httpdocs
required: true
PAGELY_INTEGRATION_ID:
description: The unique ID for the CICD integration for this app from Atomic.
required: true
PAGELY_INTEGRATION_SECRET:
description: The CICD integration secret for the app from Atomic.
required: true
PAGELY_APP_ID:
description: The numeric ID of the app to deploy to and for which the CICD integration is configured.
required: true
PAGELY_WORKING_DIR:
description: The directory from the repo you wish to deploy. Defaults to the root of the repo.
default: "${{github.workspace}}"
required: false
runs:
using: 'docker'
image: 'docker://ghcr.io/godaddy-wordpress/pagely-deploy:latest'
1 change: 1 addition & 0 deletions image/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dockerfile
7 changes: 7 additions & 0 deletions image/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM alpine:3.18

RUN apk add --no-cache tar curl ca-certificates bash httpie

COPY pipe.sh /

ENTRYPOINT ["/pipe.sh"]
120 changes: 120 additions & 0 deletions image/pipe.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env bash
#
# Deploy to a Pagely app with a CICD integration - https://pagely.com
# Required globals:
# PAGELY_DEPLOY_DEST
# PAGELY_INTEGRATION_SECRET
# PAGELY_INTEGRATION_ID
# PAGELY_APP_ID
#
# Optional globals:
# PAGELY_WORKING_DIR (default: "$PWD" - the default will be the GitHub workspace)

set -o errexit
set -o pipefail
set -o nounset

PAGELY_DEPLOY_DEST="${INPUT_PAGELY_DEPLOY_DEST:-${PAGELY_DEPLOY_DEST:-}}"
PAGELY_INTEGRATION_SECRET="${INPUT_PAGELY_INTEGRATION_SECRET:-${PAGELY_INTEGRATION_SECRET:-}}"
PAGELY_INTEGRATION_ID="${INPUT_PAGELY_INTEGRATION_ID:-${PAGELY_INTEGRATION_ID:-}}"
PAGELY_APP_ID="${INPUT_PAGELY_APP_ID:-${PAGELY_APP_ID:-}}"
PAGELY_WORKING_DIR="${INPUT_PAGELY_WORKING_DIR:-${PAGELY_WORKING_DIR:-"$(pwd)"}}"

if [[ -z "${PAGELY_DEPLOY_DEST}" ]]; then
echo "PAGELY_DEPLOY_DEST is required"
exit 1
fi
if [[ -z "${PAGELY_INTEGRATION_SECRET}" ]]; then
echo "PAGELY_INTEGRATION_SECRET is required"
exit 1
fi
if [[ -z "${PAGELY_APP_ID}" ]]; then
echo "PAGELY_APP_ID is required"
exit 1
fi
if [[ -z "${PAGELY_WORKING_DIR}" ]]; then
echo "PAGELY_WORKING_DIR is required"
exit 1
fi


echo "Running deploy with the following settings: "
echo "PAGELY_DEPLOY_DEST: ${PAGELY_DEPLOY_DEST}"
echo "PAGELY_INTEGRATION_ID: ${PAGELY_INTEGRATION_ID}"
echo "PAGELY_APP_ID: ${PAGELY_APP_ID}"
echo "PAGELY_WORKING_DIR: ${PAGELY_WORKING_DIR}"


outputGroupStart ()
{
echo "::group::$1"
}

outputGroupEnd ()
{
echo "::endgroup::"
}

# Tar everything in the directory as requested
outputGroupStart "Tarball from the contents of the working dir"
tar --exclude-vcs -zcvf /tmp/deploy.tar.gz -C "${PAGELY_WORKING_DIR}" .
ls -lh /tmp/deploy.tar.gz
outputGroupEnd

URL_LOOKUP_OUTPUT="$(mktemp)"
URL_LOOKUP_URL="https://mgmt.pagely.com/api/apps/integration/${PAGELY_INTEGRATION_ID}/endpoint?appId=${PAGELY_APP_ID}"

echo "Lookup app's deployment URL"
if http --check-status --ignore-stdin --timeout=10 GET "${URL_LOOKUP_URL}" \
"X-Token: ${PAGELY_INTEGRATION_SECRET}" \
> "${URL_LOOKUP_OUTPUT}"
then
DEPLOY_URL="$(cat "${URL_LOOKUP_OUTPUT}")&tail=1"
echo "Successfully got deployment URL"
else
HTTP_EXIT_CODE=$?

echo "FAILURE in request to ${URL_LOOKUP_URL}"
case ${HTTP_EXIT_CODE} in
2) echo 'Request timed out!' ;;
3) echo 'Unexpected HTTP 3xx Redirection!' ;;
4) echo 'HTTP 4xx Client Error!';;
5) echo 'HTTP 5xx Server Error!' ;;
6) echo 'Exceeded --max-redirects=<n> redirects!' ;;
*) echo 'Other Error!' ;;
esac
cat "${URL_LOOKUP_OUTPUT}"
exit 1
fi

echo "Running deploy..."
DEPLOY_OUTPUT="$(mktemp)"
DEPLOY_HTTP_CODE_FILE="$(mktemp)"

# Using curl for streaming here as httpie seems to have issues actually live streaming this data back even though it has line breaks
set -o pipefail
curl \
--fail-with-body \
--show-error \
--silent \
--no-buffer \
--header "X-Token: ${PAGELY_INTEGRATION_SECRET}" \
--write-out "%{stderr}%{http_code}" \
--request POST \
--form "dest=${PAGELY_DEPLOY_DEST}" \
--form 'file=@/tmp/deploy.tar.gz' \
"${DEPLOY_URL}" 2>"${DEPLOY_HTTP_CODE_FILE}" | tee "${DEPLOY_OUTPUT}"
DEPLOY_CURL_EXIT_CODE=$?
DEPLOY_HTTP_RESP_CODE="$(cat "${DEPLOY_HTTP_CODE_FILE}")"

# Failure conditions
if [[ ${DEPLOY_CURL_EXIT_CODE} -ne 0 ]]; then
echo "FAILURE in deploy request to: ${DEPLOY_URL}"
if [[ -n "${DEPLOY_HTTP_RESP_CODE}" ]]; then
echo "HTTP response code was: ${DEPLOY_HTTP_RESP_CODE}"
fi
exit 1
fi
if [[ "$(tail -n 1 "${DEPLOY_OUTPUT}")" == "FAILURE" ]]; then
exit 1
fi

0 comments on commit c8a9523

Please sign in to comment.