diff --git a/README.md b/README.md index 7836387..2f5a306 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,15 @@ -# actions-path-to-docker-image-name -[GitHub Action](https://github.com/features/actions) to convert a directory path to a valid name. +# actions-branch-to-docker-tag-name +[GitHub Action](https://github.com/features/actions) to convert a branch name to a valid docker tag name + +If the branch is your `main` branch then `latest` will be returned. ## Example ```yaml -- uses: sleepypikachu/actions-path-to-docker-image-name@v1 - id: path-to-docker-image +- uses: sleepypikachu/actions-branch-to-docker-tag-name@v1 + id: branch-to-docker-tag with: - path: "./docker/foo/bar" - prefix: "./docker" + branch: "feature/some-branch-name" + prefix: "feature/" ``` diff --git a/action.yml b/action.yml index 00748e4..938acf8 100644 --- a/action.yml +++ b/action.yml @@ -1,17 +1,22 @@ -name: 'Path to Docker Image Name' -description: 'Converts a directory path to a valid docker image name' +name: 'Branch to Docker Tag Name' +description: 'Converts a branch to a valid docker tag name' inputs: - path: - description: 'The path to clean' + branch: + description: 'The branch to clean' required: true prefix: description: 'A prefix to remove' required: false default: '' + main: + description: 'The name of your main branch' + required: false + default: 'main' + outputs: image-name: - description: 'Valid docker image name' - value: ${{ steps.clean.outputs.image-name }} + description: 'Valid docker tag name' + value: ${{ steps.clean.outputs.tag-name }} branding: icon: 'activity' diff --git a/index.js b/index.js index 0328759..fe42dad 100644 --- a/index.js +++ b/index.js @@ -2,42 +2,38 @@ const core = require('@actions/core'); /** https://docs.docker.com/engine/reference/commandline/tag/#description -Name components may contain lowercase letters, digits and separators. -A separator is defined as a period, one or two underscores, or one or -more dashes. A name component may not start or end with a separator. +A tag name must be valid ASCII and may contain lowercase and uppercase +letters, digits, underscores, periods and dashes. A tag name may not +start with a period or a dash and may contain a maximum of 128 +characters. **/ try { - const path = core.getInput('path'); + const main = core.getInput('main') + const branch = core.getInput('branch'); const prefix = core.getInput('prefix'); - let result = path; + if (main == branch) { + core.setOutput('tag-name', 'latest'); + } else { + let result = branch; - // Remove prefix - if (path.startsWith(prefix)) { - result = result.substr(prefix.length); + // Remove prefix + if (branch.startsWith(prefix)) { + result = result.substr(prefix.length); + } + + // Remove invalid characters + result = result.replaceAll(/[^a-zA-Z0-9\._-]/g, '') + + // Remove start and end periods and dashes + result = result.replace(/^[-\.]+/, '') + result = result.replace(/[-\.]+$/, '') + + // Maximum of 128 characters + result = result.substr(0, 128) + + core.setOutput('tag-name', result); } - - // Remove start - result = result.replace(/^\./, ''); - - // Replace '/' (we expect lots of / in paths) - result = result.replaceAll('/', '-'); - - // Replace excessive underscores - result = result.replaceAll(/___+/g, '__'); - - // Lower case - result = result.toLowerCase(); - - // Remove invalid characters - result = result.replaceAll(/[^a-z0-9\._-]/g, '') - - // Remove start and end seperators - result = result.replace(/^[-\._]+/, '') - result = result.replace(/[-\._]+$/, '') - - core.setOutput('image-name', result); - } catch (error) { core.setFailed(error.message); } diff --git a/package.json b/package.json index 5457f46..d2210ca 100644 --- a/package.json +++ b/package.json @@ -1,23 +1,24 @@ { - "name": "actions-path-to-docker-image-name", + "name": "actions-branch-to-docker-tag-name", "version": "1.0.0", - "description": "[GitHub Action](https://github.com/features/actions) to convert a directory path to a valid name.", + "description": "[GitHub Action](https://github.com/features/actions) to convert a branch to a valid docker tag name.", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", - "url": "git+https://github.com/sleepypikachu/actions-path-to-docker-image-name.git" + "url": "git+https://github.com/sleepypikachu/actions-branch-to-docker-tag-name.git" }, "keywords": [], "author": "Elliot Iddon", "license": "MIT", "bugs": { - "url": "https://github.com/sleepypikachu/actions-path-to-docker-image-name/issues" + "url": "https://github.com/sleepypikachu/actions-branch-to-docker-tag-name/issues" }, - "homepage": "https://github.com/sleepypikachu/actions-path-to-docker-image-name#readme", + "homepage": "https://github.com/sleepypikachu/actions-branch-to-docker-tag-name#readme", "dependencies": { - "@actions/core": "^1.9.1" + "@actions/core": "^1.9.1", + "@actions/github": "^5.0.3" } }