Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: write status of the build/deployment as a PR comment when using github workflows #1115

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 79 additions & 4 deletions cognite_toolkit/_repo_files/GitHub/.github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name: Toolkit Dry Run
on:
pull_request:

env:
# set the width of the terminal emulator in github workflows
COLUMNS: 99

jobs:
build-modules:
runs-on: ubuntu-latest
Expand All @@ -17,9 +21,80 @@ jobs:
IDP_CLIENT_ID: ${{ vars.IDP_CLIENT_ID }}
IDP_CLIENT_SECRET: ${{ secrets.IDP_CLIENT_SECRET }}
IDP_TENANT_ID: ${{ vars.IDP_TENANT_ID }}
defaults:
run:
shell: bash
outputs:
build-status: ${{ steps.build.outcome }}
deploy-status: ${{ steps.deploy.outcome }}

steps:
- uses: actions/checkout@v4
- name: Create a folder for logs
run: |
if [ ! -d "cdf-tk-logs" ]; then
mkdir cdf-tk-logs
fi
- name: Build modules
id: build
continue-on-error: true
run: |
set -o pipefail
cdf build --verbose --env dev -o toolkit --build-dir cdf-tk-build | tee cdf-tk-logs/build.log
- name: Dry run modules deployment
id: deploy
continue-on-error: true
if: ${{ steps.build.outcome == 'success' }}
run: |
set -o pipefail
cdf deploy --verbose --dry-run cdf-tk-build | tee cdf-tk-logs/deploy.log
- name: Store logs
id: store-logs
uses: actions/upload-artifact@v4
with:
name: cdf-tk-logs
path: cdf-tk-logs/
retention-days: 1
if-no-files-found: error

pr-writeback:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a separate job because jq is not installed in the toolkit container. And to install it need to install curl first.

But jq is pre-installed in the default runner image, so just went with that.

Ideally, I'd bake jq into the toolkit docker image and then we could simplify the workflow a lot. No need to run a separate job, no need to upload/download log files. It'll be fast and simpler.

needs: build-modules
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v4
- name: Build the modules
run: cdf build
- name: Dry Run the modules deployment
run: cdf deploy --dry-run
- name: Download log files
id: download-logs
uses: actions/download-artifact@v4
with:
name: cdf-tk-logs
path: cdf-tk-logs
- name: Write a comment back to the PR
id: write_comment
shell: bash
run: |
set -e
B_STATUS="Build status: **${{ needs.build-modules.outputs.build-status }}**"
DR_STATUS="Dry run status: **${{ needs.build-modules.outputs.deploy-status }}**"

if [ -f cdf-tk-logs/deploy.log ]; then
MSG=$(cat cdf-tk-logs/deploy.log)
elif [ -f cdf-tk-logs/build.log ]; then
MSG=$(cat cdf-tk-logs/build.log)
else
MSG='Error. Check workflow run for details.'
fi

REPO_URL=$(jq '.repository.html_url' -r < "$GITHUB_EVENT_PATH")
PR_NUMBER=$(jq '.number' -r < "$GITHUB_EVENT_PATH")

RUN_LINK="[$GITHUB_RUN_ID]($REPO_URL/actions/runs/$GITHUB_RUN_ID)"

COMMENT=$(printf 'Toolkit dry run triggered: %s\n\n%s\n%s\n\n```\n%s\n```\n' \
"$RUN_LINK" "$B_STATUS" "$DR_STATUS" "$MSG")

gh pr comment $PR_NUMBER -b "$COMMENT"
env:
GH_TOKEN: ${{ github.token }}
Loading