Skip to content

Commit acd8693

Browse files
committed
chore: transform script into a composite action
1 parent 73fed71 commit acd8693

File tree

5 files changed

+191
-184
lines changed

5 files changed

+191
-184
lines changed

.github/workflows/main.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
on:
2+
pull_request_target:
3+
types: [opened, closed, synchronize, reopened]
4+
branches: [ main ]
5+
6+
jobs:
7+
deploy:
8+
name: Deploy/redeploy review app
9+
runs-on: ubuntu-latest
10+
permissions:
11+
issues: write
12+
pull-requests: write
13+
contents: read
14+
steps:
15+
- uses: actions/checkout@v4
16+
with:
17+
repository: ${{ github.event.pull_request.head.repo.full_name }}
18+
ref: ${{ github.event.pull_request.head.ref }}
19+
token: ${{ secrets.GITHUB_TOKEN }}
20+
fetch-depth: 0
21+
- name: Create review app
22+
uses: juliamrch/gh-action-composite@v20
23+
env:
24+
CLEVER_SECRET: ${{ secrets.CLEVER_SECRET }}
25+
CLEVER_TOKEN: ${{ secrets.CLEVER_TOKEN }}
26+
ORGA_ID: ${{ secrets.ORGA_ID }}
27+
GH_CC_RUN_SUCCEEDED_HOOK: ${{ secrets.CC_RUN_SUCCEEDED_HOOK }}
28+
with:
29+
type: 'static-apache'
30+
set-env: true
31+
environment: 'review'
32+
- name: Comment PR
33+
uses: actions/github-script@v7
34+
env:
35+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
with:
37+
script: |
38+
const issue_number = context.payload.pull_request.number;
39+
const message = `Deployment has finished 👁️👄👁️ Your app is available [here](https://${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }}.cleverapps.io)`;
40+
github.rest.issues.createComment({
41+
owner: context.repo.owner,
42+
repo: context.repo.repo,
43+
issue_number: issue_number,
44+
body: message
45+
});

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

README.md

Lines changed: 63 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,46 @@ The action will consider both branches on the same repository, and pull requests
1515
- An [organisation](https://developers.clever-cloud.com/doc/account/administrate-organization/) in Clever Cloud
1616
- [Clever Cloud CLI](https://developers.clever-cloud.com/doc/cli/getting_started/) installed in your machine to inject your tokens
1717

18+
### Mandatory configuration
19+
20+
Add the following secrets to your repository, in **Settings > Secrets and variables**:
21+
22+
- `CLEVER_SECRET` and `CLEVER_TOKEN`: find them in your `clever-tools.json` after installing the CLI (example path on Mac: `~/.config/clever-cloud/clever-tools.json`)
23+
- `ORGA_ID`: the organisation in which your app is created
24+
25+
For better security, we advise generating a specific `CLEVER_SECRET` and `CLEVER_TOKEN` for GitHub Actions. Follow these steps to do so:
26+
27+
1. Create a new user with a new email adress on Clever Cloud
28+
2. Create a specific organization for deploying review apps
29+
3. From your terminal, run `clever logout` and `clever login` right after
30+
4. Log into the Console with your new user credetials
31+
5. Get the generated `CLEVER_SECRET` and `CLEVER_TOKEN` and inject it into your repository secrets
32+
33+
Run `clever login` again and connect from your main account to set your personal tokens. Your GitHub Acction user's tokens won't be revoked and will be used only from GitHub.
34+
1835
## How to Use this Action
1936

20-
Place this script in your repository in `.github/workflows/` and modify the following values:
37+
1. In your `.github/workflow/review-app.yml`, define the event trigger for running the action:
2138

22-
- `<type>` for the type of app
23-
- `region` for where you want the app to be deployed
24-
- `<VARIABLE_NAME>` and `variable_value` for your environment variables
39+
```yaml
40+
on:
41+
pull_request_target:
42+
types: [opened, closed, synchronize, reopened]
43+
branches: [ main ]
44+
```
45+
46+
2. Then, define the mandatory input:
47+
48+
```yaml
49+
- name: Create review app
50+
uses: CleverCloud/clever-cloud-review-app@latest
51+
env:
52+
CLEVER_SECRET: ${{ secrets.CLEVER_SECRET }}
53+
CLEVER_TOKEN: ${{ secrets.CLEVER_TOKEN }}
54+
ORGA_ID: ${{ secrets.ORGA_ID }}
55+
with:
56+
type: '<type-of-app>'
57+
```
2558
2659
### Values for `--type`
2760

@@ -43,6 +76,32 @@ Place this script in your repository in `.github/workflows/` and modify the foll
4376
- `static-apache`: for static (HTML only) websites
4477
- `war`: for applications deployed as war files
4578

79+
### Inject secrets and variables
80+
81+
To inject your app secrets and environment variables on Clever Cloud, add them to your GitHub repository in **Settings > Secrets and variables**, then add them with an `GH_` prefix in your workflow file. Finally, enable the injection with `set-env: true`:
82+
83+
```yaml
84+
name: Create review app
85+
uses: CleverCloud/clever-cloud-review-app@latest
86+
env:
87+
CLEVER_SECRET: ${{ secrets.CLEVER_SECRET }}
88+
CLEVER_TOKEN: ${{ secrets.CLEVER_TOKEN }}
89+
ORGA_ID: ${{ secrets.ORGA_ID }}
90+
GH_CC_RUN_SUCCEEDED_HOOK: ${{ secrets. CC_RUN_SUCCEEDED_HOOK }} # This envrironment variable will be set on Clever Cloud
91+
with:
92+
type: '<type-of-app>'
93+
set-env: true # Enables the command to set en vars on Clever Cloud
94+
```
95+
96+
## Options
97+
98+
You can override default options by defining `region`, `domain`, `name`, and `alias`. Default values are:
99+
100+
- `region`=`par` (Paris)
101+
- `domain`=`<repo-name>-PR-#.cleverapps.io`
102+
- `name`=`<repo-name>-PR-#>`
103+
- `alias`=`<repo-name>-PR-#>`
104+
46105
### Values for `--region`
47106

48107
- `par` (Paris, [Clever Cloud](https://www.clever-cloud.com/infrastructure/))
@@ -54,40 +113,3 @@ Place this script in your repository in `.github/workflows/` and modify the foll
54113
- `sgp` (Singapore, OVHcloud)
55114
- `syd` (Sydney, OVHcloud)
56115
- `wsw` (Warsaw, OVHcloud)
57-
58-
## Secrets You'll Need
59-
60-
- `CLEVER_SECRET` and `CLEVER_TOKEN`: find them in your `clever-tools.json` after installing the CLI (example path on Mac: `~/.config/clever-cloud/clever-tools.json`)
61-
- `ORGA_ID`: the organisation in which your app is created
62-
63-
For better security, we advise generating a specific `CLEVER_SECRET` and `CLEVER_TOKEN` for GitHub Actions. Follow these steps to do so:
64-
65-
1. Create a new user with a new email adress on Clever Cloud
66-
2. Create a specific organization for deploying review apps
67-
3. From your terminal, run `clever logout` and `clever login` right after
68-
4. Log into the Console with your new user credetials
69-
5. Get the generated `CLEVER_SECRET` and `CLEVER_TOKEN` and inject it into your repository secrets
70-
71-
Run `clever login` again and connect from your main account to set your personal tokens. Your GitHub Acction user's tokens won't be revoked and will be used only from GitHub.
72-
73-
## Inject App Secrets
74-
75-
You can pass more secrets in your app by setting them in your GitHub repository and listing them in `env` and adding them like this : `<A_SECRET>: ${{ secrets.<A_SECRET> }}`.
76-
77-
Then when injecting environment variables in `Create and deploy app` step, add `clever env set <A_SECRET> "$<A_SECRET>"`.
78-
79-
For better security, follow this syntax and store the secrets in-memory for each step, to avoid exploits and leaks, instead ouf sourcing them directly in a shell script.
80-
81-
### Example Script
82-
83-
```yaml
84-
step: Create and deploy app
85-
env:
86-
...
87-
HUGO_VERSION: ${{ secrets.HUGO_VERSION }}
88-
89-
...
90-
- name: Set evironment variables
91-
run: |
92-
clever env set HUGO_VERSION "$HUGO_VERSION"
93-
```

action.yml

Lines changed: 67 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,69 @@
1-
name: Clever Cloud Review App on Pull Requests
1+
name: 'Clever Cloud review app on PRs'
2+
description: 'Deploy a review app on Clever Cloud when a PR is opened'
23

3-
description: Deploy, sync and delete review apps on Clever Cloud for every pull request targeting the `main` branch
4-
branding:
5-
icon: upload-cloud
6-
colore: red
7-
8-
on:
9-
pull_request_target:
10-
types: [opened, closed, synchronize, reopened]
11-
branches: [ main ]
4+
inputs:
5+
type:
6+
description: 'Which type of app to create'
7+
required: true
8+
name:
9+
description: 'The name of your app'
10+
required: true
11+
default: ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }}
12+
alias:
13+
description: 'The alias of your app'
14+
required: true
15+
default: ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }}
16+
region:
17+
description: 'The region to deploy on'
18+
required: true
19+
default: 'par'
20+
organization:
21+
description: 'The organization to deploy on'
22+
required: true
23+
default: $ORGA_ID
24+
domain:
25+
description: 'The domain to use for the app'
26+
required: false
27+
default: ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }}.cleverapps.io
28+
set-env:
29+
description: 'Set environment variables'
30+
type: boolean
31+
required: false
32+
default: false
33+
environment:
34+
description: 'Environment to run tests against'
35+
type: environment
36+
required: true
37+
default: ''
38+
runs:
39+
using: "composite"
40+
steps:
41+
- name: Install clever-tools
42+
shell: bash
43+
run: npm install -g clever-tools
44+
- name: Execute commands based on action
45+
run: |
46+
if [ "${{ github.event.action }}" = 'opened' ] || [ "${{ github.event.action }}" = 'reopened' ]; then
47+
clever create --type ${{ inputs.type }} ${{ inputs.name }} --alias ${{ inputs.alias }} --region ${{ inputs.region }} --org ${{ inputs.organization }}
48+
clever domain add ${{ inputs.domain }}
49+
# Only select environment variables with GH_ prefix
50+
# to exclude GitHub runner variables
51+
if ${{ inputs.set-env }}; then
52+
# Remove prefix from print
53+
for var in $(env | awk -F= '/^GH_/ { print $1 }')
54+
do
55+
real_var=${var#GH_}
56+
# Inject variable in the app on Clever Cloud
57+
clever env set $real_var "${!var}"
58+
done
1259
13-
# Inject these secrets in your GitHub repository.
14-
# List more secrets if needed, for example: HUGO_VERSION: ${{ secrets.HUGO_VERSION }}
15-
16-
17-
jobs:
18-
deploy:
19-
if: github.event.action == 'opened'|| github.event.action == 'reopened'
20-
runs-on: ubuntu-latest
21-
permissions:
22-
issues: write
23-
pull-requests: write
24-
contents: read
25-
environment:
26-
name: PR review apps
27-
steps:
28-
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
29-
- name: Check out repository code
30-
uses: actions/checkout@v4
31-
with:
32-
repository: ${{ github.event.pull_request.head.repo.full_name }}
33-
ref: ${{ github.event.pull_request.head.ref }}
34-
token: ${{ secrets.GITHUB_TOKEN }}
35-
fetch-depth: 0
36-
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
37-
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
38-
- name: List files in the repository
39-
run: |
40-
ls ${{ github.workspace }}
41-
- run: echo "🍏 This job's status is ${{ job.status }}."
42-
- name: install clever-tools
43-
run: npm install -g clever-tools
44-
- name: Create and deploy app
45-
env:
46-
CLEVER_SECRET: ${{ secrets.CLEVER_SECRET }}
47-
CLEVER_TOKEN: ${{ secrets.CLEVER_TOKEN }}
48-
ORGA_ID: ${{ secrets.ORGA_ID }}
49-
# Use "clever create" to deploy your app.
50-
# Replace <type> and <region>
51-
run: |
52-
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"
53-
# Set environment variable with "clever env set".
54-
# For example: clever env set CC_WEBROOT "/public"
55-
# Inject your secrets as well, for ex:
56-
#clever env set HUGO_VERSION ${{env.HUGO_VERSION}}
57-
clever env set <VARIABLE_NAME> "<variable_value>"
58-
# Set review app domain with "clever domain add".
59-
clever domain add ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }}.cleverapps.io
60-
clever deploy
61-
# Post your domain in PR's discussion
62-
- name: Comment PR
63-
uses: actions/github-script@v5
64-
with:
65-
script: |
66-
const issue_number = context.payload.pull_request.number;
67-
const message = `Deployment has finished 👁️👄👁️ Your app is available [here](https://${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }}.cleverapps.io)`;
68-
github.rest.issues.createComment({
69-
owner: context.repo.owner,
70-
repo: context.repo.repo,
71-
issue_number: issue_number,
72-
body: message
73-
});
74-
env:
75-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
76-
77-
# Deploy review apps on new commits
78-
update:
79-
if: github.event.action == 'synchronize'
80-
runs-on: ubuntu-latest
81-
permissions:
82-
issues: write
83-
contents: read
84-
pull-requests: write
85-
steps:
86-
- name: Check out repository code
87-
uses: actions/checkout@v4
88-
with:
89-
repository: ${{ github.event.pull_request.head.repo.full_name }}
90-
ref: ${{ github.event.pull_request.head.ref }}
91-
token: ${{ secrets.GITHUB_TOKEN }}
92-
fetch-depth: 0
93-
- name: install clever-tools
94-
run: npm install -g clever-tools
95-
- name: Link and update app
96-
env:
97-
CLEVER_SECRET: ${{ secrets.CLEVER_SECRET }}
98-
CLEVER_TOKEN: ${{ secrets.CLEVER_TOKEN }}
99-
ORGA_ID: ${{ secrets.ORGA_ID }}
100-
run: |
101-
clever link -o "$ORGA_ID" ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }}
102-
clever deploy
103-
- name: Comment PR
104-
uses: actions/github-script@v5
105-
with:
106-
script: |
107-
const issue_number = context.payload.pull_request.number;
108-
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)`;
109-
github.rest.issues.createComment({
110-
owner: context.repo.owner,
111-
repo: context.repo.repo,
112-
issue_number: issue_number,
113-
body: message
114-
});
115-
env:
116-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
117-
118-
# Delete review app when the PR is closed (merged or not)
119-
delete:
120-
runs-on: ubuntu-latest
121-
permissions:
122-
issues: write
123-
pull-requests: write
124-
if: always() && github.event_name == 'pull_request_target' && github.event.action == 'closed'
125-
steps:
126-
- name: install clever-tools
127-
run: npm install -g clever-tools
128-
- name: Delete app
129-
run: |
130-
clever link -o "$ORGA_ID" ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }}
131-
clever delete --alias ${{ github.event.pull_request.base.repo.name }}-PR-${{ github.event.number }} --yes
132-
- name: Comment PR
133-
uses: actions/github-script@v5
134-
with:
135-
script: |
136-
const issue_number = context.payload.pull_request.number;
137-
const message = `Your review app has been deleted 👋`;
138-
github.rest.issues.createComment({
139-
owner: context.repo.owner,
140-
repo: context.repo.repo,
141-
issue_number: issue_number,
142-
body: message
143-
});
144-
env:
145-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
60+
clever deploy
61+
fi
62+
elif [ "${{ github.event.action }}" = 'synchronize' ]; then
63+
clever link -o "$ORGA_ID" ${{ inputs.name }}
64+
clever deploy --force
65+
elif [ "${{ github.event.action }}" = 'closed' ]; then
66+
clever link -o "$ORGA_ID" ${{ inputs.name }}
67+
clever delete --alias ${{ inputs.alias }}
68+
fi
69+
shell: bash

0 commit comments

Comments
 (0)