Skip to content

Commit 087c733

Browse files
committed
RTDEV-577 - Archive-old-artifacts-worker-sample-for-deprecated-plugin
1 parent 6f29c1d commit 087c733

File tree

7 files changed

+626
-0
lines changed

7 files changed

+626
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Artifactory Archive Old Artifacts User Worker
2+
This worker is used to archive artifacts from a given source repository in Artifactory to a given destination repository. The artifacts are chosen based on a mixture of available parameters.
3+
4+
Note that this worker will delete your artifacts. The archive process is designed to preserve the name, path, and properties of an artifact, but save disk space by deleting the file contents. This worker is to be used for build artifacts that are no longer needed, when it's still useful to keep the build around for auditing or history purposes.
5+
6+
7+
Features
8+
Re-deploys artifacts that are to be archived, to save disk space.
9+
Archived artifacts are moved to an archive repository, to be separate from non-archived artifacts.
10+
Archived artifacts retain all properties that were set, and are also tagged with the archival timestamp.
11+
Input Parameters
12+
filePattern - the file pattern to match against in the source repository
13+
srcRepo - the source repository to scan for artifacts to be archived
14+
archiveRepo - the repository where matching artifacts are archived to
15+
archiveProperty - the name of the property to use when tagging the archived artifact with the archive timestamp
16+
Available 'time period' archive policies:
17+
lastModified - the last time the artifact was modified
18+
lastUpdated - the last time the artifact was updated
19+
created - the creation date of the artifact
20+
lastDownloaded - the last time the artifact was downloaded
21+
age - the age of the artifact
22+
NOTE: the time period archive policies are all specified in number of days
23+
24+
Available 'property' archive policies:
25+
includePropertySet - the artifact will be archived if it possesses all of the passed in properties
26+
excludePropertySet - the artifact will not be archived if it possesses all of the passed in properties
27+
NOTE: property set format ⇒ prop[:value1[;prop2[:value2]......[;propN[:valueN]]])
28+
29+
A property key must be provided, but a corresponding value is not necessary. If a property is set without a value, then a check is made for just the key.
30+
31+
Available artifact keep policy:
32+
numKeepArtifacts - the number of artifacts to keep per directory
33+
NOTE: This allows one to keep X number of artifacts (based on natural directory sort per directory). So, if your artifacts are laid out in a flat directory structure, you can keep the last X artifacts in each directory with this setting.
34+
35+
One can set any number of 'time period' archive policies as well as any number of include and exclude attribute sets. It is up to the caller to decide how best to archive artifacts. If no archive policy parameters are sent in, the plugin aborts in order to not allow default deleting of every artifact.
36+
37+
Archive Process
38+
The 'archive' process performs the following:
39+
40+
Grabs all of the currently set properties on the artifact
41+
Does a deploy over top of the artifact, to conserve space
42+
Adds all of the previously held attributes to the newly deployed artifact
43+
Moves the artifact from the source repository to the destination repository specified
44+
Adds a property containing the archive timestamp to the artifact
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "archive-old-artifact",
3+
"description": "This worker script is designed to archive old artifacts in Artifactory based on configurable criteria. It is useful for managing storage and maintaining repository hygiene.",
4+
"secrets": {},
5+
"sourceCodePath": "./worker.ts",
6+
"action": "GENERIC_EVENT",
7+
"enabled": false,
8+
"debug": true
9+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "archive-old-artifact",
3+
"description": "Run a script on GENERIC_EVENT",
4+
"version": "1.0.1",
5+
"scripts": {
6+
"deploy": "jf worker deploy",
7+
"undeploy": "jf worker rm \"archive-old-artifact\"",
8+
"test": "jest"
9+
},
10+
"license": "ISC",
11+
"devDependencies": {
12+
"jfrog-workers": "^0.4.0",
13+
"@golevelup/ts-jest": "^0.4.0",
14+
"@types/jest": "^29.5.12",
15+
"jest": "^29.7.0",
16+
"jest-jasmine2": "^29.7.0",
17+
"ts-jest": "^29.1.2"
18+
},
19+
"jest": {
20+
"moduleFileExtensions": [
21+
"ts",
22+
"js"
23+
],
24+
"rootDir": ".",
25+
"testEnvironment": "node",
26+
"clearMocks": true,
27+
"maxConcurrency": 1,
28+
"testRegex": "\\.spec\\.ts$",
29+
"moduleDirectories": ["node_modules"],
30+
"collectCoverageFrom": [
31+
"**/*.ts"
32+
],
33+
"coverageDirectory": "../coverage",
34+
"transform": {
35+
"^.+\\.(t|j)s$": "ts-jest"
36+
},
37+
"testRunner": "jest-jasmine2",
38+
"verbose": true
39+
}
40+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"filePattern": "example.txt",
3+
"srcRepo": "libs-release-local",
4+
"archiveRepo": "archive-repo",
5+
"lastModifiedDays": 30,
6+
"lastUpdatedDays": 0,
7+
"createdDays": 1,
8+
"lastDownloadedDays": 60,
9+
"ageDays": 30,
10+
"excludePropertySet": "keeper:true",
11+
"includePropertySet": "archive:true",
12+
"archiveProperty": "archived.timestamp",
13+
"numKeepArtifacts": 5
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"declaration": true,
5+
"target": "es2017",
6+
"skipLibCheck": true,
7+
"forceConsistentCasingInFileNames": false,
8+
"noFallthroughCasesInSwitch": false,
9+
"allowJs": true
10+
},
11+
"include": [
12+
"**/*.ts",
13+
"node_modules/@types/**/*.d.ts"
14+
]
15+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { PlatformContext } from 'jfrog-workers';
2+
import archiveOldArtifacts from './worker';
3+
4+
describe('archiveOldArtifacts', () => {
5+
let context: PlatformContext;
6+
7+
beforeEach(() => {
8+
context = {
9+
clients: {
10+
platformHttp: {
11+
get: jest.fn(),
12+
post: jest.fn(),
13+
put: jest.fn(),
14+
},
15+
},
16+
} as unknown as PlatformContext;
17+
});
18+
19+
20+
it('should throw an error when archiveRepo is missing', async () => {
21+
const params = {
22+
filePattern:"example.txt",
23+
srcRepo: "source-repo",
24+
archiveRepo:"",
25+
lastModifiedDays:30,
26+
lastUpdatedDays:80,
27+
createdDays:102,
28+
lastDownloadedDays:20,
29+
ageDays:102,
30+
excludePropertySet: "keeper:true",
31+
includePropertySet: "archive:true",
32+
archiveProperty: "archived.timestamp",
33+
numKeepArtifacts: 5
34+
};
35+
36+
await expect(() => archiveOldArtifacts(context, params)).rejects.toThrow(
37+
'Both srcRepo and archiveRepo must be defined, srcRepo: source-repo, archiveRepo: '
38+
);
39+
});
40+
41+
it('should throw an error when any of the day is missing', async () => {
42+
const params = {
43+
filePattern: "example.txt",
44+
srcRepo: "source-repo",
45+
archiveRepo: "archive-repo",
46+
lastModifiedDays: 0,
47+
lastUpdatedDays: 0,
48+
createdDays: 0,
49+
lastDownloadedDays: 0,
50+
ageDays: 0,
51+
excludePropertySet: "",
52+
includePropertySet: "",
53+
archiveProperty: "archived.timestamp",
54+
numKeepArtifacts: 5
55+
};
56+
57+
await expect(archiveOldArtifacts(context, params)).rejects.toThrow(
58+
'No selection criteria specified!'
59+
);
60+
});
61+
62+
63+
});

0 commit comments

Comments
 (0)