從 Azure DevOps 推送 source code 到 Gitlab Ci

在 GitLab CI 上弄 ASP.NET Core 真的會搞到自己,來試試看 GitLab 能不能把 source code 推送到 Azure DevPps 上後再做 Build 跟 Test

目標

  1. 透過 merge request events觸發 gitlab ci
  2. 讓事先寫好的 script 執行
  3. 將程式碼 push 到 Azure DevOps Repo 上

流程

產生 ssh key

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ ssh-keygen -C "user@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/user/.ssh/id_rsa.
Your public key has been saved in /c/Users/user/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:******************************************* user@example.com
The key's randomart image is:
+---[RSA 2048]----+
|+. +yX*o . |
|... ..E+*=o |
| ..o.=E=.o |
| . * =.o . |
| . S o o.. |
| + .oo |
| S+. . |
| ..+.+ |
| o*.. |
+----[SHA256]-----+

在 Azure DevOps Services 加入 public key

Connect to your Git repos with SSH - Azure Repos

Create .gitlab-ci.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
image: microsoft/dotnet:latest

stages:
- build

variables:
AZURE_URL: git@ssh.dev.azure.com:v3/Oragnization/Project/gitlab-ci-test

build:
stage: build
script:
- chmod +x Script/*
- ./Script/git-push $AZURE_URL $CI_COMMIT_REF_NAME
only:
- merge_requests

GitLab > Project > Settings > CI/CD > Variables,加入變數 SSH_PRIVATE_KEY

參考連結

  1. IlyaSemenov/gitlab-ci-git-push

  2. Connect to your Git repos with SSH - Azure Repos