forked from CleverCloud/clever-cloud-review-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: transform script into a composite action
- Loading branch information
Showing
5 changed files
with
191 additions
and
184 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
on: | ||
pull_request_target: | ||
types: [opened, closed, synchronize, reopened] | ||
branches: [ main ] | ||
|
||
jobs: | ||
deploy: | ||
name: Deploy/redeploy review app | ||
runs-on: ubuntu-latest | ||
permissions: | ||
issues: write | ||
pull-requests: write | ||
contents: read | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
repository: ${{ github.event.pull_request.head.repo.full_name }} | ||
ref: ${{ github.event.pull_request.head.ref }} | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
fetch-depth: 0 | ||
- name: Create review app | ||
uses: juliamrch/gh-action-composite@v20 | ||
env: | ||
CLEVER_SECRET: ${{ secrets.CLEVER_SECRET }} | ||
CLEVER_TOKEN: ${{ secrets.CLEVER_TOKEN }} | ||
ORGA_ID: ${{ secrets.ORGA_ID }} | ||
GH_CC_RUN_SUCCEEDED_HOOK: ${{ secrets.CC_RUN_SUCCEEDED_HOOK }} | ||
with: | ||
type: 'static-apache' | ||
set-env: true | ||
environment: 'review' | ||
- name: Comment PR | ||
uses: actions/github-script@v7 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
script: | | ||
const issue_number = context.payload.pull_request.number; | ||
const message = `Deployment has finished 👁️👄👁️ Your app is available [here](https://${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }}.cleverapps.io)`; | ||
github.rest.issues.createComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: issue_number, | ||
body: message | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,145 +1,69 @@ | ||
name: Clever Cloud Review App on Pull Requests | ||
name: 'Clever Cloud review app on PRs' | ||
description: 'Deploy a review app on Clever Cloud when a PR is opened' | ||
|
||
description: Deploy, sync and delete review apps on Clever Cloud for every pull request targeting the `main` branch | ||
branding: | ||
icon: upload-cloud | ||
colore: red | ||
|
||
on: | ||
pull_request_target: | ||
types: [opened, closed, synchronize, reopened] | ||
branches: [ main ] | ||
inputs: | ||
type: | ||
description: 'Which type of app to create' | ||
required: true | ||
name: | ||
description: 'The name of your app' | ||
required: true | ||
default: ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }} | ||
alias: | ||
description: 'The alias of your app' | ||
required: true | ||
default: ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }} | ||
region: | ||
description: 'The region to deploy on' | ||
required: true | ||
default: 'par' | ||
organization: | ||
description: 'The organization to deploy on' | ||
required: true | ||
default: $ORGA_ID | ||
domain: | ||
description: 'The domain to use for the app' | ||
required: false | ||
default: ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }}.cleverapps.io | ||
set-env: | ||
description: 'Set environment variables' | ||
type: boolean | ||
required: false | ||
default: false | ||
environment: | ||
description: 'Environment to run tests against' | ||
type: environment | ||
required: true | ||
default: '' | ||
runs: | ||
using: "composite" | ||
steps: | ||
- name: Install clever-tools | ||
shell: bash | ||
run: npm install -g clever-tools | ||
- name: Execute commands based on action | ||
run: | | ||
if [ "${{ github.event.action }}" = 'opened' ] || [ "${{ github.event.action }}" = 'reopened' ]; then | ||
clever create --type ${{ inputs.type }} ${{ inputs.name }} --alias ${{ inputs.alias }} --region ${{ inputs.region }} --org ${{ inputs.organization }} | ||
clever domain add ${{ inputs.domain }} | ||
# Only select environment variables with GH_ prefix | ||
# to exclude GitHub runner variables | ||
if ${{ inputs.set-env }}; then | ||
# Remove prefix from print | ||
for var in $(env | awk -F= '/^GH_/ { print $1 }') | ||
do | ||
real_var=${var#GH_} | ||
# Inject variable in the app on Clever Cloud | ||
clever env set $real_var "${!var}" | ||
done | ||
# Inject these secrets in your GitHub repository. | ||
# List more secrets if needed, for example: HUGO_VERSION: ${{ secrets.HUGO_VERSION }} | ||
|
||
|
||
jobs: | ||
deploy: | ||
if: github.event.action == 'opened'|| github.event.action == 'reopened' | ||
runs-on: ubuntu-latest | ||
permissions: | ||
issues: write | ||
pull-requests: write | ||
contents: read | ||
environment: | ||
name: PR review apps | ||
steps: | ||
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." | ||
- name: Check out repository code | ||
uses: actions/checkout@v4 | ||
with: | ||
repository: ${{ github.event.pull_request.head.repo.full_name }} | ||
ref: ${{ github.event.pull_request.head.ref }} | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
fetch-depth: 0 | ||
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner." | ||
- run: echo "🖥️ The workflow is now ready to test your code on the runner." | ||
- name: List files in the repository | ||
run: | | ||
ls ${{ github.workspace }} | ||
- run: echo "🍏 This job's status is ${{ job.status }}." | ||
- name: install clever-tools | ||
run: npm install -g clever-tools | ||
- name: Create and deploy app | ||
env: | ||
CLEVER_SECRET: ${{ secrets.CLEVER_SECRET }} | ||
CLEVER_TOKEN: ${{ secrets.CLEVER_TOKEN }} | ||
ORGA_ID: ${{ secrets.ORGA_ID }} | ||
# Use "clever create" to deploy your app. | ||
# Replace <type> and <region> | ||
run: | | ||
clever create --type <type> ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }} --alias ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }} --region <region> --org "$ORGA_ID" | ||
# Set environment variable with "clever env set". | ||
# For example: clever env set CC_WEBROOT "/public" | ||
# Inject your secrets as well, for ex: | ||
#clever env set HUGO_VERSION ${{env.HUGO_VERSION}} | ||
clever env set <VARIABLE_NAME> "<variable_value>" | ||
# Set review app domain with "clever domain add". | ||
clever domain add ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }}.cleverapps.io | ||
clever deploy | ||
# Post your domain in PR's discussion | ||
- name: Comment PR | ||
uses: actions/github-script@v5 | ||
with: | ||
script: | | ||
const issue_number = context.payload.pull_request.number; | ||
const message = `Deployment has finished 👁️👄👁️ Your app is available [here](https://${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }}.cleverapps.io)`; | ||
github.rest.issues.createComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: issue_number, | ||
body: message | ||
}); | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
# Deploy review apps on new commits | ||
update: | ||
if: github.event.action == 'synchronize' | ||
runs-on: ubuntu-latest | ||
permissions: | ||
issues: write | ||
contents: read | ||
pull-requests: write | ||
steps: | ||
- name: Check out repository code | ||
uses: actions/checkout@v4 | ||
with: | ||
repository: ${{ github.event.pull_request.head.repo.full_name }} | ||
ref: ${{ github.event.pull_request.head.ref }} | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
fetch-depth: 0 | ||
- name: install clever-tools | ||
run: npm install -g clever-tools | ||
- name: Link and update app | ||
env: | ||
CLEVER_SECRET: ${{ secrets.CLEVER_SECRET }} | ||
CLEVER_TOKEN: ${{ secrets.CLEVER_TOKEN }} | ||
ORGA_ID: ${{ secrets.ORGA_ID }} | ||
run: | | ||
clever link -o "$ORGA_ID" ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }} | ||
clever deploy | ||
- name: Comment PR | ||
uses: actions/github-script@v5 | ||
with: | ||
script: | | ||
const issue_number = context.payload.pull_request.number; | ||
const message = `🚀 Your app has been updated and is available [here](https://${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }}.cleverapps.io)`; | ||
github.rest.issues.createComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: issue_number, | ||
body: message | ||
}); | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
# Delete review app when the PR is closed (merged or not) | ||
delete: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
issues: write | ||
pull-requests: write | ||
if: always() && github.event_name == 'pull_request_target' && github.event.action == 'closed' | ||
steps: | ||
- name: install clever-tools | ||
run: npm install -g clever-tools | ||
- name: Delete app | ||
run: | | ||
clever link -o "$ORGA_ID" ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }} | ||
clever delete --alias ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }} --yes | ||
- name: Comment PR | ||
uses: actions/github-script@v5 | ||
with: | ||
script: | | ||
const issue_number = context.payload.pull_request.number; | ||
const message = `Your review app has been deleted 👋`; | ||
github.rest.issues.createComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: issue_number, | ||
body: message | ||
}); | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
clever deploy | ||
fi | ||
elif [ "${{ github.event.action }}" = 'synchronize' ]; then | ||
clever link -o "$ORGA_ID" ${{ inputs.name }} | ||
clever deploy --force | ||
elif [ "${{ github.event.action }}" = 'closed' ]; then | ||
clever link -o "$ORGA_ID" ${{ inputs.name }} | ||
clever delete --alias ${{ inputs.alias }} | ||
fi | ||
shell: bash |
Oops, something went wrong.