Skip to content

Commit

Permalink
Use whole branch name to determine docker tag (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
paretje authored Dec 3, 2020
1 parent ef15bdf commit 273c9cc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
7 changes: 5 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
7 changes: 5 additions & 2 deletions src/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion tests/docker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 273c9cc

Please sign in to comment.