Skip to content

Commit

Permalink
push image to GitHub Container Registry instead of Google
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasHuber committed Jul 16, 2024
1 parent 533f10f commit 5715c08
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
17 changes: 12 additions & 5 deletions .github/workflows/push-migration-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: serlo/configure-repositories/actions/setup-node@main
- uses: google-github-actions/auth@v2
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
credentials_json: '${{ secrets.GCP_KEY_CONTAINER_REGISTRY }}'
- run: gcloud auth configure-docker
- uses: google-github-actions/setup-gcloud@v2
- run: yarn migrate:push-image
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
run: |
cd packages/db-migrations
DOCKER_REGISTRY=ghcr.io DOCKER_REPOSITORY=$(echo $GITHUB_REPOSITORY)/db-migration tsx scripts/push-image.ts
env:
DOCKER_REGISTRY: ghcr.io
DOCKER_REPOSITORY: ${{ github.repository }}/db-migration
27 changes: 6 additions & 21 deletions packages/db-migrations/scripts/push-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void run()
async function run() {
const { version } = await fetchPackageJSON()
buildDockerImage({
name: 'api-db-migration',
name: 'db-migration',
version,
Dockerfile: path.join(root, 'Dockerfile'),
context: '.',
Expand Down Expand Up @@ -48,7 +48,9 @@ export function buildDockerImage({
throw new Error(`illegal version number ${version}`)
}

const remoteName = `eu.gcr.io/serlo-shared/${name}`
const registry = process.env.DOCKER_REGISTRY || 'ghcr.io'
const repository = process.env.DOCKER_REPOSITORY || `serlo/${name}`
const remoteName = `${registry}/${repository}`

if (!shouldBuild()) {
// eslint-disable-next-line no-console
Expand All @@ -64,25 +66,8 @@ export function buildDockerImage({
pushTags(versions)

function shouldBuild() {
const args = [
'container',
'images',
'list-tags',
remoteName,
'--filter',
`tags=${version}`,
'--format',
'json',
]

const result = spawnSync('gcloud', args, { stdio: 'pipe' })
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const images = JSON.parse(String(result.stdout))

if (!Array.isArray(images))
throw new Error('Wrong response from google cloud')

return images.length === 0
const result = spawnSync('docker', ['manifest', 'inspect', `${remoteName}:${version}`], { stdio: 'pipe' })
return result.status !== 0
}

function runBuild(versions: string[]) {
Expand Down

0 comments on commit 5715c08

Please sign in to comment.