I want to automate from project creation to GitHub repo creation, Initial Commit to git push with one command

Introduction

I think Git and GitHub have become indispensable tools when developing,

Create a directory locally, git init, create a README file, git add ., git commit -m'Initial Commit', access GitHub, create a repository, git remote add origin [email protected]: copy and paste the part of hoge / fuga.git, execute the command and git push ~

It's a series of troublesome flows, isn't it?

Now let's create a CLI tool that automates them.

Technology used / required environment

This GitHub repository

Advance preparation

The directory structure is as follows. If you can install gem in the global environment without using Gemfile, you can just use gem install ○○.

[auto_github_repo]
|- .commands.sh
|- .env
|- github.rb
|- Gemfile #Added later
|- Gemfile.lock #Added later

Use a gem called Octokit to access GitHub from Ruby and create repositories. In addition to creating a repository, various APIs are prepared and it is interesting, so it may be good to take a look. Here is a list of various methods. https://www.rubydoc.info/github/pengwynn/octokit/Octokit

Get a GitHub access token

Perhaps many people who work as engineers in companies use GitHub as a two-step certification for security reasons. This time, I created it based on that, and I will explain below because it is necessary to obtain a GitHub access token separately in order to pass the two-step authentication from the Ruby code.

First, access your GitHub, click the icon in the upper right, open Settings, and you will be taken to the following page. Then click Developer settings at the bottom left. スクリーンショット 2020-06-07 15.55.53.png

Then you will be taken to the following page, so please proceed to Personal access tokens. スクリーンショット 2020-06-07 16.00.23.png

Click Generate new token here

スクリーンショット 2020-06-07 16.01.49.png

Then, you will be taken to the following page, so please write the intended use in the Note part and check the repo part. This time, repo is enough for the authority, but it seems that a convenient CLI tool can be created if other parts can be used well. If you can afford it, please try it. スクリーンショット 2020-06-07 16.03.26.png Then click on Generate token at the bottom to go to the page where the access token is displayed, so copy it.

Setting environment variables

Create information such as the access token you created earlier in the .env file. For personal use only, it is okay to write solid code.

.env:.env


USERNAME='GitHub username'
GITHUB_ACCESS_TOKEN='Copyed access token'
FILEPATH='Specify the directory where you want to create the project' #In the case of I/Users/mac username/Program/It is said.

procedure

Now let's make a CLI app.

Creating a Gemfile

Terminal


$ bundle init

This will create a Gemfile, so add the following gem.

Gemfile


# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem 'octokit', '~> 4.0'
gem 'dotenv'

Install Gem

Terminal


$ bundle

Now that we're ready, let's write a shell script to run from the terminal and Ruby code to work with GitHub.

sh:.commands.sh


#!/bin/zsh #For bash, change this "zsh" to bash

function create() {
    source .env #Reading environment variables
    ruby github.rb $1 #Run ruby file$1 is the argument passed in the command (name of the directory to be created)
    cd $FILEPATH$1 #Move to the directory of the created project
    git init
    git remote add origin [email protected]:$USERNAME/$1.git
    touch README.md
    git add .
    git commit -m 'Initial Commit'
    git push -u origin master
    code . #Launch VS Code
}

Now you can run this CLI tool from the terminal, run the Ruby file and even run the Git command.

Next, we will link with GitHub with Ruby.

github.rb



require 'octokit'
require 'dotenv'

# .Read env file
Dotenv.load '.env'

#Assign environment variables to variables
github_access_token =  ENV['GITHUB_ACCESS_TOKEN']
path = ENV['FILEPATH']

#Make the directory top once
system('cd')
#Get the argument (project name to create) from the terminal command
folder_name = ARGV[0].to_s
Dir.mkdir(path + folder_name)
#Work with GitHub on Octokit
client = Octokit::Client.new(access_token: github_access_token)
client.user.login
client.create_repository(folder_name)
puts "Succesfully created repository #{folder_name}"

This completes the CLI app.

Let's create the project you want to create from the terminal.

Terminal


$ source .commands.sh
$create The name of the project you want to create

If the specified project is created in the repository of GitHub, the current directory is moved to the directory of the project name, and VS Code can be started automatically, it is successful.

ezgif-4-4dcfe9e64830.gif

We hope you find it useful as a start dash for development.

Recommended Posts

I want to automate from project creation to GitHub repo creation, Initial Commit to git push with one command
I want to push an app made with Rails 6 to GitHub
I want to play with Firestore from Rails
I want to redirect sound from Ubuntu with xrdp
I want to pass the startup command to postgres with docker-compose.
Addicted to project imports from GitHub
I want to mess with Permission of Windows directory from WSL (ubuntu)
I want to use DBViewer with Eclipse 2018-12! !!