Skip to content

werft/build/prepare: Cleanup code #8643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions .werft/jobs/build/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
import * as shell from 'shelljs';
import { exec } from '../../util/shell';
import { Werft } from "../../util/werft";
import { GCLOUD_SERVICE_ACCOUNT_PATH } from "./const";

const phaseName = "prepare";

export async function prepare(werft: Werft) {
werft.phase("prepare");
werft.phase(phaseName);
try {
compareWerftAndGitpodImage()
activateCoreDevServiceAccount()
configureDocker()
configureCoreDevAccess()
} catch (err) {
werft.fail(phaseName, err);
}
werft.done(phaseName);
}

const werftImg = shell.exec("cat .werft/build.yaml | grep dev-environment").trim().split(": ")[1];
const devImg = shell.exec("yq r .gitpod.yml image").trim();
// TODO: The reasoning behind this step should be clarified
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's because both images need to be updated separately and letting them go out of sync may cause weird follow-up errors.

function compareWerftAndGitpodImage() {
const werftImg = exec("cat .werft/build.yaml | grep dev-environment", { silent: true }).trim().split(": ")[1];
const devImg = exec("yq r .gitpod.yml image", { silent: true }).trim();
if (werftImg !== devImg) {
werft.fail('prep', `Werft job image (${werftImg}) and Gitpod dev image (${devImg}) do not match`);
throw new Error(`Werft job image (${werftImg}) and Gitpod dev image (${devImg}) do not match`);
}
}

try {
exec(`gcloud auth activate-service-account --key-file "${GCLOUD_SERVICE_ACCOUNT_PATH}"`);
exec("gcloud auth configure-docker --quiet");
exec("gcloud auth configure-docker europe-docker.pkg.dev --quiet");
exec('gcloud container clusters get-credentials core-dev --zone europe-west1-b --project gitpod-core-dev');
werft.done('prep');
} catch (err) {
werft.fail('prep', err);
function activateCoreDevServiceAccount() {
const rc = exec(`gcloud auth activate-service-account --key-file "${GCLOUD_SERVICE_ACCOUNT_PATH}"`, { slice: phaseName }).code;

if (rc != 0) {
throw new Error("Failed to activate core-dev service account.")
}
}

function configureDocker() {
const rcDocker = exec("gcloud auth configure-docker --quiet", { slice: phaseName }).code;
const rcDockerRegistry = exec("gcloud auth configure-docker europe-docker.pkg.dev --quiet", { slice: phaseName }).code;

if (rcDocker != 0 || rcDockerRegistry != 0) {
throw new Error("Failed to configure docker with gcloud.")
}
}

function configureCoreDevAccess() {
const rc = exec('gcloud container clusters get-credentials core-dev --zone europe-west1-b --project gitpod-core-dev', { slice: phaseName }).code;

if (rc != 0) {
throw new Error("Failed to get core-dev kubeconfig credentials.")
}
}