various fixes and improvements

This commit is contained in:
James Brumond 2023-08-18 21:34:55 -07:00
parent af98273b46
commit db4149302a
Signed by: james
GPG Key ID: E8F2FC44BAA3357A
3 changed files with 19 additions and 10 deletions

View File

@ -7,6 +7,9 @@ inputs:
registry:
description: URL to the container registry
required: true
insecure-registry:
description: If "true", will use HTTP rather than HTTPS
required: false
image:
description: The name of the image in the registry
required: true

View File

@ -14,6 +14,8 @@ async function main() {
try {
const input = {
registry: core.getInput('registry'),
registry_url: null,
insecure_registry: core.getInput('insecure-registry') === 'true',
image: core.getInput('image'),
old_tag: core.getInput('old-tag'),
new_tags: core.getInput('new-tags').trim().split('\n'),
@ -22,12 +24,13 @@ async function main() {
token: null,
};
input.registry_url = `http${input.insecure_registry ? '' : 's'}://${input.registry}`;
console.log('Tagging %s/%s:%s with new tags', input.registry, input.image, input.old_tag, input.new_tags);
const manifest = await get_manifest(`${input.registry}/v2/${input.image}/manifests/${input.old_tag}`);
const manifest = await get_manifest(input);
const promises = input.new_tags.map((new_tag) => {
return put_manifest(`${input.registry}/v2/${input.image}/manifests/${new_tag}`, manifest);
return put_manifest(new_tag, manifest, input);
});
await Promise.all(promises);
@ -39,17 +42,18 @@ async function main() {
}
}
async function get_manifest(url_str, input) {
let { status, headers, body } = await http_req('GET', url_str, {
async function get_manifest(input) {
const path = `/v2/${input.image}/manifests/${input.old_tag}`;
let { status, headers, body } = await http_req('GET', input.registry_url + path, {
accept: manifest_media_type,
});
if (status === 401 && headers['www-authenticate']) {
await get_token(headers['www-authenticate'], input);
({ status, headers, body } = await http_req('GET', url_str, {
({ status, headers, body } = await http_req(input.insecure_registry, 'GET', input.registry_url + path, {
accept: manifest_media_type,
authorization: `Bearer ${token}`,
authorization: `Bearer ${input.token}`,
}));
}
@ -61,16 +65,17 @@ async function get_manifest(url_str, input) {
return body;
}
async function put_manifest(url_str, manifest) {
async function put_manifest(new_tag, manifest, input) {
const path = `/v2/${input.image}/manifests/${new_tag}`;
const req_headers = {
'content-type': manifest_media_type,
};
if (token) {
req_headers.authorization = `Bearer ${token}`;
req_headers.authorization = `Bearer ${input.token}`;
}
const { status, headers, body } = await http_req('PUT', url_str, req_headers, manifest);
const { status, headers, body } = await http_req('PUT', input.registry_url + path, req_headers, manifest);
if (status >= 400) {
console.error('get manifest response', { status, headers, body });

View File

@ -15,9 +15,10 @@ jobs:
- name: Tag the "gitea.example.com/owner/package:latest" image with some new tags
uses: https://gitea.jbrumond.me/actions/docker-tag@v0.1
with:
registry: https://gitea.example.com
registry: gitea.example.com
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
insecure-registry: false
image: owner/package
old-tag: latest
new-tags: |