generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
7 changed files
with
444 additions
and
19 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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
name: Test PR post artifact | ||
|
||
on: | ||
pull_request: | ||
branches: [main] | ||
pull_request_target: | ||
paths: | ||
- '.github/workflows/test-post-artifact.yml' | ||
- 'post-artifact/action.yml' | ||
push: | ||
branches: [main] | ||
paths: | ||
- '.github/workflows/test-post-artifact.yml' | ||
- 'post-artifact/action.yml' | ||
|
||
jobs: | ||
build-artifact-container: | ||
runs-on: ubuntu-latest | ||
container: rocker/tidyverse:4.4.0 | ||
|
||
permissions: | ||
contents: read | ||
pull-requests: write | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
name: Checkout code | ||
|
||
- uses: actions/upload-artifact@v4 | ||
name: Upload artifact | ||
with: | ||
path: './README.md' | ||
name: 'readme-rocker' | ||
|
||
- name: Install gh | ||
run: | | ||
apt update | ||
apt install -y --no-install-recommends gh | ||
- name: Post the artifact | ||
uses: ./post-artifact | ||
with: | ||
artifact-name: 'readme-rocker' | ||
python: 'python3' | ||
gh-token: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
build-artifact: | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
os: [ubuntu-latest, windows-latest, macos-latest] | ||
|
||
runs-on: ${{ matrix.os }} | ||
|
||
permissions: | ||
contents: read | ||
pull-requests: write | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
name: Checkout code | ||
|
||
- uses: actions/upload-artifact@v4 | ||
name: Upload artifact | ||
with: | ||
path: './README.md' | ||
name: ${{ format('readme-{0}', matrix.os) }} | ||
|
||
- name: Post the artifact | ||
uses: ./post-artifact | ||
with: | ||
artifact-name: ${{ format('readme-{0}', matrix.os) }} | ||
gh-token: ${{ secrets.GITHUB_TOKEN }} |
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
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Post artifact as a comment [![Test PR post artifact](https://github.com/CDCgov/cfa-actions/actions/workflows/test-post-artifact.yml/badge.svg)](https://github.com/CDCgov/cfa-actions/actions/workflows/test-post-artifact.yml) | ||
|
||
## Inputs | ||
|
||
| Field | Description | Required | Default | | ||
|---------------|-----------------------------------------------------------------------------------------------------------------------------------------|----------|----------------| | ||
| `gh-token` | The GitHub token to use for the API calls. | true | - | | ||
| `artifact-name` | Artifact name as in the `actions/upload-artifact` step. | false | `artifact` | | ||
| `message` | Message template to be posted in the PR. The message should include a placeholder for { artifact-url }. Optionally, the { artifact-name } placeholder can be used to include the artifact name in the message. | false | `'Thank you for your contribution ${{ github.actor }} :rocket:! Your { artifact-name } is ready for download :point_right: [here]({ artifact-url }) :point_left:!'` | | ||
| `python` | The path to the Python executable. | false | `'python'` | | ||
|
||
This action only runs in PRs and requires the `pull-requests: write` permission. | ||
|
||
## Example: Post artifact created within a job | ||
|
||
Here are the contents of a job that (i) uploads an artifact using `actions/upload-artifact` and (ii) posts the artifact as a comment using this action. The action requires the runner to have: `python` and `gh cli` installed. | ||
|
||
|
||
```yaml | ||
# Required permissions | ||
permissions: | ||
contents: read | ||
pull-requests: write | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
name: Checkout code | ||
|
||
# Uploading an artifact with name 'readme' | ||
- uses: actions/upload-artifact@v4 | ||
name: Upload artifact | ||
with: | ||
path: './README.md' | ||
name: readme | ||
|
||
# Post the artifact pulling the id from the `readme` step. | ||
- name: Post the artifact | ||
uses: CDCgov/cfa-actions/post-artifact@main | ||
with: | ||
artifact-name: readme | ||
gh-token: ${{ secrets.GITHUB_TOKEN }} | ||
message: 'Thank you for your contribution ${{ github.actor }} :rocket:! Your { artifact-name } is ready for download :point_right: [here]({ artifact-url }) :point_left:!' | ||
``` | ||
For a live example, see [../.github/workflows/test-post-artifact.yml](../.github/workflows/test-post-artifact.yml). | ||
## Example: Post an artifact created in a previous job | ||
When passing between jobs, the artifact id can be passed as an output. Here is an example of how to do it: | ||
```yaml | ||
jobs: | ||
|
||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
# Expose the artifact id as an output | ||
outputs: | ||
artifact-id: ${{ steps.upload.outputs.artifact-id }} | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
name: Checkout code | ||
|
||
# Uploading an artifact with id 'readme' | ||
- uses: actions/upload-artifact@v4 | ||
name: Upload artifact | ||
with: | ||
path: './README.md' | ||
name: readme | ||
|
||
post: | ||
runs-on: ubuntu-latest | ||
|
||
# This job depends on the `build` job | ||
needs: build | ||
|
||
# Required permissions | ||
permissions: | ||
contents: read | ||
pull-requests: write | ||
|
||
steps: | ||
# Post the artifact pulling the id from the `readme` step. | ||
# The msg will refer to the arfitact as 'README file'. | ||
- name: Post the artifact | ||
uses: CDCgov/cfa-actions/post-artifact@main | ||
with: | ||
artifact-name: readme | ||
gh-token: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
``` |
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
name: post-artifact | ||
description: | | ||
Creates a post in the PR linking to an artifact for easy access. | ||
Subsequent runs will update the comment. | ||
inputs: | ||
artifact-name: | ||
description: | | ||
Name of the artifact to upload (should match the one passed to | ||
`actions/upload-artifact`). | ||
required: false | ||
default: 'artifact' | ||
message: | ||
description: | | ||
Message template to be posted in the PR. The message should include | ||
a placeholder for { artifact-url }. Optionally, the { artifact-name } | ||
placeholder can be used to include the artifact name in the message. | ||
required: false | ||
default: 'Thank you for your contribution @${{ github.actor }} :rocket:! Your { artifact-name } is ready for download :point_right: [here]({ artifact-url }) :point_left:!' | ||
python: | ||
description: | | ||
The path to the Python executable. This input is optional and | ||
defaults to 'python'. | ||
required: false | ||
default: 'python' | ||
gh-token: | ||
description: | | ||
The GitHub token to use for the API calls. | ||
required: true | ||
runs: | ||
using: 'composite' | ||
|
||
steps: | ||
|
||
# https://docs.github.com/en/rest/actions/artifacts?apiVersion=2022-11-28#list-workflow-run-artifacts | ||
- name: List artifacts | ||
if: ${{ github.event_name == 'pull_request' }} | ||
run: | | ||
gh api \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/artifacts > _artifacts-${{ github.sha }}.json | ||
shell: bash | ||
env: | ||
GH_TOKEN: ${{ inputs.gh-token }} | ||
|
||
- name: Id artifact | ||
if: ${{ github.event_name == 'pull_request' }} | ||
run: ${{ inputs.python }} ${GITHUB_ACTION_PATH}/scripts/id-artifact.py | ||
shell: bash | ||
env: | ||
ARTIFACT_NAME: ${{ inputs.artifact-name }} | ||
SHA: ${{ github.sha }} | ||
|
||
- name: Get artifact id | ||
if: ${{ github.event_name == 'pull_request' }} | ||
id: artifact-id | ||
run: | | ||
echo "VALUE=$(cat '${{ github.sha }}_artifact_id')" >> $GITHUB_OUTPUT | ||
shell: bash | ||
|
||
- name: Compose message | ||
if: ${{ github.event_name == 'pull_request' }} | ||
run: ${{ inputs.python }} ${GITHUB_ACTION_PATH}/scripts/compose-msg.py | ||
shell: bash | ||
env: | ||
ARTIFACT_NAME: ${{ inputs.artifact-name }} | ||
MESSAGE: ${{ inputs.message }} | ||
SERVER_URL: ${{ github.server_url }} | ||
REPOSITORY: ${{ github.repository }} | ||
RUN_ID: ${{ github.run_id }} | ||
ARTIFACT_ID: ${{ steps.artifact-id.outputs.VALUE }} | ||
SHA: ${{ github.sha }} | ||
|
||
- name: Get the event | ||
if: ${{ github.event_name == 'pull_request' }} | ||
run: | | ||
gh api \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
repos/${{ github.repository }}/issues/${{ github.event.number }}/comments > _events-${{ github.sha }}.json | ||
shell: bash | ||
env: | ||
GH_TOKEN: ${{ inputs.gh-token }} | ||
|
||
- name: Find the comment | ||
if: ${{ github.event_name == 'pull_request' }} | ||
run: ${{ inputs.python }} ${GITHUB_ACTION_PATH}/scripts/find-comment.py | ||
shell: bash | ||
env: | ||
SHA: ${{ github.sha }} | ||
ARTIFACT_NAME: ${{ inputs.artifact-name }} | ||
|
||
- name: Putting the contents of _msg.txt into an environment var | ||
if: ${{ github.event_name == 'pull_request' }} | ||
id: set-env | ||
run: | | ||
echo "MSG=$(cat msg-${{ github.sha }}.txt)" >> $GITHUB_OUTPUT | ||
echo "ID=$(cat _ID-${{ github.sha }})" >> $GITHUB_OUTPUT | ||
echo "FOUND=$(cat _ID-${{ github.sha }}_found)" >> $GITHUB_OUTPUT | ||
shell: bash | ||
|
||
# See: | ||
# https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#update-an-issue-comment | ||
- name: Add comment | ||
if: ${{ github.event_name == 'pull_request' && steps.set-env.outputs.FOUND == 'false' }} | ||
run: | | ||
echo "No comment from github-bot found, adding a new one." | ||
gh pr comment -R ${{ github.repository }} \ | ||
${{ github.event.number }} -b "${{ steps.set-env.outputs.MSG }}" | ||
shell: bash | ||
env: | ||
GH_TOKEN: ${{ inputs.gh-token }} | ||
|
||
- name: Update comment | ||
if: ${{ github.event_name == 'pull_request' && steps.set-env.outputs.FOUND == 'true' }} | ||
run: | | ||
echo "Editing original comment id: ${{ steps.set-env.outputs.ID }}." | ||
gh api \ | ||
--method PATCH \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
repos/${{ github.repository }}/issues/comments/${{ steps.set-env.outputs.ID }} \ | ||
-f "body=${{ steps.set-env.outputs.MSG }}" | ||
shell: bash | ||
env: | ||
GH_TOKEN: ${{ inputs.gh-token }} |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import re | ||
|
||
# Retrieving environment variables | ||
import os | ||
import sys | ||
|
||
ARTIFACT_NAME = os.environ.get('ARTIFACT_NAME') | ||
MESSAGE = os.environ.get('MESSAGE') | ||
SERVER_URL = os.environ.get('SERVER_URL') | ||
REPOSITORY = os.environ.get('REPOSITORY') | ||
RUN_ID = os.environ.get('RUN_ID') | ||
ARTIFACT_ID = os.environ.get('ARTIFACT_ID') | ||
SHA = os.environ.get('SHA') | ||
|
||
msg = "[]("+ re.sub(r'\s+', '_', ARTIFACT_NAME)+")" | ||
msg = msg + MESSAGE | ||
|
||
updated = re.sub( | ||
r'{ artifact-name }', | ||
ARTIFACT_NAME, | ||
msg | ||
) | ||
|
||
if not re.search(r'{ artifact-url }', updated): | ||
print( | ||
"The message template must include the placeholder " \ | ||
"{ artifact-url }." | ||
) | ||
sys.exit(1) | ||
|
||
updated = re.sub( | ||
r'{ artifact-url }', | ||
SERVER_URL + '/' + REPOSITORY + '/actions/runs/' + RUN_ID + \ | ||
'/artifacts/' + ARTIFACT_ID, | ||
updated | ||
) | ||
|
||
with open('msg-' + SHA + '.txt', 'w') as file: | ||
file.write(updated) |
Oops, something went wrong.