Edit and checkin versions.txt file in GitHub Actions using powershell - github-actions

I see the documentation in github actions to run a powershell script in a step. I need to increment version number and checkin versions.txt file in GitHub Actions using powershell. Can I accomplish this by running a powershell script from my folder and by using git.exe and powershell commands?
How do I set a variable from powershell script to use the version number in subsequent steps?

So there are two questions in your question, I will answer in reverse order.
To have one step (for example shell script) set a value for subsequent steps, you can use the special workflow commands which are just done by outputting specially formatted strings to STDOUT.
In your case, are looking for Setting an Output Parameter:
echo "::set-output name=version::1.2.3"
Which can then be read by subsequent steps, like this (for example):
env:
VERSION: ${{ steps.the-other-step-id.outputs.version }}
As for having GitHub actions modify code and then check it in, I would strongly advise against it as it will a) complicate your workflow, b) might create "workflow loops", where actions-originated checkins trigger this/other workflows.
For version tagging, I suggest you use tags as it is the almost universally accepted way to mark versions in source control. You might want to have a local script that both tags and updates your version.txt, and then your workflow just reads the file or the tag.

Related

How can I generate a changelog in github without showing tags?

I am working on a branching strategy in Github Enterprise, using mikepenz/release-changelog-builder-action to generate changelogs when new main versions are created. Changes are merged into a develop branch first, and when the action generates a changelog, it includes all the tags from the develop branch. All we want is to see the changes themselves, grouped by category (feature, fix etc.). Instead we get what you see in the picture. This is a test repo where I pushed one 'bug fix' but as you can see it is very cluttered with unnecessary tags. I have tried to search on Google as well as the action documentation, but either I don't understand it properly or it's not possible. Any help appreciated. This is the default, without a config file. Essentially, I want to remove everything boxed in red.
I figured out a workaround for this. The step that generates the changelog saves it as a .md file. So, I wrote a step that uses bash to take out what I don't want before the file is used to create a release. the lines I don't want start with either a single # with a space after it, ###, or -. Hopefully this is useful to someone.
- name: Remove clutter from changelog
id: trim-changelog
run: |
cat release_changelog.md | grep -e '^# ' -e ^### -e ^- > trimmed_changelog.md

In GitHub Actions is it possible to pass steps to a sub-action?

Is there any way that a step in a workflow job can specify sub-steps to pass to a custom action?
steps:
- uses: ...
with:
steps:
- ...
- ...
- ...
For example, a uses: actions/cache#v2 will attempt to download a snapshot immediately (in the current step), and also inserts a post step to upload a fresh snapshot after all other steps in the parent job. This works well for build processes that (through multiple stages) intelligently recognise which objects need to be recreated and which do not. But it is not suited for workflows that need to set up a clean environment (or generate test data) that may then be manipulated by the tests. I'd like to make a different cache action, that is explicitly passed instructions for how to regenerate the cache from scratch, and which uploads the state before returning to the following step of the parent action.
Is there any way this could be achieved? (Composite actions? Advanced yaml syntax? Template expressions? Low-level actions toolkits/features? Implementing an interpreter to re-parse the input parameter?)

git commits into html filr through jenkins

I am passing a HTML file through Jenkins from build to production servers.
I am injecting the build number into the HTML file using perl.
Now I also want to inject the git commits into the HTML page.
The below is how I injected build number in place of the {BUILD_VERSION} in the HTML page.
perl -pi -e 's/{BUILD_VERSION}/Android_IT_ACCEPTANCE - $ENV{build}/g' index.html
Now, I want git commits in release notes page. I tried this. It didn't workout.
perl -pi -e 's/{RELEASE_NOTES}/Release Notes: $ENV{Change}/g' index.html
I may be misunderstanding you, because it's unclear how you came up with build in the environment, but looking at the Jenkins environment variable docs it kind of looks like you want to be using these:
BUILD_NUMBER The current build number, such as "153"
BUILD_ID The current build id, such as "2005-08-22_23-59-59" (YYYY-MM-DD_hh-mm-ss, defunct since version 1.597)
BUILD_URL The URL where the results of this build can be found (e.g. http://buildserver/jenkins/job/MyJobName/666/)
...
GIT_COMMIT For Git-based projects, this variable contains the Git hash of the commit checked out for the build (like ce9a3c1404e8c91be604088670e93434c4253f03) (all the GIT_* variables require git plugin)
GIT_URL For Git-based projects, this variable contains the Git url (like git#github.com:user/repo.git or [https://github.com/user/repo.git])
GIT_BRANCH For Git-based projects, this variable contains the Git branch that was checked out for the build (normally origin/master)
So maybe you're looking for something more like:
perl -pi -e '
s/{BUILD_VERSION}/Android_IT_ACCEPTANCE - $ENV{BUILD_NUMBER}/g;
s/{RELEASE_NOTES}/Release Notes: $ENV{GIT_COMMIT}/g;
' index.html

Velocity Plugin for Hudson/Jenkins?

Does Jenkins/Hudson have a plugin which invokes something like a Velocity template engine to allows the interpolation of variables into a set of templates in order to generate files?
I have an html page which needs to have the ${BUILD_NUMBER} inserted into it at the proper place each time I do a build.
You can try Groovy Plugin and exploit Groovy's Template Engine feature. Add a Groovy build step and pass ${BUILD_ID} and the path to your HTML template file as parameters. In the build step itself write code that uses ${args[0]},${args[1]} to get the parameters, and then employs SimpleTemplateEngine to process.
I was going to go the Groovy route as suggested (which is a good idea), however instead I took advantage of the fact my build server is on a *Nix OS and instead wrote a line of sed to do the job using the Shell build step.
sed -e '/BUILD_NUMBER/${BUILD_NUMBER}/' ${WORKSPACE}/index.html.template > ${WORKSPACE}/index.html
It just replaces any occurrence of the text: BUILD_NUMBER inside my template file with the Jenkins/Hudson build number. Quick, dirty, but works.

How to get a response from a script back to Hudson and to fail/success emails?

I'm starting a Python script from a Hudson job. The script is started though 'Execute Windows batch command' in build section as 'python my_script.py'
Now I'd need to get some data created by the script back to Hudson and add it to the fail/success emails. My current approach is that the Python script writes data to stderr which is read to a temp file by the batch and then taken into an environment variable. I can see the environment variable correctly right after the script execution (using set command), but in the post-build actions it's not visible any more. The email sending is probably done in different process, so the variables are not visible anymore. I'm accessing the env vars in the email as ${ENV, varname} (or actually in debug mode as $ENV to print them all)
Is there a way to make the environment variable global inside Hudson?
Or can someone provide a better solution for getting data back from Python script to Hudson.
All the related parts (Hudson, batch and Python script) are under my control and can be modified as needed.
Thanks.
Every build step get's is own shell. This implies, that your environment variables are only valid within the build step.
You can just write the data in a nice format to the std output (use a header that is easy to identify) and if the job fails, the data output gets attached in the email.
If you insist on only putting in the data, you can use the following token for the Editable Email Notification post build action (Email-ext plugin).
${BUILD_LOG_REGEX, regex, linesBefore, linesAfter, maxMatches, showTruncatedLines, substText}