30

Is it possible for Github actions to upload a build artifact for commits on a release branch, and then for another workflow to download & use that artifact?

name: Deploy release to UAT & archive artifact
on:
  release:
    types: [published]
jobs:
  package:
    name: package and archive
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: '12'
      - name: serverless package
        uses: serverless/github-action@master
        with:
          args: package --stage=prod
      - name: Upload Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: .serverless
          asset_name: release-asset-${{ github.event.release.name }}.zip
          asset_content_type: application/zip
      - name: Upload Artifact
        uses: actions/upload-artifact@v2
        with:
          name: release-artifact-${{ github.event.release.name }}
          path: .serverless

...but how do you download the asset/artifact? I think up/download-artifact is intended only to be used only from the same workflow, and there doesn't seem to be an action for downloading a release asset.

name: Deploy to production
on:
  workflow_dispatch:
    inputs:
      release:
        description: Name of release to deploy
        required: true
        default: v1.0.0

jobs:
  deploy:
    name: deploy
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v1
      with:
        node-version: '12'
    - run: npm ci --only=prod
    - name: Download the release artifact
      uses: actions/download-artifact@v2
      with:
        name: release-${{ github.event.inputs.release }}
        path: .serverless
    - name: serverless deploy
      uses: serverless/github-action@master
      with:
        args: deploy --stage=prod --package=.serverless
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
1
  • This question is ambiguous since it doesn't maintain a clear distinction between workflow artifacts and release assets. The example creates both, the title mentions both "artifact" and "asset", and as a result, answers are inconsistent as to which scenario they solve. Your use case may be able to use them interchangeably, but others will have a requirement of only one or the other.
    – jschmitter
    Commented Oct 9, 2023 at 20:42

7 Answers 7

20

You can use download-workflow-artifact action.

0
6

One of my team mates got this working:

      - id: download-release-asset
        name: Download release asset
        uses: dsaltares/fetch-gh-release-asset@master
        with:
          version: tags/${{ env.RELEASE }}
          file: myproject-${{ env.RELEASE }}.tar.gz
          target: release.tar.gz
          token: ${{ secrets.DEPLOY_TOKEN }}
4
  • 2
    Note that this only works on Linux, not on Windows. Commented Jun 19, 2021 at 22:55
  • 1
    It now works on Linux, Windows and MacOS. Commented May 29, 2023 at 9:14
  • @DavidSaltares - all the releases from 0.0.6 to date say, "This action only supports Linux runners as this is a docker container action." Presumably there's no chance using it with a self-hosted Windows instance (that does not have Docker installed)? Commented Jul 5, 2023 at 7:22
  • 1
    @RobPomeroy, apologies, I just removed the notice from the README.md github.com/dsaltares/fetch-gh-release-asset/commit/…. The action only requires node to be installed now. Should run on all 3 runner platforms. Commented Jul 6, 2023 at 9:21
5

Using a 3rd party GitHub Action

I needed to access another workflow's artifacts as well and was able to by using the download workflow artifact GitHub action.

In my case, I have separate build and deploy workflows and I needed to access the build artifacts from the deploy workflow. This is how I accomplished it:

name: deploy

on:
  workflow_run:
    workflows: [build]
    types: [completed]
    branches: [main]

jobs:
  deploy:
    # Only run the deployment if the build workflow succeeded.
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    runs-on: ubuntu-latest
    steps:
      - name: Download build artifact from triggered workflow
        uses: dawidd6/action-download-artifact@v2
        with:
          run_id: ${{ github.event.workflow_run.id }}
          name: BuildArtifact
          path: ./artifacts
          search_artifacts: true

The deploy workflow automatically runs when the build workflow completes on the main branch, and we get the build workflow's run ID using github.event.workflow_run.id.

Using the GitHub API

If you do not want to use a 3rd party GitHub action and prefer to use the native GitHub API instead, the GitHub Docs here show how to accomplish this same task.

Workflow to upload the artifact:

name: Upload data

on:
  pull_request:

jobs:
  upload:
    runs-on: ubuntu-latest

    steps:
      - name: Save PR number
        env:
          PR_NUMBER: ${{ github.event.number }}
        run: |
          mkdir -p ./pr
          echo $PR_NUMBER > ./pr/pr_number
      - uses: actions/upload-artifact@v3
        with:
          name: pr_number
          path: pr/

Workflow to download the artifact:

name: Use the data

on:
  workflow_run:
    workflows: [Upload data]
    types:
      - completed

jobs:
  download:
    runs-on: ubuntu-latest
    steps:
      - name: 'Download artifact'
        uses: actions/github-script@v6
        with:
          script: |
            let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
               owner: context.repo.owner,
               repo: context.repo.repo,
               run_id: context.payload.workflow_run.id,
            });
            let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
              return artifact.name == "pr_number"
            })[0];
            let download = await github.rest.actions.downloadArtifact({
               owner: context.repo.owner,
               repo: context.repo.repo,
               artifact_id: matchArtifact.id,
               archive_format: 'zip',
            });
            let fs = require('fs');
            fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr_number.zip`, Buffer.from(download.data));

      - name: 'Unzip artifact'
        run: unzip pr_number.zip

      - name: 'Comment on PR'
        uses: actions/github-script@v6
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            let fs = require('fs');
            let issue_number = Number(fs.readFileSync('./pr_number'));
            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: issue_number,
              body: 'Thank you for the PR!'
            });
4

I found this quite easy these days using the API as described here:

https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow

All you need to do once the action is created is set the "workflow" permission on your access token.

Here is an example yml. Set OTHER_REPO to be your user and repo name and the WF_NAME is the yml action file and also set the ARTIFACT_NAME

name: Download Built Artifact
run-name: ${{ github.actor }} is testing out Download Artifact 🚀
on:
  workflow_dispatch:

jobs:
  download:
    runs-on: ubuntu-latest
    steps:
    - name: "Download artifact"
      run: |
        OTHER_REPO="USER/REPO"
        WF_NAME="action-file.yml"
        ARTIFACT_NAME="test_artifact"
        RUN_ID=`gh run --repo ${OTHER_REPO} list --workflow ${WF_NAME} --json databaseId --jq .[0].databaseId`
        gh run --repo ${OTHER_REPO} download ${RUN_ID} -n ${ARTIFACT_NAME}
        # List the artifiact directory/files
        ls -lR
      env:
        GITHUB_TOKEN: ${{ secrets.ARTIFACT_DEPLOY_TOKEN }}
1
  • This solution worked for me, however I had to use the personal access token with adequate access permissions for a GITHUB_TOKEN Commented Aug 29, 2023 at 10:57
2

Thanks to @Marcin Kłopotek, I get it done finally.

Here's one simple example(add to github workflow steps section as you need):

      - name: Download artifact
        id: download-artifact
        uses: dawidd6/action-download-artifact@v2
        with:
          name: production-files
          repo: {repo_with_username, e.g. xxx/yyy}
          workflow: {the other workflow filename e.g. build.yml}
          path: {path you desired to extract to e.g. ./static/}
          search_artifacts: true
0
2

actions/download-artifact v4 now supports that. You can check the documentation here https://github.com/actions/download-artifact?tab=readme-ov-file#download-artifacts-from-other-workflow-runs-or-repositories

You have to supply a PAT with the correct permissions

0

Thanks to TechWatching's answer for pointing out that this is now possible with the first-party GitHub actions/download-artifact action. I combined this with John's answer, using the GitHub CLI to get the run number of the source workflow for which to download the artifact. Overall, one might still prefer John's solution since it also uses the GitHub CLI to download the artifact in the same step rather than adding a separate actions/download-artifact step to the job.

See below for how my workflows looked, with test.yml producing an artifact that deploy.yml downloads. I should note that both of these workflows are in the same repository as well, which means I didn't have to setup any special GitHub Personal Access Tokens (PATs) or anything. Instead, simply using the default ${{ github.token }} provides the necessary permissions.

test.yml:

name: Test
jobs:
  test:
    name: Test
    runs-on: macos-14
    steps:
      - uses: actions/checkout@v4

      # Steps to generate the build artifact here

      - name: Upload generated artifact
        uses: actions/upload-artifact@v4
        id: upload-generated-artifact
        with:
          name: build-output # Match name used in deploy.yml download artifact step
          path: my-app # Path to build artifact

deploy.yml:

name: Deploy
on:
  workflow_run:
    workflows: [Test]
    branches: [main]
    types: [completed]
jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-22.04
    steps:
      - name: Get run ID of "Test" workflow
        id: get-run-id
        run: |
          OTHER_REPO="${{ github.repository }}"
          WF_NAME="Test"
          RUN_ID=`gh run --repo ${OTHER_REPO} list --workflow ${WF_NAME} --json databaseId --jq .[0].databaseId`
          echo "Detected latest run id of ${RUN_ID} for workflow ${WF_NAME}"
          echo "run-id=${RUN_ID}" >> "$GITHUB_OUTPUT"
        env:
          GH_TOKEN: ${{ github.token }}
      - name: Download artifact from "Test" workflow
        uses: actions/download-artifact@v4
        with:
          name: build-output # Match name used in test.yml upload artifact step
          github-token: ${{ github.token }}
          repository: ${{ github.repository }}
          run-id: ${{ steps.get-run-id.outputs.run-id }}

      - name: Show downloaded files
        run: ls -la

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.