Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save choinashil/9081bf4abb7d8b78f8443fd4a9e801ab to your computer and use it in GitHub Desktop.

Select an option

Save choinashil/9081bf4abb7d8b78f8443fd4a9e801ab to your computer and use it in GitHub Desktop.
GitHub Actions를 이용한 Nx앱 병렬 빌드
name: Build Affected Apps in Parallel
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
detect-affected-apps:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 20
- uses: actions/cache@v4
with:
path: |
~/.yarn
**/node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install dependencies
run: yarn install
- name: Determine affected apps
id: set-matrix
run: |
AFFECTED_APPS=$(npx nx affected:apps --plain --base=HEAD^ --head=HEAD) || (echo "Error running nx affected:apps" && exit 1)
APPS_JSON=$(echo $AFFECTED_APPS | xargs -n 1 | awk '{print "{\"app\": \""$0"\"}"}' | paste -sd, -)
AFFECTED_APPS_JSON="{\"include\":[$APPS_JSON]}"
echo "affected_apps=$AFFECTED_APPS" >> $GITHUB_OUTPUT
echo "affected_apps_json=$AFFECTED_APPS_JSON" >> $GITHUB_OUTPUT
if [ -z "$AFFECTED_APPS" ]; then
echo "has_affected_apps=false" >> $GITHUB_OUTPUT
else
echo "has_affected_apps=true" >> $GITHUB_OUTPUT
fi
outputs:
affected_apps: ${{ steps.set-matrix.outputs.affected_apps }}
affected_apps_json: ${{ steps.set-matrix.outputs.affected_apps_json }}
has_affected_apps: ${{ steps.set-matrix.outputs.has_affected_apps }}
comment:
needs: detect-affected-apps
runs-on: ubuntu-latest
steps:
- name: Comment on PR with affected apps
uses: actions/github-script@v7
with:
script: |
const issue_number = context.issue.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
const rawAffectedApps = `${{ needs.detect-affected-apps.outputs.affected_apps }}`;
const affectedApps = rawAffectedApps.split(' ').filter(app => app.trim() !== '');
const formattedApps = affectedApps.map(app => `\`${app}\``).join(', ');
const commentIdentifier = '<!-- AFFECTED_APPS_COMMENT -->';
let commentBody = '';
if (formattedApps) {
commentBody = `🙆 이번 작업으로 영향 받는 앱: ${formattedApps}`;
} else {
commentBody = '🙅 이번 작업으로 영향 받는 앱이 없습니다.';
}
const comments = await github.rest.issues.listComments({
owner,
repo,
issue_number,
});
const existingComment = comments.data.find(comment => comment.body.includes(commentIdentifier));
if (existingComment) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existingComment.id,
body: `${commentIdentifier}\n${commentBody}`,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body: `${commentIdentifier}\n${commentBody}`,
});
}
build:
needs: detect-affected-apps
runs-on: ubuntu-latest
if: needs.detect-affected-apps.outputs.has_affected_apps == 'true'
strategy:
fail-fast: false
matrix: ${{fromJson(needs.detect-affected-apps.outputs.affected_apps_json)}}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 20
- uses: actions/cache@v4
with:
path: |
~/.yarn
**/node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install dependencies
run: yarn install
- name: Build affected app
run: |
APP=${{ matrix.app }}
yarn build $APP dev || (echo "Build failed for $APP" && exit 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment