-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Authorize dockerhub calls to get access to private repos
- Loading branch information
1 parent
44918cd
commit c462740
Showing
2 changed files
with
38 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,46 @@ | ||
const debug = require('debug')('digitransit-deployer-repo') | ||
|
||
const loginRequest = `{ | ||
"username": "${process.env.DOCKER_USER}", | ||
"password": "${process.env.DOCKER_AUTH}" | ||
}` | ||
|
||
module.exports = { | ||
getImageDate: (repoAndRef) => { | ||
const [repository, tag] = repoAndRef.split(':') | ||
const url = `https://hub.docker.com/v2/repositories/${repository}/tags/${tag || 'latest'}` | ||
return fetch(url).then(res => { | ||
const [image, tag] = repoAndRef.split(':') | ||
const [namespace, repository] = image.split('/') | ||
const url = `https://hub.docker.com/v2/namespaces/${namespace}/repositories/${repository}/tags/${tag || 'latest'}` | ||
const tokenUrl = 'https://hub.docker.com/v2/users/login/' | ||
|
||
return fetch(tokenUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: loginRequest | ||
}).then(res => { | ||
if (res.ok) { | ||
return res.json() | ||
} else { | ||
debug(`failed to get access token from docker hub`) | ||
} | ||
}).then(body => { | ||
const token = body?.token | ||
return fetch(url, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `JWT ${token}` | ||
}}) | ||
}).then(res => { | ||
if (res.ok) { | ||
return res.json() | ||
} else { | ||
debug(`failed to fetch data for ${repoAndRef} from docker hub`) | ||
console.log(`failed to fetch data for ${repoAndRef} from docker hub`) | ||
} | ||
}).then(data => { | ||
return data && Date.parse(data.last_updated) | ||
}).catch(err => { | ||
debug(err) | ||
}) | ||
.then(data => { | ||
return Date.parse(data.last_updated) | ||
}) | ||
.catch(err => { | ||
debug(err) | ||
}) | ||
} | ||
} |