Skip to content

Commit

Permalink
Add input to specify workflow conclusion (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ch4t4r authored Nov 9, 2020
1 parent 86f3002 commit 5b72739
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/download.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,18 @@ jobs:
run: |
cat artifact1/sha1 | grep $GITHUB_SHA
cat artifact2/sha2 | grep $GITHUB_SHA
download-conclusion:
runs-on: ubuntu-latest
needs: wait
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Download
uses: ./
with:
workflow: upload.yml
name: artifact
path: artifact
workflow_conclusion: success
- name: Test
run: cat artifact/sha | grep $GITHUB_SHA
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar

## Usage

> If `commit` or `pr` or `branch` or `run_id` is not specified then the artifact from the most recent completed workflow run will be downloaded.
> If `commit` or `pr` or `branch` or `run_id` or `workflow_conclusion` is not specified then the artifact from the most recent completed workflow run will be downloaded.
**Do not specify `pr`, `commit`, `branch` or `run_id` together. Pick just one or none.**
**Do not specify `pr`, `commit`, `branch`, `run_id` or `workflow_conclusion` together. Pick just one or none.**

```yaml
- name: Download artifact
Expand All @@ -18,6 +18,11 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar
github_token: ${{secrets.GITHUB_TOKEN}}
# Required, workflow file name or ID
workflow: workflow_name.yml
# Optional, the conclusion of a completed workflow to search for
# Can be one of:
# "failure", "success", "neutral", "cancelled", "skipped", "timed_out", "action_required"
# Ignores conclusion by default (thus using the most recent completed run when no other option is specified, regardless of conclusion)
workflow_conclusion: success
# Optional, will get head commit SHA
pr: ${{github.event.pull_request.number}}
# Optional, no need to specify if PR is
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ inputs:
workflow:
description: Workflow name
required: true
workflow_conclusion:
description: Wanted conclusion to search for in recent runs
required: false
repo:
description: Repository name with owner (like actions/checkout)
required: false
Expand Down
18 changes: 13 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,29 @@ const filesize = require('filesize')
const pathname = require('path')
const fs = require("fs")

const allowed_workflow_conclusions = ["failure", "success", "neutral", "cancelled", "skipped", "timed_out", "action_required"];

async function main() {
try {
const token = core.getInput("github_token", { required: true })
const workflow = core.getInput("workflow", { required: true })
const [owner, repo] = core.getInput("repo", { required: true }).split("/")
const path = core.getInput("path", { required: true })
const name = core.getInput("name")
const workflow_conclusion = core.getInput("workflow_conclusion")
let pr = core.getInput("pr")
let commit = core.getInput("commit")
let branch = core.getInput("branch")
let runID = core.getInput("run_id")

const client = github.getOctokit(token)

if ([runID, branch, pr, commit].filter(elem => elem).length > 1) {
throw new Error("don't specify `run_id`, `branch`, `pr`, and `commit` together")
if ([runID, branch, pr, commit, workflow_conclusion].filter(elem => elem).length > 1) {
throw new Error("don't specify `run_id`, `branch`, `pr`, `commit` and `workflow_conclusion` together")
}

if(workflow_conclusion && !allowed_workflow_conclusions.includes(workflow_conclusion)) {
throw new Error(`Unknown workflow conclusion '${workflow_conclusion}'`)
}

console.log("==> Repo:", owner + "/" + repo)
Expand Down Expand Up @@ -52,9 +59,10 @@ async function main() {
const run = runs.data.find(r => {
if (commit) {
return r.head_sha == commit
}
else {
// No PR or commit was specified just return the first one.
} else if(workflow_conclusion) {
return r.conclusion == workflow_conclusion
} else {
// No PR, commit or conclusion was specified; just return the first one.
// The results appear to be sorted from API, so the most recent is first.
// Just check if workflow run completed.
return r.status == "completed"
Expand Down

0 comments on commit 5b72739

Please sign in to comment.