1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

gitlabにコマンドでたくさんプロジェクト(リポジトリ)とブランチを作ってみる

Last updated at Posted at 2024-08-09

概要

表題の通り
個人のAWSでgitlabをインストールして試してみる。(注:EC2はt3.largeにしないと起動してくれなかった)
グループ(githubでいうOrganization)はhello配下にプロジェクト(githubでいうリポジトリ)をスクリプトで作る。スクリプトはchatgptに頼む
あくまで個人環境で権限の制約がない環境で実行していると思うので、実際の環境ではどうなるかわからない。
tokenはさらしているが、この後インスタンスは停止する。

chatgptに依頼したこと

gitlabに以下の機能のスクリプトを作りたい
プロジェクト作成。作成するときにnamespaceも指定する。変数で指定します。
mainブランチにreadme登録。中身はリポジトリ名
ブランチを作成する。ブランチは0または複数ある
プロジェクト名とブランチはファイルから与えたい。
プロジェクトはpublicで作るようにしたい

実行結果

これが初期状態

image.png

リポジトリは空っぽです

リポジトリを作る

image.png

ブランチもできてるね
image.png

うまくいったね。AIしゅごい

実行ログ
tsuyoshi@DESKTOP-TBB7HV0:~/gitlab$ ./create_project.sh
Created public project: project1 (ID: 14)
{"id":"2f9a83fe9188274e2d39f64ee96ca812848d7ad4","short_id":"2f9a83fe","created_at":"2024-08-09T14:28:59.000+00:00","parent_ids":[],"title":"Add README","message":"Add README","author_name":"create_Repo","author_email":"group_2_bot_d6b649c2aedb1e9c6f0b32333e43f754@noreply.13.230.155.78","authored_date":"2024-08-09T14:28:59.000+00:00","committer_name":"create_Repo","committer_email":"group_2_bot_d6b649c2aedb1e9c6f0b32333e43f754@noreply.13.230.155.78","committed_date":"2024-08-09T14:28:59.000+00:00","trailers":{},"extended_trailers":{},"web_url":"http://13.230.155.78/gitlab/hello/project1/-/commit/2f9a83fe9188274e2d39f64ee96ca812848d7ad4","stats":{"additions":1,"deletions":0,"total":1},"status":null,"project_id":14,"last_pipeline":null}Added README to main branch in project: project1
{"name":"feature1","commit":{"id":"2f9a83fe9188274e2d39f64ee96ca812848d7ad4","short_id":"2f9a83fe","created_at":"2024-08-09T14:28:59.000+00:00","parent_ids":[],"title":"Add README","message":"Add README","author_name":"create_Repo","author_email":"group_2_bot_d6b649c2aedb1e9c6f0b32333e43f754@noreply.13.230.155.78","authored_date":"2024-08-09T14:28:59.000+00:00","committer_name":"create_Repo","committer_email":"group_2_bot_d6b649c2aedb1e9c6f0b32333e43f754@noreply.13.230.155.78","committed_date":"2024-08-09T14:28:59.000+00:00","trailers":{},"extended_trailers":{},"web_url":"http://13.230.155.78/gitlab/hello/project1/-/commit/2f9a83fe9188274e2d39f64ee96ca812848d7ad4"},"merged":false,"protected":false,"developers_can_push":false,"developers_can_merge":false,"can_push":true,"default":false,"web_url":"http://13.230.155.78/gitlab/hello/project1/-/tree/feature1"}Created branch: feature1 in project: project1
 in project: project1me is invalid"}Created branch: feature2
Created public project: project2 (ID: 15)
{"id":"654d08ec8b3e0eb2933644b2bcd2eacb571d5ca1","short_id":"654d08ec","created_at":"2024-08-09T14:29:05.000+00:00","parent_ids":[],"title":"Add README","message":"Add README","author_name":"create_Repo","author_email":"group_2_bot_d6b649c2aedb1e9c6f0b32333e43f754@noreply.13.230.155.78","authored_date":"2024-08-09T14:29:05.000+00:00","committer_name":"create_Repo","committer_email":"group_2_bot_d6b649c2aedb1e9c6f0b32333e43f754@noreply.13.230.155.78","committed_date":"2024-08-09T14:29:05.000+00:00","trailers":{},"extended_trailers":{},"web_url":"http://13.230.155.78/gitlab/hello/project2/-/commit/654d08ec8b3e0eb2933644b2bcd2eacb571d5ca1","stats":{"additions":1,"deletions":0,"total":1},"status":null,"project_id":15,"last_pipeline":null}Added README to main branch in project: project2
{"name":"develop","commit":{"id":"654d08ec8b3e0eb2933644b2bcd2eacb571d5ca1","short_id":"654d08ec","created_at":"2024-08-09T14:29:05.000+00:00","parent_ids":[],"title":"Add README","message":"Add README","author_name":"create_Repo","author_email":"group_2_bot_d6b649c2aedb1e9c6f0b32333e43f754@noreply.13.230.155.78","authored_date":"2024-08-09T14:29:05.000+00:00","committer_name":"create_Repo","committer_email":"group_2_bot_d6b649c2aedb1e9c6f0b32333e43f754@noreply.13.230.155.78","committed_date":"2024-08-09T14:29:05.000+00:00","trailers":{},"extended_trailers":{},"web_url":"http://13.230.155.78/gitlab/hello/project2/-/commit/654d08ec8b3e0eb2933644b2bcd2eacb571d5ca1"},"merged":false,"protected":false,"developers_can_push":false,"developers_can_merge":false,"can_push":true,"default":false,"web_url":"http://13.230.155.78/gitlab/hello/project2/-/tree/develop"}Created branch: develop in project: project2
 in project: project2me is invalid"}Created branch: hotfix
Created public project: project3 (ID: 16)
{"id":"de706529f774f52127143faae4a88d8c79715bb3","short_id":"de706529","created_at":"2024-08-09T14:29:10.000+00:00","parent_ids":[],"title":"Add README","message":"Add README","author_name":"create_Repo","author_email":"group_2_bot_d6b649c2aedb1e9c6f0b32333e43f754@noreply.13.230.155.78","authored_date":"2024-08-09T14:29:10.000+00:00","committer_name":"create_Repo","committer_email":"group_2_bot_d6b649c2aedb1e9c6f0b32333e43f754@noreply.13.230.155.78","committed_date":"2024-08-09T14:29:10.000+00:00","trailers":{},"extended_trailers":{},"web_url":"http://13.230.155.78/gitlab/hello/project3/-/commit/de706529f774f52127143faae4a88d8c79715bb3","stats":{"additions":1,"deletions":0,"total":1},"status":null,"project_id":16,"last_pipeline":null}Added README to main branch in project: project3
 in project: project3mpty"}Created branch: 

プロジェクトを全部消してみる。成功

tsuyoshi@DESKTOP-TBB7HV0:~/gitlab$ ./delete_project_all.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2293  100  2293    0     0   7527      0 --:--:-- --:--:-- --:--:--  7542
{"message":"202 Accepted"}{"message":"202 Accepted"}{"message":"202 Accepted"}tsuyoshi@DESKTOP-TBB7HV0:~/gitlab$ 

image.png

コード

作成するプロジェクトとブランチを設定するファイル

projects_and_branches.txt
project1:feature1,feature2
project2:develop,hotfix
project3:

プロジェクトとブランチを作るコード

create_project.sh
#!/bin/bash

# GitLabのURLとパーソナルアクセストークン
GITLAB_URL="http://13.230.155.78/gitlab/api/v4" # ★ここは環境で変わるよ
TOKEN="glpat-4wzwaGQusx4epDkWNYiY" # ★ここは環境で変わるよ
NAMESPACE_ID="2"  # hello ネームスペースIDを指定  ★ここは環境で変わるよ

# プロジェクトとブランチのリストファイル
FILE="projects_and_branches.txt"

# ファイルを読み込み、各プロジェクトとブランチを作成
while IFS=: read -r project branches; do
  # プロジェクトを作成
  response=$(curl --silent --header "PRIVATE-TOKEN: $TOKEN" \
            --data "name=$project&namespace_id=$NAMESPACE_ID&visibility=public" \
            "$GITLAB_URL/projects")
  
  # プロジェクトIDを取得
  project_id=$(echo $response | jq -r '.id')

  if [[ "$project_id" == "null" ]]; then
    echo "Error creating project: $project"
    echo "Response: $response"
    continue
  fi
  
  echo "Created public project: $project (ID: $project_id)"
  
  # 1秒待機
  sleep 1

  # READMEファイルを追加
  readme_content="# $project"
  curl --silent --request POST --header "PRIVATE-TOKEN: $TOKEN" \
  --data "branch=main&start_branch=main&commit_message=Add README&actions[][action]=create&actions[][file_path]=README.md&actions[][content]=$readme_content" \
  "$GITLAB_URL/projects/$project_id/repository/commits"
  
  echo "Added README to main branch in project: $project"
  
  # 1秒待機
  sleep 1

  # ブランチを作成
  IFS=',' read -ra branch_list <<< "$branches"
  for branch in "${branch_list[@]}"; do
    # ブランチ作成
    curl --silent --request POST --header "PRIVATE-TOKEN: $TOKEN" \
    --data "branch=$branch&ref=main" \
    "$GITLAB_URL/projects/$project_id/repository/branches"
    
    echo "Created branch: $branch in project: $project"
    
    # 1秒待機
    sleep 1
  done
done < "$FILE"

helloネームスペースのIDが必要になったので以下のコマンドで調べたよ

curl --header "PRIVATE-TOKEN: glpat-4wzwaGQusx4epDkWNYiY" "http://13.230.155.78/gitlab/api/v4/namespaces"

[{"id":2,"name":"hello ...
ー>hello namespace(organization)のIDが2ということ

すべてのプロジェクトを削除するコード

おそらく100を超えるプロジェクトは削除できない気がする。100を超える場合は、全部処理するように少し改良がいる。

delete_project_all.sh
for project_id in $(curl --header "PRIVATE-TOKEN: glpat-4wzwaGQusx4epDkWNYiY" "http://13.230.155.78/gitlab/api/v4/projects?simple=true&per_page=100" |  jq -r '.[].id'); do
  curl --request DELETE --header "PRIVATE-TOKEN: glpat-4wzwaGQusx4epDkWNYiY" "http://13.230.155.78/gitlab/api/v4/projects/${project_id}"
done

tsuyoshi@DESKTOP-TBB7HV0:~/gitlab$ ./delete_project_all.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2293  100  2293    0     0   7527      0 --:--:-- --:--:-- --:--:--  7542
{"message":"202 Accepted"}{"message":"202 Accepted"}{"message":"202 Accepted"}

懸念

  • tokenはowner roleの権限で行った。実際はどうなるのかわからない。権限が不足したら動かないかもしれない
  • スクリプトは読んでない

gitlabセットアップコマンド

EC2はt3.large、OSはubuntu
こんな感じ。IPはelastic IPを割り当てよう
ディスク容量が8GB->12GBぐらいに増やそう

Elastic IP アドレス

セットアップ

cd C:\Users\user.ssh
ssh ubuntu@XXXX

sudo apt update;sudo apt upgrade -y;sudo apt install -y curl openssh-server ca-certificates tzdata perl;curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash


# EXTERNAL_URLをpublic IPに変えよう! =>toki.moritoki-test.clickでもいける!?
sudo EXTERNAL_URL="http://13.230.155.78/gitlab" apt install -y gitlab-ee;sudo gitlab-ctl reconfigure;sudo gitlab-ctl restart;sudo gitlab-ctl status;sudo systemctl is-enabled gitlab-runsvdir;sudo cat /etc/gitlab/gitlab.rb|grep external_url; sudo cat /etc/gitlab/initial_root_password

http://13.230.155.78/gitlab を開く
アカウント:root
パスワード:画面にパスワードが出ているはずなので、それを入力(/etc/gitlab/initial_root_password)の中身がパスワード)

apiのマニュアル

追加、windowsのpowersherll版

実行方法

cd スクリプトを設置した場所
# create
powershell -ExecutionPolicy Bypass -File .\CreateGitLabProjects.ps1
# delete
powershell -ExecutionPolicy Bypass -File .\DeleteGitLabProjects.ps1

ヘルプ

GitLab Self Managed に接続画面の出来事、CodeConnectionsをつかっている

codebuildとgitlabを接続させようとした
接続名:gitlab-connect
URL:https://XXXXX/gitlab # httpだと設定不可(怒)

エラーとなった
Provider endpoint is not valid

https化はALBとACMを使った。サーバ内ではhttps化はしてない。そのせい?

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?