Home > Net >  How to run same github actions job multiple times?
How to run same github actions job multiple times?

Time:09-29

I'm using gradle. I have project like that:

project/
--- sub1/
--- sub2/

I want to have artifact uploaded as 2 differents files (i.e. sub1.jar and sub2.jar separately).

Actually, I'm using this job:

- uses: actions/upload-artifact@v3
  with:
    name: Artifacts
    path: project*/build/libs/*.jar

But the file uploaded is only one file, with sub folder to files.

I tried to run same upload-artifact job, but with different argument. I can't do that.

I don't want to copy/paste the same job, because in the futur I will have multiple sub project, and I don't want to have 50 lines or same code ...

How can I upload my generated files, or run same job multiple times ?

CodePudding user response:

So using a matrix strategy would allow you to do this for a list of inputs.

You can do something like this as job in a workflow which would do the same steps for each value in the matrix.

  some-job:
    name: Job 1
    runs-on: ubuntu-latest
    strategy:
      matrix:
        subdir: [sub1, sub2]
    steps:
      - name: Create some files
        run: echo "test data" > /tmp/${{ matrix.subdir }}/.test.jar

      - uses: actions/upload-artifact@v3
        with:
          name: Artifacts
          path: /tmp/${{ matrix.subdir }}/*.jar

CodePudding user response:

It doesn't seems to be possible, so I made my own script. I'm using same code as actions/upload-artifact for the upload itself.

We should run JS script with the required dependency @actions/artifact. So, there is 2 actions to setup node and the dep.

My code is like that:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16
      - name: Install NPM package
        run: npm install @actions/artifact
      - uses: actions/github-script@v6
        name: Artifact script
        with:
          script: CHECK MY SCRIPT BELOW

I'm using this script to upload all files in all sub folder:

let artifact = require('@actions/artifact');

const fs = require('fs');
function getContentFrom(path, check) {
   return fs.readdirSync(path).filter(function (file) {
      return check == fs.statSync(path '/' file).isDirectory();
   });
}
function getDirectories(path) {
   return getContentFrom(path, true);
}
function getFiles(path) {
   return getContentFrom(path, false);
}
const artifactClient = artifact.create();
for(let sub of getDirectories("./")) { // get all folders
   console.log("Checking for", sub);

   let filesDir = "./"   sub; // if you are using multiples folder
   let files = [];
   for(let build of getFiles(filesDir)) {
       // here you can filter which files to upload
       files.push(filesDir   "/"   build);
   }

   console.log("Uploading", files);

   await artifactClient.uploadArtifact(
      "Project "   sub,
      files,
      filesDir,
      { continueOnError: false }
   )
}
  • Related