From 273c9cc67dfc12f879d0c89fa3d03b003d1d9b1a Mon Sep 17 00:00:00 2001 From: Kevin Velghe Date: Thu, 3 Dec 2020 06:29:15 +0100 Subject: [PATCH] Use whole branch name to determine docker tag (#35) --- dist/index.js | 7 +++++-- src/docker.js | 7 +++++-- tests/docker.test.js | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/dist/index.js b/dist/index.js index 7a04ca7..d276c37 100644 --- a/dist/index.js +++ b/dist/index.js @@ -565,8 +565,11 @@ const createTag = () => { // If we're not building a tag, use branch-prefix-{GIT_SHORT_SHA) as the Docker tag // refs/heads/jira-123/feature/something const branchName = ref.replace('refs/heads/', ''); - const branchPrefix = branchName.includes('/') ? branchName.substring(0, branchName.indexOf('/')) : branchName; - dockerTag = `${branchPrefix}-${shortSha}`; + const safeBranchName = branchName + .replace(/[^\w.-]+/g, '-') + .replace(/^[^\w]+/, '') + .substring(0, 120); + dockerTag = `${safeBranchName}-${shortSha}`; } else { core.setFailed( 'Unsupported GitHub event - only supports push https://help.github.com/en/articles/events-that-trigger-workflows#push-event-push' diff --git a/src/docker.js b/src/docker.js index c0edcbc..23d4600 100644 --- a/src/docker.js +++ b/src/docker.js @@ -22,8 +22,11 @@ const createTag = () => { // If we're not building a tag, use branch-prefix-{GIT_SHORT_SHA) as the Docker tag // refs/heads/jira-123/feature/something const branchName = ref.replace('refs/heads/', ''); - const branchPrefix = branchName.includes('/') ? branchName.substring(0, branchName.indexOf('/')) : branchName; - dockerTag = `${branchPrefix}-${shortSha}`; + const safeBranchName = branchName + .replace(/[^\w.-]+/g, '-') + .replace(/^[^\w]+/, '') + .substring(0, 120); + dockerTag = `${safeBranchName}-${shortSha}`; } else { core.setFailed( 'Unsupported GitHub event - only supports push https://help.github.com/en/articles/events-that-trigger-workflows#push-event-push' diff --git a/tests/docker.test.js b/tests/docker.test.js index c3218a4..bcdb210 100644 --- a/tests/docker.test.js +++ b/tests/docker.test.js @@ -40,7 +40,7 @@ describe('Create Docker image tag from git ref', () => { context.ref = 'refs/heads/jira-123/feature/some-cool-feature'; context.sha = 'f427b0b731ed7664ce4a9fba291ab25fa2e57bd3'; - expect(docker.createTag()).toBe('jira-123-f427b0b'); + expect(docker.createTag()).toBe('jira-123-feature-some-cool-feature-f427b0b'); }); test('Create from feature branch without Jira number', () => {