Skip to content

Commit

Permalink
Created retrieveArtifact
Browse files Browse the repository at this point in the history
  • Loading branch information
Demmonius committed Jan 8, 2021
1 parent 78a52d7 commit 572b480
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
48 changes: 47 additions & 1 deletion lib/manageArtifacts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand All @@ -9,9 +28,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Receive and manage variables
* Options:
* 0: Set value "value" to variable "key"
* 1: Retrieve value from variable "key"
*/
const path_1 = require("path");
const fs_1 = require("fs");
const core = __importStar(require("@actions/core"));
const config_1 = require("./config");
const artifact = require('@actions/artifact');
const io = require('@actions/io');
const defineVariableOperation = (variable) => {
try {
const variableContent = {
Expand Down Expand Up @@ -39,7 +67,6 @@ const defineVariableOperation = (variable) => {
}
};
const storeArtifact = (variables) => __awaiter(void 0, void 0, void 0, function* () {
const artifact = require('@actions/artifact');
const client = artifact.create();
const artifactOptions = {
retentionDays: 1 // Only keep artifacts 1 day to avoid reach limit: https://github.com/actions/toolkit/blob/c861dd8859fe5294289fcada363ce9bc71e9d260/packages/artifact/src/internal/upload-options.ts#L1
Expand All @@ -54,6 +81,19 @@ const storeArtifact = (variables) => __awaiter(void 0, void 0, void 0, function*
const uploadResponses = yield Promise.all(artifactsUploadPromises);
console.log(uploadResponses);
});
const retrieveArtifact = (variables) => __awaiter(void 0, void 0, void 0, function* () {
const client = artifact.create();
for (const variable of variables) {
try {
const file = path_1.join(config_1.WORKDIR, `${variable.key}.txt`);
yield client.downloadAllArtifacts(variable.key);
variable.value = fs_1.readFileSync(file, { encoding: 'utf8' }).toString();
}
catch (error) {
core.warning(`Cannot retrieve variable ${variable.key}`);
}
}
});
const manageArtifacts = (variables, delimiter) => __awaiter(void 0, void 0, void 0, function* () {
const io = require('@actions/io');
yield io.mkdirP(config_1.WORKDIR);
Expand All @@ -67,7 +107,13 @@ const manageArtifacts = (variables, delimiter) => __awaiter(void 0, void 0, void
console.log(error);
}
}
console.log("Before:");
console.log(variablesDetail);
yield storeArtifact(variablesDetail.filter((variable) => variable.operationToProceed === 0)
.map((variable) => variable.variableDetail));
yield retrieveArtifact(variablesDetail.filter((variable) => variable.operationToProceed === 1)
.map((variable) => variable.variableDetail));
console.log("After:");
console.log(variablesDetail);
});
exports.default = manageArtifacts;
35 changes: 27 additions & 8 deletions src/manageArtifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
* 0: Set value "value" to variable "key"
* 1: Retrieve value from variable "key"
*/
import {VariableDetail, VariableStatus} from "./types/variableStatus";
import artifact, {ArtifactClient, UploadOptions} from '@actions/artifact';
import io from '@actions/io';
import {join} from 'path';
import {readFileSync, mkdirSync, writeFileSync} from 'fs';
import rimraf from 'rimraf';

import * as core from '@actions/core';
import {VariableDetail, VariableStatus} from "./types/variableStatus";
import {WORKDIR} from "./config";
import {ArtifactClient, UploadOptions} from "@actions/artifact";

const artifact = require('@actions/artifact');
const io = require('@actions/io');

const defineVariableOperation = (variable: string): VariableStatus => {
try {
Expand All @@ -38,8 +39,6 @@ const defineVariableOperation = (variable: string): VariableStatus => {
}

const storeArtifact = async (variables: VariableDetail[]): Promise<void> => {
const artifact = require('@actions/artifact');

const client: ArtifactClient = artifact.create();
const artifactOptions: UploadOptions = {
retentionDays: 1 // Only keep artifacts 1 day to avoid reach limit: https://github.com/actions/toolkit/blob/c861dd8859fe5294289fcada363ce9bc71e9d260/packages/artifact/src/internal/upload-options.ts#L1
Expand All @@ -49,7 +48,7 @@ const storeArtifact = async (variables: VariableDetail[]): Promise<void> => {
console.log(variables);

for (const variable of variables) {
const file = join(WORKDIR, `${variable.key}.txt`);
const file: string = join(WORKDIR, `${variable.key}.txt`);

writeFileSync(file, variable.value, {encoding: 'utf8'});
artifactsUploadPromises.push(client.uploadArtifact(variable.key, [file], process.cwd(), artifactOptions));
Expand All @@ -58,6 +57,20 @@ const storeArtifact = async (variables: VariableDetail[]): Promise<void> => {
console.log(uploadResponses);
}

const retrieveArtifact = async (variables: VariableDetail[]): Promise<void> => {
const client: ArtifactClient = artifact.create();

for (const variable of variables) {
try {
const file = join(WORKDIR, `${variable.key}.txt`);
await client.downloadAllArtifacts(variable.key);
variable.value = readFileSync(file, {encoding: 'utf8'}).toString();
} catch (error) {
core.warning(`Cannot retrieve variable ${variable.key}`)
}
}
}

const manageArtifacts = async (variables: string, delimiter: string): Promise<void> => {
const io = require('@actions/io');
await io.mkdirP(WORKDIR);
Expand All @@ -70,8 +83,14 @@ const manageArtifacts = async (variables: string, delimiter: string): Promise<vo
console.log(error)
}
}
console.log("Before:")
console.log(variablesDetail)
await storeArtifact(variablesDetail.filter((variable: VariableStatus) => variable.operationToProceed === 0)
.map((variable: VariableStatus) => variable.variableDetail));
await retrieveArtifact(variablesDetail.filter((variable: VariableStatus) => variable.operationToProceed === 1)
.map((variable: VariableStatus) => variable.variableDetail));
console.log("After:")
console.log(variablesDetail)
}

export default manageArtifacts;

0 comments on commit 572b480

Please sign in to comment.