-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
116 lines (106 loc) · 4.6 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
name: Build an Ansible collection
description: >
This action builds an Ansible collection. The resulting artifact (tarball) can be used in other steps or uploaded as a GitHub artifact.
author: "Felix Fontein (@felixfontein)"
inputs:
python-version:
description: >-
The Python version to use for running ansible-core.
default: '3.13'
ansible-core-version:
description: >-
The branch of tag name of ansible-core to install.
This is assumed to exist in https://github.com/ansible/ansible.
default: 'stable-2.18'
subdirectory:
description: >-
The subdirectory in which the collection's sources can be found. Must contain the `galaxy.yml` file.
default: .
collection-requirements-path:
description: >-
If provided, the collection's requirements will be written as a Galaxy `requirements.yml` file to this path.
outputs:
collection-full-name:
description: >-
The collection's full name (namespace.name).
value: ${{ steps.collection-metadata.outputs.namespace }}.${{ steps.collection-metadata.outputs.name }}
collection-name:
description: >-
The collection's name. For example `ansible.posix`'s name is `posix`.
value: ${{ steps.collection-metadata.outputs.name }}
collection-namespace:
description: >-
The collection's namespace. For example `ansible.posix`'s namespace is `ansible`.
value: ${{ steps.collection-metadata.outputs.namespace }}
collection-version:
description: >-
The collection's version. Will be set to `0.0.1` if no `version` field is present in `galaxy.yml`.
value: ${{ steps.collection-metadata.outputs.version }}
artifact-filename:
description: >-
Filename of the built collection artifact.
value: ${{ steps.collection-metadata.outputs.filename }}
artifact-path:
description: >-
Path to the built collection artifact.
value: ${{ steps.collection-metadata.outputs.path }}
runs:
using: composite
steps:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}
- name: Install ansible-core
shell: bash
env:
ANSIBLE_CORE_VERSION: ${{ inputs.ansible-core-version }}
run: pip install "https://github.com/ansible/ansible/archive/${ANSIBLE_CORE_VERSION}.tar.gz" --disable-pip-version-check
- name: Make sure galaxy.yml has version entry, and extract information
shell: python
id: collection-metadata
env:
SUBDIRECTORY: ${{ inputs.subdirectory }}
COLLECTION_REQUIREMENTS_PATH: ${{ inputs.collection-requirements-path }}
run: |
# Make sure galaxy.yml has version entry, and extract information
import os
import yaml
def set_output(name, value):
with open(os.environ['GITHUB_OUTPUT'], 'a', encoding='utf-8') as f:
f.write(f'{name}={value}{os.linesep}')
# Make sure galaxy.yml contains a version
galaxy_filename = os.path.join(os.environ['SUBDIRECTORY'], 'galaxy.yml')
with open(galaxy_filename, 'rb') as f:
data = yaml.safe_load(f) ;
data['version'] = data.get('version') or '0.0.1'
with open(galaxy_filename, 'w', encoding='utf-8') as f:
f.write(yaml.dump(data))
# Create Galaxy requirements file
requirements_filename = os.environ['COLLECTION_REQUIREMENTS_PATH']
if requirements_filename:
reqs = dict(collections=[])
if data.get('dependencies'):
for collection, version in sorted(data['dependencies'].items()):
reqs['collections'].append(dict(
name=collection,
source='https://galaxy.ansible.com',
version=version,
))
with open(requirements_filename, 'w', encoding='utf-8') as f:
f.write(yaml.dump(reqs))
# Extract namespace and collection name
set_output('name', data['name'])
set_output('namespace', data['namespace'])
set_output('version', data['version'])
artifact_filename = f"{data['namespace']}-{data['name']}-{data['version']}.tar.gz"
set_output('filename', artifact_filename)
set_output('path', os.path.join(os.environ['SUBDIRECTORY'], artifact_filename))
- name: Build collection
shell: bash
run: ansible-galaxy collection build
working-directory: ${{ inputs.subdirectory }}