此版本的 GitHub Enterprise 将停止服务 2022-02-16. 即使针对重大安全问题,也不会发布补丁。 要获得更好的性能、改进的安全性和新功能,请升级到 GitHub Enterprise 的最新版本。 如需升级方面的帮助,请联系 GitHub Enterprise 支持

Quickstart for GitHub Packages

Publish to GitHub Packages with GitHub Actions.

注: GitHub 托管的运行器目前在 GitHub Enterprise Server 上不受支持。 您可以在 GitHub 公共路线图 上查看有关未来支持计划的更多信息。

Introduction

In this guide, you'll create a GitHub Actions workflow to test your code and then publish it to GitHub Packages.

Publishing your package

  1. Create a new repository on GitHub, adding the .gitignore for Node. Create a private repository if you’d like to delete this package later, public packages cannot be deleted. For more information, see "Creating a new repository."

  2. Clone the repository to your local machine.

    $ git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.git
    $ cd YOUR-REPOSITORY
  3. Create an index.js file and add a basic alert to say "Hello world!"

    JavaScript
    alert("Hello, World!");
  4. Initialize an npm package with npm init. In the package initialization wizard, enter your package with the name: @YOUR-USERNAME/YOUR-REPOSITORY, and set the test script to exit 0. This will generate a package.json file with information about your package.

    $ npm init
      ...
      package name: @YOUR-USERNAME/YOUR-REPOSITORY
      ...
      test command: exit 0
      ...    
  5. Run npm install to generate the package-lock.json file, then commit and push your changes to GitHub.

    $ npm install
    $ git add index.js package.json package-lock.json
    $ git commit -m "initialize npm package"
    $ git push
  6. Create a .github/workflows directory. In that directory, create a file named release-package.yml.

  7. Copy the following YAML content into the release-package.yml file.

    YAML
    name: Node.js Package
    
    on:
      release:
        types: [created]
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
          - uses: actions/setup-node@v2
            with:
              node-version: 12
          - run: npm ci
          - run: npm test
    
      publish-gpr:
        needs: build
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
          - uses: actions/setup-node@v2
            with:
              node-version: 12
              registry-url: https://npm.pkg.github.com/
          - run: npm ci
          - run: npm publish
            env:
              NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
  8. Tell NPM which scope and registry to publish packages to using one of the following methods:

    • Add an NPM configuration file for the repository by creating a .npmrc file in the root directory with the contents:
      @YOUR-USERNAME:registry=https://npm.pkg.github.com
    • Edit the package.json file and specify the publishConfig key:
      "publishConfig": {
         "@YOUR-USERNAME:registry": "https://npm.pkg.github.com"
       }
  9. Commit and push your changes to GitHub.

    $ git add .github/workflows/release-package.yml
    # Also add the file you created or edited in the previous step.
    $ git add .npmrc or package.json
    $ git commit -m "workflow to publish package"
    $ git push
  10. The workflow that you created will run whenever a new release is created in your repository. If the tests pass, then the package will be published to GitHub Packages.

    To test this out, navigate to the Code tab in your repository and create a new release. For more information, see "Managing releases in a repository."

Viewing your published package

You can view all of the packages you have published.

  1. 在 your GitHub Enterprise Server instance 上,导航到仓库的主页面。

  2. 在文件列表右侧,单击 Packages(包)

    概述页面上的包链接

  3. 单击要查看的包的名称。 包名称

Installing a published package

Now that you've published the package, you'll want to use it as a dependency across your projects. For more information, see "Working with the npm registry."

Next steps

The basic workflow you just added runs any time a new release is created in your repository. But this is only the beginning of what you can do with GitHub Packages. You can publish your package to multiple registries with a single workflow, trigger the workflow to run on different events such as a merged pull request, manage containers, and more.

Combining GitHub Packages and GitHub Actions can help you automate nearly every aspect of your application development processes. Ready to get started? Here are some helpful resources for taking your next steps with GitHub Packages and GitHub Actions:

此文档对您有帮助吗?

隐私政策

帮助我们创建出色的文档!

所有 GitHub 文档都是开源的。看到错误或不清楚的内容了吗?提交拉取请求。

做出贡献

或者, 了解如何参与。