1+ // index.js (or main.js - see package.json "main" field)
2+
3+ const core = require ( '@actions/core' ) ;
4+ const exec = require ( '@actions/exec' ) ;
5+ const github = require ( '@actions/github' ) ;
6+
7+ async function run ( ) {
8+ try {
9+ // Get inputs
10+ const dockerfile = core . getInput ( 'Dockerfile' , { required : true } ) ;
11+ const dockerBuildArgs = core . getInput ( 'DockerBuildArgs' ) ; // Defaults to "" if not provided.
12+ const repository = core . getInput ( 'Repository' , { required : true } ) ;
13+
14+ const context = dockerfile . substring ( 0 , dockerfile . lastIndexOf ( '/' ) ) || '.' ; // Extract directory of Dockerfile. or .
15+ core . info ( `Dockerfile context directory: ${ context } ` ) ;
16+
17+ // Determine if we should use the cache. Avoid cache if it's a PR from a fork.
18+ const isPullRequest = github . context . eventName === 'pull_request' ;
19+ const isFork = isPullRequest && github . context . payload . pull_request . head . repo . fork ;
20+ const useCache = ! isFork ;
21+
22+ let azLoginRan = false ;
23+
24+ if ( useCache ) {
25+ // Log in to Azure
26+ try {
27+ await exec . exec ( 'az' , [ 'login' , '--identity' ] ) ;
28+ azLoginRan = true ;
29+ await exec . exec ( 'az' , [ 'acr' , 'login' , '-n' , 'onnxruntimebuildcache' ] ) ;
30+ } catch ( error ) {
31+ core . setFailed ( `Azure login or ACR login failed: ${ error . message } ` ) ;
32+ return ; // Exit if login fails. Critical error.
33+ }
34+ }
35+
36+
37+ // Build arguments for get_docker_image.py
38+ let scriptArgs = [
39+ 'tools/ci_build/get_docker_image.py' ,
40+ '--dockerfile' , dockerfile ,
41+ '--context' , context ,
42+ '--docker-build-args' , dockerBuildArgs ,
43+ '--repository' , repository ,
44+ ] ;
45+
46+ if ( useCache ) {
47+ scriptArgs . push ( '--container-registry' , 'onnxruntimebuildcache' ) ;
48+ scriptArgs . push ( '--use_imagecache' ) ;
49+ }
50+
51+
52+ // Execute the Python script.
53+ try {
54+ await exec . exec ( 'python3' , scriptArgs ) ;
55+ } catch ( error ) {
56+ core . setFailed ( `Docker build failed: ${ error . message } ` ) ;
57+ return ; // Exit if docker build failed.
58+ }
59+
60+ // Logout from Azure ACR (if we logged in)
61+ if ( azLoginRan ) {
62+ try {
63+ await exec . exec ( 'az' , [ 'acr' , 'logout' , '-n' , 'onnxruntimebuildcache' ] ) ;
64+ } catch ( error ) {
65+ core . warning ( `Azure ACR logout failed: ${ error . message } ` ) ; // Warning, not critical.
66+ }
67+ }
68+
69+ core . info ( 'Docker build completed successfully.' ) ;
70+
71+ } catch ( error ) {
72+ core . setFailed ( error . message ) ;
73+ }
74+ }
75+
76+ run ( ) ;
0 commit comments