├── .github └── workflows │ └── test.yml ├── LICENSE ├── README.md ├── action.gemspec ├── action.yml └── main.rb /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test Action 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | 9 | jobs: 10 | test: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Checkout 14 | uses: actions/checkout@v2 15 | - name: Test 16 | uses: ./ 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 Dawid Dziurla 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Publish gem Github Action 2 | 3 | An action that builds and then publishes a Ruby gem to [rubygems.org](https://rubygems.org) or GitHub Packages. 4 | 5 | Will find every `.gemspec` file in current directory and publish a gem for it. 6 | 7 | Runs on `ubuntu` and `macos`. 8 | 9 | ## Usage 10 | 11 | ```yaml 12 | on: 13 | push: 14 | tags: 15 | - '*' 16 | 17 | --- 18 | 19 | - name: Publish gem 20 | uses: dawidd6/action-publish-gem@v1 21 | with: 22 | # Optional, will publish to RubyGems if specified 23 | api_key: ${{secrets.RUBYGEMS_API_KEY}} 24 | # Optional, will publish to GitHub Packages if specified 25 | github_token: ${{secrets.GITHUB_TOKEN}} 26 | ``` 27 | 28 | > It is possible to publish both to RubyGems and GitHub Packages 29 | -------------------------------------------------------------------------------- /action.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |spec| 2 | spec.name = "action" 3 | spec.version = "0.0.0" 4 | spec.authors = ["Dawid Dziurla"] 5 | spec.email = ["dawidd0811@gmail.com"] 6 | 7 | spec.summary = "Test gem" 8 | spec.homepage = "https://github.com/dawidd6/action-publish-gem" 9 | spec.license = "MIT" 10 | spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0") 11 | 12 | spec.metadata = { "github_repo" => spec.homepage } 13 | 14 | spec.files = `git ls-files`.split("\n") 15 | end 16 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: Publish gem 2 | description: Build and publish a Ruby gem 3 | author: dawidd6 4 | branding: 5 | icon: upload-cloud 6 | color: red 7 | inputs: 8 | api_key: 9 | description: Rubygems.org API key 10 | required: false 11 | github_token: 12 | description: GitHub Packages token 13 | required: false 14 | runs: 15 | using: composite 16 | steps: 17 | - run: ruby -W0 $GITHUB_ACTION_PATH/main.rb 18 | shell: sh 19 | env: 20 | API_KEY: ${{inputs.api_key}} 21 | GITHUB_TOKEN: ${{inputs.github_token}} 22 | -------------------------------------------------------------------------------- /main.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'fileutils' 4 | 5 | def safe_system(*cmd) 6 | exit 1 unless system(*cmd) 7 | end 8 | 9 | api_key = ENV['API_KEY'] 10 | github_token = ENV['GITHUB_TOKEN'] 11 | github_repository_owner = ENV["GITHUB_REPOSITORY_OWNER"] 12 | github_url = "https://rubygems.pkg.github.com/#{github_repository_owner}" 13 | 14 | credentials_dir_path = "#{Dir.home}/.gem" 15 | credentials_file_path = "#{credentials_dir_path}/credentials" 16 | credentials = <<~END_OF_CREDENTIALS 17 | --- 18 | :rubygems_api_key: #{api_key} 19 | :github: Bearer #{github_token} 20 | END_OF_CREDENTIALS 21 | 22 | FileUtils.mkdir_p(credentials_dir_path) 23 | File.open(credentials_file_path, 'w') { |f| f.write(credentials) } 24 | FileUtils.chmod(0o600, credentials_file_path) 25 | 26 | begin 27 | Dir.glob('*.gemspec').each do |gemspec_file| 28 | gemspec = Gem::Specification.load(gemspec_file) 29 | gem_file = gemspec.full_name + '.gem' 30 | safe_system('gem', 'build', gemspec_file) 31 | safe_system('gem', 'push', gem_file) unless api_key.empty? 32 | safe_system('gem', 'push', gem_file, "--key", "github", "--host", github_url) unless github_token.empty? 33 | end 34 | ensure 35 | FileUtils.rm(credentials_file_path) 36 | end --------------------------------------------------------------------------------