File Coverage

blib/lib/GitHub/Actions.pm
Criterion Covered Total %
statement 48 53 90.5
branch 6 12 50.0
condition 2 2 100.0
subroutine 17 19 89.4
pod 12 12 100.0
total 85 98 86.7


line stmt bran cond sub pod time code
1             package GitHub::Actions;
2              
3 4     4   254013 use Exporter 'import'; # needed to use @EXPORT
  4         33  
  4         123  
4 4     4   18 use warnings;
  4         5  
  4         94  
5 4     4   16 use strict;
  4         5  
  4         60  
6 4     4   15 use Carp;
  4         4  
  4         231  
7              
8 4     4   53 use v5.14;
  4         9  
9              
10             # Module implementation here
11             our %github;
12             our $EXIT_CODE = 0;
13              
14             our @EXPORT = qw( %github set_output set_env debug error warning set_failed command_on_file error_on_file warning_on_file start_group end_group exit_action);
15              
16             BEGIN {
17 4     4   63 for my $k ( keys(%ENV) ) {
18 138 100       254 if ( $k =~ /^GITHUB_/ ) {
19 2         6 my ($nogithub) = ( $k =~ /^GITHUB_(\w+)/ );
20 2         5 $github{$nogithub} = $ENV{$k} ;
21             }
22             }
23             }
24              
25 4     4   1515 use version; our $VERSION = qv('0.1.1.1');
  4         6735  
  4         20  
26              
27             sub set_output {
28 2 50   2 1 2900 carp "Need name and value" unless @_;
29 2         7 my ($output_name, $output_value) = @_;
30 2   100     8 $output_value ||='';
31 2         108 say "::set-output name=$output_name\::$output_value";
32             }
33              
34             sub set_env {
35 0     0 1 0 my ($env_var_name, $env_var_value) = @_;
36 0 0       0 open(my $fh, '>>', $github{'ENV'}) or die "Could not open file ". $github{'ENV'} ." $!";
37 0         0 say $fh "$env_var_name=$env_var_value";
38 0         0 close $fh;
39             }
40              
41             sub debug {
42 1     1 1 1474 my $debug_message = shift;
43 1         36 say "::debug::$debug_message";
44             }
45              
46             sub error {
47 2     2 1 1312 my $error_message = shift;
48 2         5 $EXIT_CODE = 1;
49 2         84 say "::error::$error_message"
50             }
51              
52             sub warning {
53 2     2 1 1238 my $warning = shift;
54 2         44 say "::warning::$warning"
55             }
56              
57             sub error_on_file {
58 1     1 1 1243 command_on_file( "::error", @_ );
59             }
60              
61             sub warning_on_file {
62 1     1 1 1232 command_on_file( "::warning", @_ );
63             }
64              
65             sub command_on_file {
66 3     3 1 1260 my $command = shift;
67 3         11 my $message = shift;
68 3         5 my ($file, $line, $col ) = @_;
69 3 50       8 if ( $file ) {
70 3         3 my @data;
71 3         7 push( @data, "file=$file");
72 3 50       7 push( @data, "line=$line") if $line;
73 3 50       43 push( @data, "col=$col") if $col;
74 3         12 $command .= " ".join(",", @data );
75             }
76 3         102 say $command."::$message"
77             }
78              
79             sub start_group {
80 1     1 1 1242 say "::group::" . shift;
81             }
82              
83             sub end_group {
84 1     1 1 15 say "::endgroup::";
85             }
86              
87             sub set_failed {
88 1     1 1 1070 error( @_ );
89 1         10 exit( 1);
90             }
91              
92             sub exit_action {
93 0     0 1   exit( $EXIT_CODE );
94             }
95              
96             "Action!"; # Magic true value required at end of module
97             __END__
98              
99             =head1 NAME
100              
101             GitHub::Actions - Work in GitHub Actions using Perl
102              
103              
104             =head1 VERSION
105              
106             This document describes GitHub::Actions version 0.1.1.1
107              
108              
109             =head1 SYNOPSIS
110              
111             use GitHub::Actions;
112             use v5.14;
113              
114             # %github contains all GITHUB_* environment variables
115             for my $g (keys %github ) {
116             say "GITHUB_$g -> ", $github{$g}
117             }
118              
119             # Set step output
120             set_output("FOO", "BAR");
121              
122             # Set environment variable value
123             set_env("FOO", "BAR");
124              
125             # Produces an error and sets exit code to 1
126             error( "FOO has happened" )
127              
128             # Exits with error if that's the case
129             exit_action();
130              
131             Install this module within a GitHub action
132              
133             . name: "Install GitHub::Actions"
134             run: sudo cpan GitHub::Actions
135              
136             (we need C<sudo> since we're using the system Perl)
137              
138             You can use this as a C<step>
139              
140             - name: Test env variables
141             shell: perl {0}
142             run: |
143             use GitHub::Actions;
144             set_env( 'FOO', 'BAR');
145              
146             In most cases, you'll want to just have it installed locally and fatpack it to
147             upload it to the repository.
148              
149             =head1 DESCRIPTION
150              
151             GitHub Actions include by default, at least in its Linux runners, a
152             system Perl which you can use directly in your GitHub actions. This here is
153             a (for the time being) minimalistic module that tries to help a bit
154             with that, by defining a few functions that will be useful when
155             performing GitHub actions. Besides the system Perl, you can use any of
156             L<the modules
157             installed|https://gist.github.com/JJ/edf3a39d68525439978da2a02763d42b>. You
158             can install other modules via cpan or, preferably for speed, via the
159             Ubuntu package (or equivalent)
160              
161             Check out an example of using it in the L<repository|https://github.com/JJ/perl-GitHub-Actions/blob/main/.github/workflows/self-test.yml>
162              
163             =head1 INTERFACE
164              
165             =head2 set_env( $env_var_name, $env_var_value)
166              
167             This is equivalent to
168             L<setting an environment variable|https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable>
169              
170             =head2 set_output( $output_name, $output_value)
171              
172             Equivalent to L<C<set_output>|https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-output-parameter>
173              
174             =head2 debug( $debug_message )
175              
176             Equivalent to L<C<debug>|https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-a-debug-message>
177              
178             =head2 error( $error_message )
179              
180             Equivalent to
181             L<C<error>|https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-error-message>,
182             prints an error message. Remember to call L<exit_action()> to make the step fail
183             if there's been some error.
184              
185             =head2 warning( $warning_message )
186              
187             Equivalent to L<C<warning>|https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-a-warning-message>, simply prints a warning.
188              
189             =head2 command_on_file( $error_message, $file, $line, $col )
190              
191             Common code for L<error_on_file> and L<warning_on_file>. Can be used for any future commands.
192              
193             =head2 error_on_file( $error_message, $file, $line, $col )
194              
195             Equivalent to L<C<error>|https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-error-message>, prints an error message with file and line info
196              
197             =head2 warning_on_file( $warning_message, $file, $line, $col )
198              
199             Equivalent to L<C<warning>|https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-a-warning-message>, prints an warning with file and line info.
200              
201             =head2 set_failed( $error_message )
202              
203             Exits with an error status of 1 after setting the error message.
204              
205             =head2 start_group( $group_name )
206              
207             Starts a group in the logs, grouping the following messages. Corresponds to
208             L<C<group>|https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#grouping-log-lines>.
209              
210             =head2 end_group
211              
212             Ends current log grouping.
213              
214             =head2 exit_action
215              
216             Exits with the exit code generated during run, that is, 1 if there's been any
217             error reported.
218              
219             =head1 CONFIGURATION AND ENVIRONMENT
220              
221             GitHub::Actions requires no configuration files or environment
222             variables. Those set by GitHub Actions will only be available there,
223             or if you set them explicitly. Remember that they will need to be set
224             during the C<BEGIN> phase to be available when this module loads.
225              
226             BEGIN {
227             $ENV{'GITHUB_FOO'} = 'foo';
228             $ENV{'GITHUB_BAR'} = 'bar';
229             }
230              
231              
232             =head1 DEPENDENCIES
233              
234             Intentionally, no dependencies are included.
235              
236              
237             =head1 INCOMPATIBILITIES
238              
239             None reported.
240              
241              
242             =head1 BUGS AND LIMITATIONS
243              
244             No bugs have been reported.
245              
246             Please report any bugs or feature requests to L<https://github.com/JJ/perl-GitHub-Actions/issues>.
247              
248              
249             =head1 AUTHOR
250              
251             JJ Merelo C<< <jmerelo@CPAN.org> >>. Many thanks to RENEEB and Gabor Szabo for their help with test and metadata.
252              
253              
254             =head1 LICENCE AND COPYRIGHT
255              
256             Copyright (c) 2021, JJ Merelo C<< <jmerelo@CPAN.org> >>. All rights reserved.
257              
258             This module is free software; you can redistribute it and/or
259             modify it under the same terms as Perl itself. See L<perlartistic>.
260              
261              
262             =head1 DISCLAIMER OF WARRANTY
263              
264             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
265             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
266             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
267             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
268             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
269             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
270             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
271             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
272             NECESSARY SERVICING, REPAIR, OR CORRECTION.
273              
274             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
275             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
276             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
277             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
278             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
279             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
280             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
281             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
282             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
283             SUCH DAMAGES.