Skip to content

Commit

Permalink
Improve GitHub Workflows (#7614)
Browse files Browse the repository at this point in the history
* feat: Improve update-issues-on-release
* feat: Improve workflow testing tools
* feat: Log uncommited files from update-gradle-dependencies workflow
* feat: Improve existing workflow tests
* fix: Add missing step names
* fix: Fix shellcheck comments
  • Loading branch information
PerfectSlayer authored Sep 13, 2024
1 parent a472c9d commit fcd206e
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 52 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/analyze-changes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ jobs:
with:
submodules: 'recursive'

- uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
- name: Cache Gradle dependencies
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
with:
path: |
~/.gradle/caches
Expand Down Expand Up @@ -97,7 +98,8 @@ jobs:
with:
submodules: 'recursive'

- uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
- name: Cache Gradle dependencies
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
with:
path: |
~/.gradle/caches
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
source "$(dirname "$0")/../env.sh"
testworkflow pull_request

This file was deleted.

37 changes: 31 additions & 6 deletions .github/workflows/tests/env.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
#!/bin/sh
#!/bin/bash

# Move to project root directory
FILE_PATH=$(dirname "$0")
cd $FILE_PATH/../../../../

export COMMON_ACT_ARGS="--container-architecture linux/amd64 --secret GITHUB_TOKEN="$(gh auth token)" --verbose"
function testworkflow() {
local EVENT_TYPE=$1
# Get workflow name
local TEST_PATH
TEST_PATH=$(dirname "$(readlink -f "${BASH_SOURCE[1]}")")
local WORKFLOW_NAME
WORKFLOW_NAME=$(basename "$TEST_PATH")
local WORKFLOW_FILE=.github/workflows/${WORKFLOW_NAME}.yaml
local PAYLOAD_FILE=${TEST_PATH}/payload-${EVENT_TYPE//_/-}.json
# Move to project root directory
local FILE_PATH
FILE_PATH=$(dirname "$0")
cd "$FILE_PATH/../../../../" || exit 1
# Check if workflow file and payload file exist
if [ ! -f "$WORKFLOW_FILE" ]; then
echo "Workflow file not found: $WORKFLOW_FILE"
exit 1
fi
if [ ! -f "$PAYLOAD_FILE" ]; then
echo "Payload file not found: $PAYLOAD_FILE"
exit 1
fi
# Run workflow using act
act "${EVENT_TYPE}" \
--workflows "${WORKFLOW_FILE}" \
--eventpath "${PAYLOAD_FILE}" \
--container-architecture linux/amd64 \
--secret GITHUB_TOKEN="$(gh auth token)" \
--verbose
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
source "$(dirname "$0")/../env.sh"
testworkflow create
3 changes: 0 additions & 3 deletions .github/workflows/tests/increment-milestone-on-tag/test.sh

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"release": {
"name": "1.39.0",
"html_url": "https://github.com/DataDog/dd-trace-java/releases/tag/v1.39.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"inputs": {
"milestone": "1.39.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
source "$(dirname "$0")/../env.sh"
testworkflow release
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
source "$(dirname "$0")/../env.sh"
testworkflow workflow_dispatch
4 changes: 4 additions & 0 deletions .github/workflows/update-gradle-dependencies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ jobs:
COUNT=0
fi
done
# Check at uncommited files
echo "Checking uncommited files"
git status
# Create a PR from the created branch
if [ $COUNT -gt 0 ]; then
echo "Creating a commit to $BRANCH_NAME@$BRANCH_HEAD with $GH_ADD_ARGS"
ghcommit --repository ${{ github.repository }} --branch $BRANCH_NAME --sha $BRANCH_HEAD $GH_ADD_ARGS --message "chore: Update Gradle dependencies"
Expand Down
81 changes: 43 additions & 38 deletions .github/workflows/update-issues-on-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,59 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Get milestone for release
id: milestone
uses: actions/github-script@47f7cf65b5ced0830a325f705cad64f2f58dddf7 # 3.1.0
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # 7.0.1
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const milestone_name = ("${{github.event_name}}" == "workflow_dispatch") ?
"${{github.event.inputs.milestone}}" : "${{github.event.release.name}}"
const milestones = await github.paginate(github.issues.listMilestones, {
// Get release milestone name
const milestoneName = "${{github.event_name}}" == "workflow_dispatch" ?
"${{github.event.inputs.milestone}}" :
"${{github.event.release.name}}"
// Get release URL
const releaseURL = "${{github.event_name}}" == "workflow_dispatch" ?
"${{github.event.repository.html_url}}/releases/tag/v${{github.event.release.name}}" :
"${{github.event.release.html_url}}"
core.info(`Notify of release availability: v${milestoneName}`)
// Get release milestone number
const milestoneNumber = await github.paginate(github.rest.issues.listMilestones, {
owner: context.repo.owner,
repo: context.repo.repo,
state: 'all'
})
return milestones.find( milestone => milestone.title == milestone_name ).number
- name: Get issues for milestone
id: issues
uses: actions/github-script@47f7cf65b5ced0830a325f705cad64f2f58dddf7 # 3.1.0
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const issues = await github.paginate(github.issues.listForRepo, {
}, (response, done) => {
const releaseMilestone = response.data.find(milestone => milestone.title == milestoneName)
if (releaseMilestone) {
done()
return releaseMilestone.number
}
}).then(milestones => milestones.pop())
if (!milestoneNumber) {
core.setFailed(`Failed to find milestone: ${milestoneName}`)
return
}
// Get all release milestone issues
const issues = await github.paginate(github.rest.issues.listForRepo, {
owner: context.repo.owner,
repo: context.repo.repo,
state: 'all',
milestone: ${{steps.milestone.outputs.result}}
})
// Pull requests are issues so filter them out
return issues.filter( issue => !issue["pull_request"] ).map( issue => issue.number )
- name: Comment and close issues
uses: actions/github-script@47f7cf65b5ced0830a325f705cad64f2f58dddf7 # 3.1.0
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
for (let issue_number of ${{ steps.issues.outputs.result }}) {
// This can be parallelized better by moving the await but it might trip rate limits
await github.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number,
body: ':robot: This issue has been addressed in the latest release. See full details in the [Release Notes]( ${{ github.event.release.html_url }}).'
})
await github.issues.update({
milestone: milestoneNumber
}, (response) => response.data.filter(issue => !issue["pull_request"]))
// Comment and close issues
for (const issue of issues) {
core.info(issue.state == 'open' ?
`Commenting and closing issue ${issue.number}` :
`Commenting on closed issue ${issue.number}`)
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number,
state: 'closed'
issue_number: issue.number,
body: `:robot: This issue has been addressed in the latest release. Check full details in the [Release Notes](${releaseURL}).`
})
if (issue.state == 'open') {
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
state: 'closed'
})
}
}

0 comments on commit fcd206e

Please sign in to comment.