Automate CircleCI environment variable registration in Go

Since I wrote a program that automates the registration of environment variables with CircleCI, I will write the created contents.

Task

When setting the environment variables of CircleCI, I registered by clicking from the browser, but registration was troublesome due to simple work, and setting mistakes sometimes occurred due to erroneous operation.

About CircleCI environment variables

First, there are variations in how environment variables are set. In general, you can set environment variables at the following 6 levels. This time, the article is about setting Project Environment Variables. The registration method is set in Build> Project> Settings. (If anyone knows any other registration method, please let me know.)

https://medium.com/veltra-engineering/circleci-2-0-a0549c65bc3b

solution

This time, we will use a library called agouti in Go language and ChromeDriver to automate browser operations. https://agouti.org/ https://chromedriver.chromium.org/downloads

Overall processing flow

① Perform CircleCI Github login authentication https://circleci.com/login/

② Transition to the environment variable setting page

③ Click Add Environment Variable to open the input form

④ Enter Name and Value in the input form.

⑤ Registration completed

Required settings

① Install Chrome Driver.

$ brew install chromedriver

(2) Enter any value in each variable set in const.

--GithubUserName ... Please enter your Github username. --GithubPassWord ... Please enter your Github password. --Organization ... Please enter the Github user name for personal development and the organization name for organizational development. --Repository ... Please enter the target repository name.

const (
   CircleCIURL    = "https://circleci.com/login/"
   GithubUserName = "Your Github username"
   GithubPassWord = "Your Github password"
   Organization   = "Organization name"
   Repository     = "Repository name"
)

③ Preparation of environment variables to be registered In the map below, set the environment variable name in key and the value to be registered in value.

func setVariables(page *agouti.Page) error {
	vars := map[string]string{
		"HOGE": "hoge",
		"PUGE": "puge",
		"FUGA": "fuga",
	}
...
}

The entire source code is posted on Github, so please check it from the URL below. https://github.com/kozy0810/agouti-circleci

Run locally

After completing the above settings, execute the following command locally.

$ go run main.go

When the process is completed, the registration of environment variables is completed as shown below. スクリーンショット 2021-01-10 12.09.40.png

Summary

Described how to automate the registration of CircleCI environment variables in Go language. I think that you can save a lot of time and stress compared to registering by clicking on the browser. When using it, please be careful about security as it will be confidential information. This is the first time I wrote an article about Qiita. If you find it helpful, I would be grateful if you could give me LGTM.

Recommended Posts

Automate CircleCI environment variable registration in Go
Hello World with gRPC / go in Docker environment
Could not import C error in GO environment
Write Pulumi in Go
Go language environment construction
virtualenvwrapper in windows environment
virtual environment in python
Linux PATH environment variable
Development environment in Python