0

As part of my CD pipeline, I am setting up a Google Cloud Function to handle new repo pushes, create docker images and push them to the registry. I have all working on a VM but there is no need to have one running 24x7 just for this.

So, looking over NodeJS reference library, I can't find a way to push an image to a registry using node. Seems like there is no registry or build sdk for node?

Basically, all I need is to execute this command from a cloud function: gcloud builds submit --tag gcr.io/my_project/my_image.

2
  • 1
    You may want to set a build trigger instead if your code resides in Github, Bitbucket or Cloud Source Repository: cloud.google.com/cloud-build/docs/running-builds/… Commented Nov 6, 2018 at 16:58
  • Yeah, that would work fine, but I was trying to find out if there was a way to do this from Node. Commented Nov 7, 2018 at 4:19

1 Answer 1

2

It's quite possible to do this using the Cloud Build API. Here's a simple example using the client libary for Node.js.

exports.createDockerBuild = async (req, res) => {
    const google = require('googleapis').google;
    const cloudbuild = google.cloudbuild({version: 'v1'});

    const client = await google.auth.getClient({
            scopes: ['https://www.googleapis.com/auth/cloud-platform']
    });
    const projectId = await google.auth.getProjectId();
    const resource = {
            "source": {
                    "storageSource": {
                            "bucket": "my-source-bucket",
                            "object": "my-nodejs-source.tar.gz"
                    }
            },
            "steps": [{
                    "name": "gcr.io/cloud-builders/docker",
                    "args": [
                            "build",
                            "-t",
                            "gcr.io/my-project-name/my-nodejs-image",
                            "standard-hello-world"
                    ]
            }],
            "images": ["gcr.io/$PROJECT_ID/my-nodejs-image"]
    };

    const params = {projectId, resource, auth: client};
    result= await cloudbuild.projects.builds.create(params);

    res.status(200).send("200 - Build Submitted");

};

My source code was in a bucket but you could pull it from a repo just as easily.

Bear in mind that you'll need to use the Node.js 8 beta runtime for the async stuff to work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.