Created
May 21, 2020 15:44
-
-
Save paustint/de27d44045c714c057ff9a2ac430b558 to your computer and use it in GitHub Desktop.
Deploy zip to Heroku with GitHub Action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM alpine | |
RUN apk add --no-cache curl jq |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
set -o pipefail | |
# Dependencies: | |
# curl, jq | |
DEPLOY_FILE=$1 | |
HEROKU_ENV_NAME=$2 | |
HEROKU_TOKEN=$3 | |
echo DEPLOY_FILE=$DEPLOY_FILE | |
echo HEROKU_ENV_NAME=$HEROKU_ENV_NAME | |
echo HEROKU_TOKEN=$HEROKU_TOKEN | |
EXAMPLE_COMMAND="./docker-deploy.sh $DEPLOY_FILE $HEROKU_ENV_NAME $HEROKU_TOKEN" | |
if [ -z "$DEPLOY_FILE" ] | |
then | |
echo "Input filename was not provided" | |
echo $EXAMPLE_COMMAND | |
exit 1 | |
fi | |
if [ -z "$HEROKU_ENV_NAME" ] | |
then | |
echo "Heroku app name was not provided" | |
echo $EXAMPLE_COMMAND | |
exit 1 | |
fi | |
if [ -z "$HEROKU_TOKEN" ] | |
then | |
echo "Heroku auth token was not provided" | |
echo $EXAMPLE_COMMAND | |
exit 1 | |
fi | |
echo "π DEPLOY_FILE: ${DEPLOY_FILE}" | |
echo "π HEROKU_ENV_NAME: ${HEROKU_ENV_NAME}" | |
echo | |
echo "--------STARTING DEPLOY----------" | |
echo "--------PREPARING UPLOAD----------" | |
echo | |
echo "Submiting source to Heroku" | |
# https://devcenter.heroku.com/articles/build-and-release-using-the-api#sources-endpoint | |
response1=$(curl -# -n -X POST https://api.heroku.com/apps/${HEROKU_ENV_NAME}/sources \ | |
-H 'Accept: application/vnd.heroku+json; version=3' \ | |
-H "Authorization: Bearer $HEROKU_TOKEN") | |
GET_URL=$(echo $response1 | jq --raw-output '.source_blob.get_url') | |
PUT_URL=$(echo $response1 | jq --raw-output '.source_blob.put_url') | |
echo | |
echo "GET_URL=${GET_URL}" | |
echo "PUT_URL=${PUT_URL}" | |
echo | |
echo "--------UPLOADING SOURCE----------" | |
echo | |
echo "π Uploading source to Heroku - ${DEPLOY_FILE}" | |
echo "PUT - ${PUT_URL}" | |
curl -# $PUT_URL -X PUT \ | |
-H 'Content-Type:' \ | |
--data-binary "@${DEPLOY_FILE}" | |
echo "Uploading finished" | |
echo | |
echo "--------KICKING OFF BUILD----------" | |
echo | |
echo "π Starting Build Heroku - ${DEPLOY_FILE}" | |
echo "POST - ${PUT_URL}" | |
response3=$(curl -# -n \ | |
-d '{"source_blob": {"url": "'$GET_URL'","version": "'$GITHUB_SHA'"}}' \ | |
-H 'Accept: application/vnd.heroku+json; version=3' \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer $HEROKU_TOKEN" \ | |
-X POST https://api.heroku.com/apps/${HEROKU_ENV_NAME}/builds) | |
BUILD_ID=$(echo $response3 | jq --raw-output '.id') | |
BUILD_OUTPUT_URL=$(echo $response3 | jq --raw-output '.output_stream_url') | |
echo | |
echo "π Deploy Started. Build id: ${BUILD_ID}" | |
echo "π Deployment progress URL: ${BUILD_OUTPUT_URL}" | |
echo | |
curl $BUILD_OUTPUT_URL | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to Heroku | |
on: | |
push: | |
# Sequence of patterns matched against refs/tags | |
tags: | |
- release-v* # Push events to release-vx.xx tag | |
env: | |
HEROKU_ENV_NAME: my-heroku-app | |
OUTPUT_FILENAME: my-heroku-app.tgz | |
defaults: | |
run: | |
shell: bash | |
jobs: | |
build: | |
name: Build application | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
# https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows | |
- name: Cache node modules | |
uses: actions/cache@v1 | |
env: | |
cache-name: cache-node-modules | |
with: | |
# npm cache files are stored in `~/.npm` on Linux/macOS | |
path: ~/.npm | |
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-build-${{ env.cache-name }}- | |
${{ runner.os }}-build- | |
${{ runner.os }}- | |
- name: Install Application | |
run: | | |
npm ci --ignore-scripts | |
npm run ngcc | |
# Add any additional commands required here | |
deploy: | |
name: Deploy to Heroku | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: Compress build into zip | |
uses: jjkaram/[email protected] | |
with: | |
args: | | |
tar \ | |
--exclude="docker-deploy*" \ | |
--exclude="docker-build*" \ | |
--exclude="*.code-workspace" \ | |
--exclude="*.sfdx*" \ | |
--exclude="*.DS_Store" \ | |
--exclude="**/*.DS_Store" \ | |
--exclude=".circleci*" \ | |
--exclude="**/.cache*" \ | |
--exclude=".vscode*" \ | |
--exclude=".idea*" \ | |
--exclude="**/*.vscode*" \ | |
--exclude="*.env" \ | |
--exclude="**/*.env" \ | |
--exclude=".git*" \ | |
--exclude="node_modules*" \ | |
-zcf ${{ env.OUTPUT_FILENAME}} . | |
- name: Deploy to Heroku | |
uses: ./.github/actions/deploy-action | |
with: | |
entrypoint: ./docker-deploy.sh | |
args: ${{ env.OUTPUT_FILENAME }} ${{ env.HEROKU_ENV_NAME }} ${{ secrets.HEROKU_TOKEN }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment