1
1
// @ts -check
2
2
import github from '@actions/github'
3
3
import core from '@actions/core'
4
+ import { MAIN_BRANCHES } from './const.mjs' ;
4
5
5
6
const randomTag = createRandomString ( 16 )
6
7
const context = github . context
@@ -13,6 +14,7 @@ const artifactName = getArtifactname()
13
14
const tagName = getTagname ( )
14
15
15
16
core . setOutput ( 'ARTIFACT_NAME' , artifactName )
17
+ core . setOutput ( 'SHOULD_RUN_BUILD' , JSON . stringify ( shouldRun ( ) ) )
16
18
core . setOutput ( 'DOCKER_TAG' , tagName )
17
19
core . setOutput ( 'GIT_BRANCH' , targetBranch )
18
20
core . setOutput ( 'GIT_SHA' , sha )
@@ -21,82 +23,91 @@ console.info(`Docker tag: ${tagName}`)
21
23
console . info ( `Git branch: ${ targetBranch } ` )
22
24
console . info ( `Git SHA: ${ sha } ` )
23
25
26
+ function shouldRun ( ) {
27
+ if ( eventName === 'merge_group' ) {
28
+ if ( MAIN_BRANCHES . includes ( targetBranch ) ) {
29
+ return true ;
30
+ }
31
+ }
32
+ return false ;
33
+ }
34
+
24
35
function getTagname ( ) {
25
- if ( eventName === 'pull_request' && context . payload . pull_request ?. number ) {
26
- throw new Error ( `Unsupported event: ${ eventName } ` )
27
- // return `pr-${context.payload.pull_request.number}-${randomTag}`;
28
- }
29
- if ( eventName === 'merge_group' ) {
30
- const dateString = new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] . replace ( / - / g, '' )
31
- if ( typeOfDeployment . dev ) {
32
- return `main_${ dateString } _${ randomTag } `
36
+ if ( eventName === 'pull_request' && context . payload . pull_request ?. number ) {
37
+ throw new Error ( `Unsupported event: ${ eventName } ` )
38
+ // return `pr-${context.payload.pull_request.number}-${randomTag}`;
33
39
}
34
- if ( typeOfDeployment . prod ) {
35
- return `release_${ dateString } _${ randomTag } `
40
+ if ( eventName === 'merge_group' ) {
41
+ const dateString = new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] . replace ( / - / g, '' )
42
+ if ( typeOfDeployment . dev ) {
43
+ return `main_${ dateString } _${ randomTag } `
44
+ }
45
+ if ( typeOfDeployment . prod ) {
46
+ return `release_${ dateString } _${ randomTag } `
47
+ }
48
+ throw new Error ( `Unable to determine artifact name for merge_group event` )
36
49
}
37
- throw new Error ( `Unable to determine artifact name for merge_group event` )
38
- }
39
- throw new Error (
40
- `Unable to determine artifact name for event type: ${ eventName } ` ,
41
- )
50
+ throw new Error (
51
+ `Unable to determine artifact name for event type: ${ eventName } ` ,
52
+ )
42
53
}
43
54
44
55
function getArtifactname ( ) {
45
- if ( eventName === 'pull_request' && context . payload . pull_request ?. number ) {
46
- throw new Error ( `Unsupported event: ${ eventName } ` )
47
- // return `pr-${context.payload.pull_request.number}`;
48
- }
49
- if ( eventName === 'merge_group' ) {
50
- if ( typeOfDeployment . dev ) {
51
- return `main-${ context . payload . merge_group . head_sha } `
56
+ if ( eventName === 'pull_request' && context . payload . pull_request ?. number ) {
57
+ throw new Error ( `Unsupported event: ${ eventName } ` )
58
+ // return `pr-${context.payload.pull_request.number}`;
52
59
}
53
- if ( typeOfDeployment . prod ) {
54
- return `release-${ context . payload . merge_group . head_sha } `
60
+ if ( eventName === 'merge_group' ) {
61
+ if ( typeOfDeployment . dev ) {
62
+ return `main-${ context . payload . merge_group . head_sha } `
63
+ }
64
+ if ( typeOfDeployment . prod ) {
65
+ return `release-${ context . payload . merge_group . head_sha } `
66
+ }
67
+ throw new Error ( `Unable to determine artifact name for merge_group event` )
55
68
}
56
- throw new Error ( `Unable to determine artifact name for merge_group event` )
57
- }
58
69
59
- throw new Error (
60
- `Unable to determine artifact name for event type: ${ eventName } ` ,
61
- )
70
+ throw new Error (
71
+ `Unable to determine artifact name for event type: ${ eventName } ` ,
72
+ )
62
73
}
63
74
64
75
function getTypeOfDeployment ( ) {
65
- if ( targetBranch === 'main' || targetBranch === 'mq-docker-pre-main' ) {
66
- return {
67
- dev : true ,
68
- prod : false ,
76
+ if ( MAIN_BRANCHES . includes ( targetBranch ) ) {
77
+ return {
78
+ dev : true ,
79
+ prod : false ,
80
+ }
69
81
}
70
- }
71
- if ( targetBranch . startsWith ( 'release' ) ) {
72
- return {
73
- dev : false ,
74
- prod : true ,
82
+ if ( targetBranch . startsWith ( 'release' ) ) {
83
+ return {
84
+ dev : false ,
85
+ prod : true ,
86
+ }
75
87
}
76
- }
77
- // UNKNOWN BRANCH
78
- // console.error(`Unknown branch: ${targetBranch} - not sure how to tag this deployment`);
79
- throw new Error ( `Unsupported branch: ${ targetBranch } ` )
88
+ // UNKNOWN BRANCH
89
+ // console.error(`Unknown branch: ${targetBranch} - not sure how to tag this deployment`);
90
+ throw new Error ( `Unsupported branch: ${ targetBranch } ` )
80
91
}
81
92
82
93
function getTargetBranch ( ) {
83
- if ( eventName === 'pull_request' && context . payload ?. pull_request ?. base . ref ) {
84
- return context . payload . pull_request . base . ref
85
- }
86
- if ( eventName === 'merge_group' ) {
87
- return context . payload . merge_group . base_ref . replace ( 'refs/heads/' , '' )
88
- }
94
+ if ( eventName === 'pull_request' && context . payload ?. pull_request ?. base . ref ) {
95
+ return context . payload . pull_request . base . ref
96
+ }
97
+ if ( eventName === 'merge_group' ) {
98
+ return context . payload . merge_group . base_ref . replace ( 'refs/heads/' , '' )
99
+ }
89
100
90
- throw new Error (
91
- `Unable to determine target branch for event type: ${ eventName } ` ,
92
- )
101
+ throw new Error (
102
+ `Unable to determine target branch for event type: ${ eventName } ` ,
103
+ )
93
104
}
94
105
95
106
function createRandomString ( length ) {
96
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
97
- let result = ''
98
- for ( let i = 0 ; i < length ; i ++ ) {
99
- result += chars . charAt ( Math . floor ( Math . random ( ) * chars . length ) )
100
- }
101
- return result
107
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
108
+ let result = ''
109
+ for ( let i = 0 ; i < length ; i ++ ) {
110
+ result += chars . charAt ( Math . floor ( Math . random ( ) * chars . length ) )
111
+ }
112
+ return result
102
113
}
0 commit comments