如何在 Github Action 中不使用 gitignore?

huangapple go评论97阅读模式
英文:

How can I not gitignore in a Github Action?

问题

抱歉,标题中的措辞不太好。基本上,我有一个使用TypeScript(不是Node)的网站,我想通过Github Pages进行托管。我有一个tsconfig.json文件,大致如下:

{
	"compilerOptions": {
		"rootDir": "./src",
		"outDir": "./js"
	},

	"include": ["./src"]
}

我不想将编译后的代码放在主分支上,所以我的.gitignore文件包含了以下内容:

js/

为了使我的项目在Github Pages上工作,我设置了action-build-typescript来将我的TypeScript编译到一个单独的分支上,这个分支将作为Github Pages的源代码。

on:
  push:
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout project
      uses: actions/checkout@v3
    - name: Build and push
      uses: alexthemaster/action-build-typescript@master
      with:
        pushToBranch: true
        branch: "gh-pages"
        githubToken: ${{ secrets.GITHUB_TOKEN }}

这个方法很好用,但是js/目录没有被创建。经过一番调查,发现当这个操作推送到gh-pages分支时,.gitignore仍然生效,因此我的JavaScript代码没有被提交。

有没有更好的方法可以解决这个问题?我应该将编译后的代码推送到main分支吗?

英文:

Sorry for the poor wording in the title. Essentially, I have a website with Typescript (not Node) that I want to host via Github Pages. I have a tsconfig.json that essentially looks like this:

{
	"compilerOptions": {
		"rootDir": "./src",
		"outDir": "./js"
	},

	"include": ["./src"]
}

I don't want my compiled code on the main branch, so my gitignore contains

js/

In order to have my project work on Github Pages, I set up action-build-typescript to compile my Typescript onto a separate branch, which would be the source for Github pages.

on:
  push:
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout project
      uses: actions/checkout@v3
    - name: Build and push
      uses: alexthemaster/action-build-typescript@master
      with:
        pushToBranch: true
        branch: "gh-pages"
        githubToken: ${{ secrets.GITHUB_TOKEN }}

This works great, however the js/ directory wasn't being created. Some digging revealed that when this action was pushing to the gh-pages branch, the .gitignore was still being applied, thus my Javascript wasn't being committed.

Is there a better way I can do this? Should I just push my compiled code to main?

答案1

得分: 1

如果你在gh-pages分支上添加一个提交,删除(或注释掉).gitignore文件中的这一行,这些文件将会在该分支上的提交中被添加。

也许你需要先切换到gh-pages分支并从源分支合并,这样gh-pages分支上的.gitignore文件才会用于提交。

另一种方法是:
在构建和推送步骤之前,从.gitignore文件中删除这一行。为了实现这一点,你可以在源分支中用一个准备好的.gitignore-pages文件覆盖它。修改后的.gitignore文件将被提交到gh-pages分支,但由于你可能永远不会将其合并回源分支,这不会有问题。

英文:

If you add a commit to the gh-pages branch that removes (or comments out) this line in the .gitignore file, the files will be added in commits on this branch.

Perhaps you have to checkout the gh-pages branch and merge from the source branch first, so that the .gitignore file from the gh-pages branch is used for the commit.

Alternative:
Remove the line from the .gitignore file before the build and push step. To achieve this, you can just overwrite it with a prepared .gitignore-pages file in your source branch. The changed .gitignore file will be committed to the gh-pages branch, but since you probably will never merge ist back to your source branch, this will be no problem.

huangapple
  • 本文由 发表于 2023年8月9日 04:39:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定