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.
Merge pull request #1 from CDCgov/first-commit
Adding post-artifact action and updating README
- Loading branch information
Showing
7 changed files
with
466 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,76 @@ | ||
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 | ||
if: ${{ github.event_name == 'pull_request' }} | ||
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 | ||
if: ${{ github.event_name == 'pull_request' }} | ||
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,95 @@ | ||
# 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, as Github-flavored Markdown (GFM). Use the placeholder `{ artifact-url }` to reference the URL of the uploaded artifact, either raw or as GFM formatted link. Use the `{ artifact-name }` placeholder 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 | ||
if: ${{ github.event_name == 'pull_request' }} | ||
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 | ||
if: ${{ github.event_name == 'pull_request' }} | ||
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,130 @@ | ||
name: post-artifact | ||
description: | | ||
Posts a comment on a PR linking to an artifact uploaded in CI for easy access and reference. | ||
Subsequent runs will update the comment with an updated link to the artifact. | ||
inputs: | ||
artifact-name: | ||
description: | | ||
Name of the artifact to which to link (should match the one passed to | ||
`actions/upload-artifact`). | ||
required: false | ||
default: 'artifact' | ||
message: | ||
description: | | ||
Message template to be posted in the PR, as Github-flavored | ||
Markdown (GFM). Use the placeholder `{ artifact-url }` to | ||
reference the URL of the uploaded artifact, either raw | ||
or as GFM formatted link. Use the `{ artifact-name }` | ||
placeholder 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: | ||
|
||
- name: Check this is a PR | ||
if: ${{ github.event_name != 'pull_request' }} | ||
run: | ||
echo "This action must only run in pull requests." | ||
echo "See https://github.com/CDCgov/cfa-actions for more information." | ||
exit 1 | ||
shell: bash | ||
|
||
# https://docs.github.com/en/rest/actions/artifacts?apiVersion=2022-11-28#list-workflow-run-artifacts | ||
- name: "Get list of available artifacts and store as .json" | ||
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: Fetch metadata for the target artifact | ||
run: ${{ inputs.python }} ${GITHUB_ACTION_PATH}/scripts/fetch-artifact-meta.py | ||
shell: bash | ||
env: | ||
ARTIFACT_NAME: ${{ inputs.artifact-name }} | ||
SHA: ${{ github.sha }} | ||
|
||
- name: Get artifact id | ||
id: artifact-meta | ||
run: | | ||
echo "id=$(cat '${{ github.sha }}_artifact_id')" >> $GITHUB_OUTPUT | ||
echo "expires_at=$(cat '${{ github.sha }}_artifact_expires_at')" >> $GITHUB_OUTPUT | ||
shell: bash | ||
|
||
- name: Compose message | ||
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-meta.outputs.id }} | ||
SHA: ${{ github.sha }} | ||
EXP_DATE: ${{ steps.artifact-meta.outputs.expires_at }} | ||
|
||
- name: Get the event | ||
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 | ||
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 | ||
id: set-env | ||
run: | | ||
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: ${{ 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 }} -F msg-${{ github.sha }}.txt | ||
shell: bash | ||
env: | ||
GH_TOKEN: ${{ inputs.gh-token }} | ||
|
||
- name: Update comment | ||
if: ${{ 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=@msg-${{ github.sha }}.txt' | ||
shell: bash | ||
env: | ||
GH_TOKEN: ${{ inputs.gh-token }} |
Oops, something went wrong.