Skip to content

Commit

Permalink
Authorize dockerhub calls to get access to private repos
Browse files Browse the repository at this point in the history
  • Loading branch information
vesameskanen committed Jul 4, 2023
1 parent 44918cd commit c462740
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ WORKDIR /usr/src/app
ENV CHECK_INTERVAL_MINUTES 5
ENV DEBUG ""
ENV TZ "Europe/Helsinki"
ENV DOCKER_USER
ENV DOCKER_AUTH

RUN apk add --update \
python3 \
build-base \
Expand Down
45 changes: 35 additions & 10 deletions src/dockerRepo.js
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)
})
}
}

0 comments on commit c462740

Please sign in to comment.