-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
shota-kitazawa
committed
May 2, 2024
1 parent
24f9bba
commit 976067f
Showing
1 changed file
with
87 additions
and
63 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 |
---|---|---|
@@ -1,84 +1,108 @@ | ||
name: grant 'reviewapps' label if there are any changes in PR's source code. | ||
name: reviewapp | ||
|
||
on: pull_request | ||
on: | ||
push: | ||
branches-ignore: | ||
- main | ||
|
||
jobs: | ||
labeling: | ||
runs-on: ubuntu-latest # windows-latest | macos-latest | ||
reviewapp: | ||
name: create reviewapp files to dreamkast-infra | ||
runs-on: ubuntu-latest | ||
permissions: | ||
pull-requests: write | ||
name: grant 'reviewapps' label | ||
if: ${{ ! startsWith(github.head_ref, 'renovate/') }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
- uses: actions/setup-python@v5 | ||
|
||
- id: check_if_reviewapp | ||
continue-on-error: true | ||
env: | ||
GH_TOKEN: ${{ github.token }} | ||
run: | | ||
PR_NUMBER=$(gh pr view --json number -q .number) | ||
gh pr view --json labels -q .labels | jq '.[] | select(.name == "reviewapps")' -e | ||
echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT" | ||
- name: Install Go | ||
if: steps.check_if_reviewapp.outcome == 'success' | ||
uses: actions/setup-go@v5 | ||
|
||
- name: Install jsonnet | ||
if: steps.check_if_reviewapp.outcome == 'success' | ||
run: go install github.com/google/go-jsonnet/cmd/jsonnet@latest | ||
|
||
- name: Generate token | ||
if: steps.check_if_reviewapp.outcome == 'success' | ||
id: generate_token | ||
uses: tibdex/github-app-token@v2 | ||
with: | ||
python-version: '3.10' | ||
architecture: 'x64' | ||
app_id: ${{ secrets.APP_ID }} | ||
private_key: ${{ secrets.PRIVATE_KEY }} | ||
|
||
- name: Get changed files | ||
id: changed-files | ||
uses: tj-actions/changed-files@v42 | ||
- name: Configure AWS Credentials | ||
if: steps.check_if_reviewapp.outcome == 'success' | ||
uses: aws-actions/configure-aws-credentials@v4 | ||
with: | ||
use_fork_point: true | ||
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
aws-region: us-west-2 | ||
|
||
- name: List all changed files | ||
id: check-paths-ignore | ||
- name: Checkout dreamkast-infra | ||
if: steps.check_if_reviewapp.outcome == 'success' | ||
uses: actions/checkout@v4 | ||
with: | ||
repository: cloudnativedaysjp/dreamkast-infra | ||
path: dreamkast-infra | ||
token: ${{ steps.generate_token.outputs.token }} | ||
|
||
- name: Create reviewapps | ||
if: steps.check_if_reviewapp.outcome == 'success' | ||
working-directory: dreamkast-infra/ | ||
run: ecspresso/reviewapps/build-reviewapp.sh | ||
env: | ||
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} | ||
REPOSITORY_NAME: ${{ github.repository }} | ||
PR_NUMBER: ${{ steps.check_if_reviewapp.outputs.pr_number }} | ||
IMAGE_TAG: ${{ github.sha }} | ||
|
||
- name: Commit files | ||
if: steps.check_if_reviewapp.outcome == 'success' | ||
working-directory: dreamkast-infra/ | ||
run: | | ||
FLAG=$(cat << '_EOF_' | python | ||
import os | ||
import sys | ||
import pathlib | ||
paths_ignore = [ | ||
'.github/**/*.yml', | ||
'**.md', | ||
] | ||
all_changed_files = os.getenv("ALL_CHANGED_FILES").split() | ||
for filename in all_changed_files: | ||
if not any(list(map(lambda pattern: pathlib.PurePath(filename).match(pattern), paths_ignore))): | ||
print("false") | ||
sys.exit() | ||
print("true") | ||
_EOF_ | ||
) | ||
echo "::set-output name=FLAG::${FLAG}" | ||
git config --local user.email "[email protected]" | ||
git config --local user.name "GitHub Action" | ||
git status | ||
git add -A | ||
git commit -am "Bump docker tag (${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/commit/${GITHUB_SHA})" | ||
- name: Push changes | ||
if: steps.check_if_reviewapp.outcome == 'success' | ||
uses: ad-m/github-push-action@master | ||
with: | ||
github_token: ${{ steps.generate_token.outputs.token }} | ||
repository: cloudnativedaysjp/dreamkast-infra | ||
directory: dreamkast-infra | ||
branch: staging/dk-main | ||
|
||
- name: Labeling 'reviewapps' to PR | ||
uses: actions/github-script@v7 | ||
id: set-result | ||
if: ${{ steps.check-paths-ignore.outputs.FLAG == 'false' }} | ||
- name: Create and Merge Pull Request | ||
if: steps.check_if_reviewapp.outcome == 'success' | ||
uses: "actions/github-script@v7" | ||
with: | ||
result-encoding: string | ||
github-token: ${{ steps.generate_token.outputs.token }} | ||
script: | | ||
const message = ` | ||
Review app | ||
* https://dreamkast-dk-%d.dev.cloudnativedays.jp | ||
`; | ||
const targetLabel = 'reviewapps'; | ||
issue = await github.rest.issues.get({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: context.issue.number, | ||
const pr = await github.rest.pulls.create({ | ||
owner: "cloudnativedaysjp", | ||
repo: "dreamkast-infra", | ||
title: "Automated PR (staging/dk-main)", | ||
body: "**this PR is automatically created & merged**", | ||
head: "staging/dk-main", | ||
base: "main" | ||
}); | ||
flag = false; | ||
issue.data.labels.filter(label => { | ||
if (label.name == targetLabel) { flag = true; }; | ||
await github.rest.pulls.merge({ | ||
owner: "cloudnativedaysjp", | ||
repo: "dreamkast-infra", | ||
pull_number: pr.data.number, | ||
merge_method: "squash", | ||
}); | ||
if (!flag) { | ||
github.rest.issues.addLabels({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: context.issue.number, | ||
labels: [targetLabel] | ||
}); | ||
github.rest.issues.createComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: context.issue.number, | ||
body: message.replace("%d", context.issue.number), | ||
}); | ||
} |