Skip to content

Commit 363bd49

Browse files
authored
Replace marketplace action with shell commands (#98)
1 parent 57340a2 commit 363bd49

File tree

7 files changed

+182
-236
lines changed

7 files changed

+182
-236
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: create pull request to Github repository
2+
description: |
3+
Create pull request on Github repository.
4+
5+
inputs:
6+
token:
7+
description: The Github token to use to create pull request.
8+
required: true
9+
repository:
10+
description: The Github repository
11+
required: false
12+
default: ${{ github.repository }}
13+
head_branch:
14+
description: The pull request head branch.
15+
required: true
16+
base_branch:
17+
description: The pull request base, default to the repository default branch.
18+
required: false
19+
title:
20+
description: The pull request title.
21+
required: true
22+
body:
23+
description: The pull request body.
24+
required: true
25+
26+
outputs:
27+
url:
28+
description: The html url of the pull request
29+
value: ${{ steps.create.outputs.url }}
30+
number:
31+
description: The number of the pull request
32+
value: ${{ steps.create.outputs.number }}
33+
34+
runs:
35+
using: composite
36+
steps:
37+
- name: setup python
38+
uses: actions/setup-python@v4
39+
with:
40+
python-version: "3.9"
41+
42+
- name: Install python required libraries
43+
run: pip install -U pygithub
44+
shell: bash
45+
46+
- name: create pull request
47+
id: create
48+
run: >-
49+
python ${{ github.action_path }}/run.py
50+
--repository "${{ inputs.repository }}"
51+
--head "${{ inputs.head_branch }}"
52+
--base "${{ inputs.base_branch }}"
53+
--title "${{ inputs.title }}"
54+
--body "${{ inputs.body }}"
55+
env:
56+
GITHUB_TOKEN: "${{ inputs.token }}"
57+
shell: bash
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
"""Script to read release content from CHANGELOG.rst file."""
3+
4+
import logging
5+
import os
6+
import sys
7+
8+
from argparse import ArgumentParser
9+
10+
from github import Github
11+
from github import GithubException
12+
13+
14+
FORMAT = "[%(asctime)s] - %(message)s"
15+
logging.basicConfig(format=FORMAT)
16+
logger = logging.getLogger(__file__)
17+
logger.setLevel(logging.DEBUG)
18+
19+
20+
def main() -> None:
21+
"""Read release content from CHANGELOG.rst for a specific version."""
22+
parser = ArgumentParser(
23+
description="Read release content from CHANGELOG.rst for a specific version."
24+
)
25+
parser.add_argument("--repository", required=True, help="Repository name.")
26+
parser.add_argument("--head", required=True, help="Pull request head branch.")
27+
parser.add_argument("--base", required=True, help="Pull request base branch.")
28+
parser.add_argument("--title", required=True, help="Pull request title.")
29+
parser.add_argument("--body", required=True, help="Pull request body.")
30+
31+
args = parser.parse_args()
32+
33+
access_token = os.environ.get("GITHUB_TOKEN")
34+
35+
client = Github(access_token)
36+
repo = client.get_repo(args.repository)
37+
try:
38+
pr_obj = repo.create_pull(title=args.title, body=args.body, head=args.head, base=args.base)
39+
except GithubException as err:
40+
logger.error("Failed to create pull request due to: %s", err)
41+
sys.exit(1)
42+
43+
output = os.environ.get("GITHUB_OUTPUT")
44+
if output:
45+
with open(output, "a", encoding="utf-8") as file_handler:
46+
file_handler.write(f"url={pr_obj.html_url}\n")
47+
file_handler.write(f"number={pr_obj.number}\n")
48+
49+
50+
if __name__ == "__main__":
51+
main()

.github/workflows/release-branch.yml

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,82 +3,89 @@ name: Release
33
on:
44
workflow_call:
55
inputs:
6-
release_branch:
7-
description: The release base branch
6+
version:
7+
description: The release version to create.
88
required: true
99
type: string
1010

1111
jobs:
1212
release:
13-
env:
14-
source_path: "./source"
1513
runs-on: ubuntu-latest
1614
permissions:
1715
contents: write
1816
pull-requests: write
1917

2018
steps:
21-
- name: setup python
22-
uses: actions/setup-python@v4
19+
- name: Checkout the repository
20+
uses: actions/checkout@v3
2321
with:
24-
python-version: "3.9"
22+
fetch-depth: "0"
2523

26-
- name: Install python required libraries
27-
run: pip install -U pygithub semver tox pyyaml
24+
- name: Validate version format
25+
run: |-
26+
python -c "import os, re, sys;
27+
version=os.environ.get('RELEASE_VERSION');
28+
print('version <%s> is matching expecting format' % version) if re.match(r'^[0-9]+\.[0-9]+\.[0-9]+$', version) else sys.exit(1)"
2829
shell: bash
30+
env:
31+
RELEASE_VERSION: ${{ inputs.version }}
2932
30-
- name: Download python script
31-
run: >-
32-
curl -o create_release_branch.py
33-
https://raw.githubusercontent.com/ansible-network/github_actions/main/scripts/create_release_branch.py
34-
35-
- name: Compute release version and create release branch
36-
id: compute-version
37-
run: >-
38-
python3 ./create_release_branch.py
33+
- name: Create release branch on Github repository
34+
id: create-branch
35+
run: |
36+
R_BRANCH="stable-$(echo ${RELEASE_VERSION} | cut -d '.' -f1)"
37+
D_BRANCH=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')
38+
echo "release_branch=$R_BRANCH" >> $GITHUB_OUTPUT
39+
git checkout $D_BRANCH
40+
git checkout -b $R_BRANCH && git push origin $R_BRANCH || git checkout $R_BRANCH
41+
shell: bash
3942
env:
40-
REPOSITORY_NAME: ${{ github.repository }}
41-
RELEASE_BRANCH: ${{ inputs.release_branch }}
4243
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44+
RELEASE_VERSION: ${{ inputs.version }}
4345
44-
- name: Checkout the repository
45-
uses: actions/checkout@v3
46+
- name: setup python
47+
uses: actions/setup-python@v4
4648
with:
47-
path: ${{ env.source_path }}
48-
fetch-depth: "0"
49-
if: ${{ (steps.compute-version.outputs.release_version != '') }}
49+
python-version: "3.9"
50+
51+
- name: Install required python modules
52+
run: pip3 install tox yq
53+
shell: bash
5054
5155
- name: Prepare release
5256
run: tox -e prepare_release -vv
5357
shell: bash
54-
working-directory: ${{ env.source_path }}
5558
env:
56-
RELEASE_VERSION: ${{ steps.compute-version.outputs.release_version }}
57-
if: ${{ (steps.compute-version.outputs.release_version != '') }}
59+
RELEASE_VERSION: ${{ inputs.version }}
5860
59-
- name: Download python script used to update galaxy file
60-
run: >-
61-
curl -o update_galaxy_file.py
62-
https://raw.githubusercontent.com/ansible-network/github_actions/main/scripts/update_galaxy_file.py
63-
if: ${{ (steps.compute-version.outputs.release_version != '') }}
61+
- name: Update galaxy.yml file
62+
run: yq -yi ".version = \"$RELEASE_VERSION\"" galaxy.yml
63+
shell: bash
64+
env:
65+
RELEASE_VERSION: ${{ inputs.version }}
6466
65-
- name: Compute release version and create release branch
66-
run: >-
67-
python3 ./update_galaxy_file.py
67+
- name: Push changes to branch on Github repository
68+
id: push-changes
69+
run: |
70+
git checkout -b "prepare_release_${RELEASE_VERSION}"
71+
git add -A
72+
git -c user.name="$GIT_USER_NAME" -c user.email="$GIT_USER_EMAIL" commit -m "Release ${{ inputs.version }}" --author="$GIT_AUTHOR"
73+
git push origin "prepare_release_${RELEASE_VERSION}"
74+
echo "created_branch=prepare_release_${RELEASE_VERSION}" >> $GITHUB_OUTPUT
75+
shell: bash
6876
env:
69-
COLLECTION_PATH: ${{ env.source_path }}
70-
RELEASE_VERSION: ${{ steps.compute-version.outputs.release_version }}
71-
if: ${{ (steps.compute-version.outputs.release_version != '') }}
77+
RELEASE_VERSION: ${{ inputs.version }}
78+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
79+
GIT_USER_NAME: "github-actions[bot]"
80+
GIT_USER_EMAIL: "41898282+github-actions[bot]@users.noreply.github.com"
81+
GIT_AUTHOR: "${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>"
7282
7383
- name: Create Pull Request
74-
uses: peter-evans/create-pull-request@v5
84+
uses: abikouo/github_actions/.github/actions/create_pullrequest@release_v4
7585
with:
7686
token: ${{ secrets.GITHUB_TOKEN }}
77-
path: ${{ env.source_path }}
78-
commit-message: "Release ${{ steps.compute-version.outputs.release_version }}"
79-
base: ${{ inputs.release_branch }}
80-
branch: "prepare_release_${{ steps.compute-version.outputs.release_version }}"
81-
title: "Prepare release ${{ steps.compute-version.outputs.release_version }}"
82-
body: |
83-
Release ${{ steps.compute-version.outputs.release_version }}
84-
if: ${{ (steps.compute-version.outputs.release_version != '') }}
87+
repository: ${{ github.repository }}
88+
base_branch: ${{ steps.create-branch.outputs.release_branch }}
89+
head_branch: ${{ steps.push-changes.outputs.created_branch }}
90+
title: "Prepare release ${{ inputs.version }}"
91+
body: "Automatic changes for Release ${{ inputs.version }}"

.github/workflows/release-tag.yml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,28 @@ jobs:
5353
shell: bash
5454
if: "contains(github.event.pull_request.labels.*.name, 'ok-to-tag')"
5555

56-
- name: create tag
56+
- name: create and push tag to Github repository
57+
id: push-tag
5758
run: |
58-
curl -o create_github_tag.py https://raw.githubusercontent.com/ansible-network/github_actions/main/scripts/create_github_tag.py
59-
python3 ./create_github_tag.py --repository ${{ github.repository }} --tag ${{ steps.read-tag.outputs.release_tag }} --branch ${{ inputs.branch_name }}
59+
git tag ${RELEASE_TAG}
60+
git push origin ${RELEASE_TAG}
61+
# read repository default branch
62+
GIT_DEFAULT_BRANCH=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')
63+
echo "default_branch=$GIT_DEFAULT_BRANCH" >> $GITHUB_OUTPUT
64+
shell: bash
6065
env:
66+
RELEASE_TAG: ${{ steps.read-tag.outputs.release_tag }}
6167
GITHUB_TOKEN: ${{ secrets.gh_token }}
62-
if: "contains(github.event.pull_request.labels.*.name, 'ok-to-tag')"
68+
69+
- name: Create Pull Request from Release branch to default branch
70+
uses: abikouo/github_actions/.github/actions/create_pullrequest@release_v4
71+
with:
72+
token: ${{ secrets.gh_token }}
73+
repository: ${{ github.repository }}
74+
base_branch: ${{ steps.push-tag.outputs.default_branch }}
75+
head_branch: ${{ inputs.branch_name }}
76+
title: "Push changes for release '${{ steps.read-tag.outputs.release_tag }}' on '${{ steps.push-tag.outputs.default_branch }}' branch"
77+
body: "Automatic changes for Release ${{ steps.read-tag.outputs.release_tag }} on Repository default branch"
6378

6479
- name: Parse release content
6580
run: |

scripts/create_github_tag.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)