Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
identw committed Jul 11, 2024
1 parent f5a5278 commit 174af00
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 45 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Dockerfile'ы должны находится в одноименных папк
- name: nginx
```
Если в качестве registry будет передан ghcr.io, то имена образов будут формироваться по следующему принципу: `<registry>/<repo-name>:<image-name>-<tag>`:
Если в качестве registry будет передан ghcr.io, то имена образов будут формироваться по следующему принципу: `ghcr.io/<org-name>/<repo-name>:<image-name>-<tag>`:
```yaml
- id: set-tags
run: |
Expand Down Expand Up @@ -68,8 +68,8 @@ Dockerfile'ы должны находится в одноименных папк
- name: nginx
```
Будут собраны образы:
1. `ghcr.io/repo-name:server-<tag>`
1. `ghcr.io/repo-name:nginx-<tag>`
1. `ghcr.io/org-name/repo-name:server-<tag>`
1. `ghcr.io/org-name/repo-name:nginx-<tag>`

### Разделение шагов сборки и пуша в registry и копирование файлов из образов

Expand Down
13 changes: 3 additions & 10 deletions js-action/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as path from 'path';
import { Worker, isMainThread, workerData } from 'worker_threads';

// import {Util} from '@docker/actions-toolkit/lib/util';
import {generateRandomString, runCommand, createDir, ghcrDeleteVersion} from './lib.js';
import {generateRandomString, runCommand, createDir} from './lib.js';


async function main() {
Expand Down Expand Up @@ -132,13 +132,14 @@ async function main() {
continue;
}

const prePushTag = `0000001-${generateRandomString(8)}`;
let prePushTag = `0000001-${generateRandomString(8)}`;
let imageTag = `${registry}/${repoName}/${image.name}:${tag}`;
let imagePrePushTag = `${registry}/${repoName}/${image.name}:${prePushTag}`;

if (registry == githubRegistry) {
imageTag = `${registry}/${org}/${repoName}:${image.name}-${tag}`;
imagePrePushTag = `${registry}/${org}/${repoName}:${image.name}-${prePushTag}`;
prePushTag = `${image.name}-${prePushTag}`;
}

runCommand(`docker tag ${imageTag} ${imagePrePushTag}`);
Expand Down Expand Up @@ -169,14 +170,6 @@ async function main() {
});
});

for (const image of prePushImages) {
if (registry == githubRegistry) {
await ghcrDeleteVersion(org, repoName, token, image.tag);
} else {
console.log(`TODO delete ${image.registry}/${image.repo}:${image.tag} from docker api`);
}
}

core.setOutput('pushed-images', JSON.stringify(pushImages));
console.log('\x1b[34m%s\x1b[0m',`built images: ${JSON.stringify(pushImages, null, 2)}`);
await core.summary
Expand Down
64 changes: 32 additions & 32 deletions js-action/src/lib.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { execSync } from 'child_process';
import {existsSync, rmdirSync, mkdirSync} from 'fs';
import fetch from 'node-fetch';
// import fetch from 'node-fetch';


export function runCommand(command, exit = true) {
Expand Down Expand Up @@ -62,37 +62,37 @@ async function makeRequest(url, options) {
return r;
}

export async function ghcrDeleteVersion(org, repo, token, version) {
const url = `https://api.github.com/orgs/${org}/packages/container/${repo}/versions`;
const options = {
method: 'GET',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': 'Bearer ' + token,
'X-GitHub-Api-Version': '2022-11-28',
},
};
// export async function ghcrDeleteVersion(org, repo, token, version) {
// const url = `https://api.github.com/orgs/${org}/packages/container/${repo}/versions`;
// const options = {
// method: 'GET',
// headers: {
// 'Accept': 'application/vnd.github+json',
// 'Authorization': 'Bearer ' + token,
// 'X-GitHub-Api-Version': '2022-11-28',
// },
// };

const r = await makeRequest(url, options);
const f = r.filter((i) => i.metadata.container.tags.includes(version));
console.log(`Found ${f.length} versions for ${version}`);
// const r = await makeRequest(url, options);
// const f = r.filter((i) => i.metadata.container.tags.includes(version));
// console.log(`Found ${f.length} versions for ${version}`);

if (f.length > 0) {
console.log(`Deleting versions for ${version}`);
for (v of f) {
console.log(`Deleting version ${v.id}`)
const url = `https://api.github.com/orgs/${org}/packages/container/${repo}/versions/${v.id}`;
const options = {
method: 'DELETE',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': 'Bearer ' + token,
'X-GitHub-Api-Version': '2022-11-28',
},
};
await makeRequest(url, options);
console.log(`Deleted version ${v.id} successfully`);
}
}
}
// if (f.length > 0) {
// console.log(`Deleting versions for ${version}`);
// for (v of f) {
// console.log(`Deleting version ${v.id}`)
// const url = `https://api.github.com/orgs/${org}/packages/container/${repo}/versions/${v.id}`;
// const options = {
// method: 'DELETE',
// headers: {
// 'Accept': 'application/vnd.github+json',
// 'Authorization': 'Bearer ' + token,
// 'X-GitHub-Api-Version': '2022-11-28',
// },
// };
// await makeRequest(url, options);
// console.log(`Deleted version ${v.id} successfully`);
// }
// }
// }

0 comments on commit 174af00

Please sign in to comment.