-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ci): add workflow and script for pushing staging image
- Loading branch information
1 parent
ca9a8e0
commit ac55e55
Showing
4 changed files
with
89 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: Deploy to Staging | ||
on: | ||
push: | ||
branches: | ||
- staging | ||
|
||
jobs: | ||
docker-image: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: serlo/configure-repositories/actions/setup-node@main | ||
- uses: google-github-actions/auth@v2 | ||
with: | ||
credentials_json: '${{ secrets.GCP_KEY_CONTAINER_REGISTRY }}' | ||
- run: gcloud auth configure-docker | ||
- uses: google-github-actions/setup-gcloud@v2 | ||
- run: yarn deploy:images:staging | ||
# TODO: delete pod |
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
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { spawnSync } from 'node:child_process' | ||
import * as path from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
|
||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)) | ||
|
||
const root = path.join(__dirname, '..') | ||
|
||
void run() | ||
|
||
function run() { | ||
buildDockerImage({ | ||
name: 'api-server', | ||
Dockerfile: path.join(root, 'docker', 'server', 'Dockerfile'), | ||
context: '../..', | ||
envName: 'staging' | ||
}) | ||
buildDockerImage({ | ||
name: 'api-swr-queue-worker', | ||
Dockerfile: path.join(root, 'docker', 'swr-queue-worker', 'Dockerfile'), | ||
context: '../..', | ||
envName: 'staging' | ||
}) | ||
} | ||
|
||
function buildDockerImage({ | ||
name, | ||
Dockerfile, | ||
context, | ||
envName | ||
}: DockerImageOptions) { | ||
const remoteName = `eu.gcr.io/serlo-shared/${name}` | ||
const date = new Date | ||
const timestamp = `${date.toISOString().split('T')[0]}-${date.getTime()}` | ||
|
||
const {stdout: gitHash} = spawnSync( | ||
'git', | ||
['rev-parse', '--short', 'HEAD'], | ||
) | ||
|
||
const remoteTags = toTags(remoteName, [envName, timestamp, gitHash.toString()]) | ||
const tags = [...remoteTags, ...toTags(name, [envName])] | ||
|
||
spawnSync( | ||
'docker', | ||
['build', '-f', Dockerfile, ...tags.flatMap((tag) => ['-t', tag]), context], | ||
{ stdio: 'inherit' }, | ||
) | ||
|
||
remoteTags.forEach((remoteTag) => { | ||
// eslint-disable-next-line no-console | ||
console.log('Pushing', remoteTag) | ||
spawnSync('docker', ['push', remoteTag], { stdio: 'inherit' }) | ||
}) | ||
} | ||
|
||
function toTags(name: string, versions: string[]) { | ||
return versions.map((version) => `${name}:${version}`) | ||
} | ||
|
||
interface DockerImageOptions { | ||
name: string | ||
Dockerfile: string | ||
context: string | ||
envName: 'staging' | 'production' | ||
} |