Continuously Deploying GO app to google drive

Mohammed Hewedy
3 min readAug 1, 2019

--

Photo by Philip Swinburn on Unsplash

I am working on a simple go utility application that hides Facebook comments.

I wanted to distribute the binary via google drive, so I go through integrating gitlab with google drive client. here I will tell how.

First here’s the full .gitlab-ci.yml file:

# This file is a template, and might need editing before it works on your project.
image: golang:latest

variables:
# Please edit to your GitLab project
REPO_NAME: gitlab.com/mhewedy/hide_fb_comments

# The problem is that to be able to use go get, one needs to put
# the repository in the $GOPATH. So for example if your gitlab domain
# is gitlab.com, and that your repository is namespace/project, and
# the default GOPATH being /go, then you'd need to have your
# repository in /go/src/gitlab.com/namespace/project
# Thus, making a symbolic link corrects this.
before_script:
- mkdir -p $GOPATH/src/$(dirname $REPO_NAME)
- ln -svf $CI_PROJECT_DIR $GOPATH/src/$REPO_NAME
- cd $GOPATH/src/$REPO_NAME

stages:
- build
- deploy

build:
stage: build
script:
- GOOS=darwin go build -o $CI_PROJECT_DIR/hide_fb_comments
artifacts:
paths:
- hide_fb_comments

deploy:
stage: deploy
script:
- wget -O gdrive https://github.com/gdrive-org/gdrive/releases/download/2.1.0/gdrive-linux-x64
- chmod +x gdrive
- mv gdrive /usr/local/bin/gdrive
- mkdir -p ~/.gdrive && echo $TOKEN_V2_JSON > ~/.gdrive/token_v2.json
- gdrive update 19_8lz3V1jL4_MWRSbZYV2tPXA5S9lBbr hide_fb_comments

Let’s go step by step to get it working.

First, we need to get gdrive API access/refresh tokens, to do so let’s install gdrive locally so we can get the tokens.

Let’s start with running a Linux container to do the installation inside: (in case you working on mac or windows, or want to keep your machine clean)

docker run -it golang:latest

(any docker image with wget will work, but I wanted to simulate the one used by gitlab ci)

Let’s run the following in the container

wget -O gdrive https://github.com/gdrive-org/gdrive/releases/download/2.1.0/gdrive-linux-x64
chmod +x gdrive
mv gdrive /usr/local/bin/gdrive
gdrive about

you will be instructed to copy and paste a URL and then paste the token as follows:

This will write the tokens to a file ~/.gdrive/token_v2.json

In gitlab define a variable with the name TOKEN_V2_JSON in (gitlab -> settings -> CI/CD -> Variables) with the value from the ~/.gdrive/token_v2.json file.

Let’s recall the pipeline deploy stage:

deploy:
stage: deploy
script:
- wget -O gdrive https://github.com/gdrive-org/gdrive/releases/download/2.1.0/gdrive-linux-x64
- chmod +x gdrive
- mv gdrive /usr/local/bin/gdrive
- mkdir -p ~/.gdrive && echo $TOKEN_V2_JSON > ~/.gdrive/token_v2.json
- gdrive update <object_id> hide_fb_comments

last step in the script show that we used the gdrive to update an existing file by object id. so first, go and add the file manually, then to get the object id for the file to execute the command gdrive list and use it here.

Now with each push to the master branch, the deployment will be triggered and a new binary is being uploaded to google drive.

That’s all folks.

--

--

Mohammed Hewedy
Mohammed Hewedy

Responses (1)